引导界面框架

news/2024/7/7 20:29:28

效果图:

          

首先自定义一个小红点;

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
    <size android:height="10dp" android:width="10dp"/>
    <solid android:color="@android:color/holo_red_light"/>
</shape>

 未被选中时的小点:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
    <size android:height="10dp" android:width="10dp"/>
    <solid android:color="@android:color/darker_gray"/>
</shape>

布局:一个按钮,一个线性布局用来加载小红点,一个viewpaer

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"  >
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <Button
        android:id="@+id/btn_start_main"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="80dp"
        android:background="@drawable/btn_start_main_selector"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:text="开始体验"
        android:visibility="gone"
        android:textColor="@drawable/btn_start_main_textcolor_selector"
        android:textSize="20sp" />
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="40dp">
        <LinearLayout
            android:id="@+id/ll_point_group"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" />
        <ImageView
            android:id="@+id/iv_red_point"
            android:background="@drawable/point_red"
            android:layout_width="10dp"
            android:layout_height="10dp" />
    </RelativeLayout>
</RelativeLayout>


引导界面activity的实现:

public class GuideActivity extends Activity {


    private static final String TAG = GuideActivity.class.getSimpleName();
    private ViewPager viewpager;
    private Button btn_start_main;
    private LinearLayout ll_point_group;
    private ImageView iv_red_point;


    private ArrayList<ImageView> imageViews;
    /**
     * 两点的间距
     */
    private int leftmax;


    private int widthdpi ;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        viewpager = (ViewPager) findViewById(R.id.viewpager);
        btn_start_main = (Button) findViewById(R.id.btn_start_main);
        ll_point_group = (LinearLayout) findViewById(R.id.ll_point_group);
        iv_red_point = (ImageView) findViewById(R.id.iv_red_point);


        //准备数据
        int[] ids = new int[]{
                R.drawable.guide_1,
                R.drawable.guide_2,
                R.drawable.guide_3
        };


        widthdpi = DensityUtil.dipTopx(this,10);
        Log.e(TAG,widthdpi+"--------------");


        imageViews = new ArrayList<>();
        for (int i = 0; i < ids.length; i++) {
            ImageView imageView = new ImageView(this);
            //设置背景
            imageView.setBackgroundResource(ids[i]);


            //添加到集合中
            imageViews.add(imageView);


            //创建点
            ImageView point = new ImageView(this);
            point.setBackgroundResource(R.drawable.point_normal);
            /**
             * 单位是像数
             * 把单位当成dp转成对应的像数
             */
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(widthdpi,widthdpi);
            if(i !=0){
                //不包括第0个,所有的点距离左边有10个像数
                params.leftMargin = widthdpi;
            }
            point.setLayoutParams(params);
            //添加到线性布局里面
            ll_point_group.addView(point);
        }
        //设置ViewPager的适配器
        viewpager.setAdapter(new MyPagerAdapter());
        //根据View的生命周期,当视图执行到onLayout或者onDraw的时候,视图的高和宽,边距都有了
        iv_red_point.getViewTreeObserver().addOnGlobalLayoutListener(new MyOnGlobalLayoutListener());
        //得到屏幕滑动的百分比
        viewpager.addOnPageChangeListener(new MyOnPageChangeListener());
        //设置按钮的点击事件
        btn_start_main.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //1.保存曾经进入过主页面
                ShareUtils.putBoolean(GuideActivity.this, ConstantValue.START_MAIN,true);
                //2.跳转到主页面
                Intent intent = new Intent(GuideActivity.this,MainActivity.class);
                startActivity(intent);
                //3.关闭引导页面
                finish();
            }
        });
    }
    class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {
        /**
         * 当页面回调了会回调这个方法
         * @param position 当前滑动页面的位置
         * @param positionOffset 页面滑动的百分比
         * @param positionOffsetPixels 滑动的像数
         */
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            //两点间移动的距离 = 屏幕滑动百分比 * 间距
//            int leftmargin = (int) (positionOffset * leftmax);
//            Log.e(TAG,"position=="+position+",positionOffset=="+positionOffset+",positionOffsetPixels=="+positionOffsetPixels);
            //两点间滑动距离对应的坐标 = 原来的起始位置 +  两点间移动的距离
            int leftmargin = (int) (position*leftmax +  (positionOffset * leftmax));
            //params.leftMargin = 两点间滑动距离对应的坐标
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) iv_red_point.getLayoutParams();
            params.leftMargin = leftmargin;
            iv_red_point.setLayoutParams(params);
        }
        /**
         * 当页面被选中的时候,回调这个方法
         * @param position 被选中页面的对应的位置
         */
        @Override
        public void onPageSelected(int position) {
            if(position==imageViews.size()-1){
                //最后一个页面
                btn_start_main.setVisibility(View.VISIBLE);
            }else{
                //其他页面
                btn_start_main.setVisibility(View.GONE);
            }
        }


http://www.niftyadmin.cn/n/3649431.html

相关文章

prisma orm_如何使用Node.js和Prisma构建GraphQL服务器

prisma orm介绍 (Introduction) Prisma is a data layer that turns a database into a GraphQL API. We can look at it as a kind of Object-Relational Mapper (ORM), but it’s much more powerful than traditional ORMs. With Prisma, we get a server (Prisma server) t…

CentOS 7搭建Java开发平台——Java 8

Java是一门面向对象编程语言&#xff0c;不仅吸收了C语言的各种优点&#xff0c;还摒弃了C里难以理解的多继承、指针等概念&#xff0c;因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表&#xff0c;极好地实现了面向对象理论&#xff0c;…

css3字体弹跳动画_如何使用CSS3动画创建弹跳页面加载器

css3字体弹跳动画介绍 (Introduction) In this tutorial, you will create a bouncing page loader using CSS3 animation keyframes. It will show you how to style HTML for a loading page, create animation keyframes, and use animation delay with keyframes. 在本教程…

[Remoting]当client不复存在而RemoteObject并不知道时的处理办法

[Remoting]当client不复存在而RemoteObject并不知道时的处理办法编写者&#xff1a;郑昀ultrapower 20050518问题&#xff1a;“singleton服务中客户端意外退出或网络故障时&#xff0c;服务器端如何知道&#xff0c;并作相应的业务层处理”。背后的故事&#xff1a;对于这个问…

Windows 10搭建Java开发平台——Java 8

Java是一门面向对象编程语言&#xff0c;不仅吸收了C语言的各种优点&#xff0c;还摒弃了C里难以理解的多继承、指针等概念&#xff0c;因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表&#xff0c;极好地实现了面向对象理论&#xff0c;…

连接到外部sql server工具类

首先第一步&#xff1a;我们需要下载一个jtds驱动让Android连接数据库 jtds下载地址&#xff1a;http://sourceforge.net/projects/jtds/files/ 第二步&#xff1a;数据库连接和测试类DataBaseUtil.java public class DataBaseUtil {private static Connection getSQLConnec…

什么是用户体验地图?该如何绘制?

什么是用户体验地图&#xff1f; 就像打仗需要地形图&#xff0c;体验提升的战斗也需要一个蓝图来规划和指引。 用户体验地图(Experience Maps)也被称为使用者旅程图(User Journey Map)。 用直白的话来解释下&#xff1a;用户体验地图就是通过一张图&#xff0c;用一种讲故事的…

如何在Ubuntu 18.04上使用复制迁移Redis数据

介绍 (Introduction) Redis is an in-memory, key-value data store known for its flexibility, performance, wide language support, and built-in features like replication. Replication is the practice of regularly copying data from one database to another in ord…