欢迎界面与引导界面——ViewPager

这是运行效果图,里面的图片是我随意加的,

这是欢迎界面,就是每次登陆都显示的界面,


这是引导界面,只有第一次登录显示,依次像右划,



一.布局界面

1.activity_splash.xml

<?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:id="@+id/activity_splash"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/start"
    tools:context="cn.edu.bzu.shop.MainActivity">
</RelativeLayout>
2.activity_guide.xml

<?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:id="@+id/activity_guide"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="cn.edu.bzu.shop.GuideActivity">
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v4.view.ViewPager>
    <Button
        android:id="@+id/button"
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="500dp"
        android:background="@drawable/guide_start"
        android:visibility="gone"/>
</RelativeLayout>
3.guide_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/iv_guide"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>
二.Activity

1.SplashActivity

public class SplashActivity extends AppCompatActivity {
    Handler handler=new Handler();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                SharedPreferences sp=getPreferences(MODE_PRIVATE);
                boolean isFirst=sp.getBoolean("isFirst",true);
                Intent intent=new Intent();//意图对象
                if(isFirst){
                    //如果用户第一次安装或登录
                    intent.setClass(SplashActivity.this,GuideActivity.class);
                    sp.edit().putBoolean("isFirst",false).commit();
                }else{
                    intent.setClass(SplashActivity.this,MainActivity.class);
                }
                startActivity(intent);
                finish();
            }
        },3000);
    }
}
2.GuideActivity

public class GuideActivity extends AppCompatActivity {
    private ViewPager MvpGuide;
    private Button btnStart;
    private List<View> mviewList=new ArrayList<>();
    int [] imgRes=new int[]{
            R.drawable.icon1,
            R.drawable.icon2,
            R.drawable.icon3,
            R.drawable.icon4
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        InitData();
        btnStart= (Button) findViewById(R.id.button);
        MvpGuide= (ViewPager) findViewById(R.id.viewpager);
        MyPagerAdapter adapter=new MyPagerAdapter();
        MvpGuide.setAdapter(adapter);
        MvpGuide.setOnPageChangeListener(new MyListener());
        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(GuideActivity.this,MainActivity.class));
                finish();
            }
        });
    }
    /**创建初始化数据**/
    private void InitData() {
        for(int i=0;i<imgRes.length;i++){
            View inflater=getLayoutInflater().inflate(R.layout.guide_item,null);
            ImageView iv_Guide= (ImageView) inflater.findViewById(R.id.iv_guide);
            iv_Guide.setBackgroundResource(imgRes[i]);
            mviewList.add(inflater);
        }
    }
    /**引导界面的ViewPager适配器**/
    class MyPagerAdapter extends PagerAdapter{
        @Override
        public int getCount() {
            return imgRes.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mviewList.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view=mviewList.get(position);
            container.addView(view);
            return view;
        }
    }
    class MyListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }
//ViewPager滚动到第几张
        @Override
        public void onPageSelected(int position) {
            //ViewPager到最后一张图片显示按钮
            if(position==imgRes.length-1){
                btnStart.setVisibility(View.VISIBLE);
                Animation animation=AnimationUtils.loadAnimation(GuideActivity.this,R.anim.anim_guide_btn_start);
                btnStart.startAnimation(animation);
            }else {
                btnStart.setVisibility(View.GONE);
            }
        }
        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }
}
三.动画

anim_guide_btn_start.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="0.3"
    android:toAlpha="1"
    android:duration="500"/>
这个小的显示有些复杂,我只是粘的大部分代码,有想要这个小项目的可以联系我。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值