android 欢迎页和导航页

1、欢迎页SplashActivity

public class SplashActivity extends AppCompatActivity {
    
    private boolean isFirst = false;
    
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        isFirst();
    }

    private void isFirst(){
        SharedPreferences sharedPre = getPreferences(MODE_PRIVATE);
        isFirst = sharedPre.getBoolean("isFirst", true);
        if (isFirst){
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    goGuide();
                }
            },2000);
            //储存
            SharedPreferences.Editor editor = sharedPre.edit();
            editor.putBoolean("isFirst",false);
            //提交
            editor.commit();
        }else {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    goHome();
                }
            },2000);
        }
    }
    private  void goHome(){
        Intent intent_main=new Intent(this,MainActivity.class);
        startActivity(intent_main);
        finish();
    }
    public void goGuide(){
        Intent intent = new Intent(this, GuideActivity.class);
        startActivity(intent);
        finish();
        return;
    }
}

2、activity_splash页面布局

<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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitCenter"
        android:src="@mipmap/zero"/>
</LinearLayout>

3、导航页GuideActivity

public class GuideActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG="GuideActivity";
    private ViewPager viewPager;
    private ViewPagerAdapter adapter;
    private List<View> views;
    private Button going_main;
    private static final int[] pics = {  R.layout.guid_one,
            R.layout.guid_two, R.layout.guid_three};

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcom);
        initData();
    }

    private void initData() {
        viewPager = (ViewPager) findViewById(R.id.view_pager);
        views = new ArrayList<View>();
        for(int i = 0; i < pics.length; i++){
            View view = LayoutInflater.from(this).inflate(pics[i], null);
            if(i == pics.length - 1){
                going_main = (Button)view.findViewById(R.id.going_main);
                going_main.setTag("enter");
                going_main.setOnClickListener(this);
            }
            views.add(view);
        }
        adapter = new ViewPagerAdapter(this,views);
        viewPager.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.going_main:
                Intent intent=new Intent(GuideActivity.this,MainActivity.class);
                startActivity(intent);
                finish();
                break;
        }
    }
}

4、R.layout.guid_two举例

<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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@mipmap/two"/>
</LinearLayout>

5、  导航页的布局activity_welcom

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

6、AndroidManifest.xml

<application
    android:name=".NettyAppliction"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".SplashActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".GuideActivity"/>
    <activity android:name=".MainActivity"/>
</application>

7、ViewPagerAdapter适配器

public class ViewPagerAdapter extends androidx.viewpager.widget.PagerAdapter {
    private final List<View> views;

    public ViewPagerAdapter(Context context, List view) {
        views = view;
    }

    @Override
    public int getCount() {
        return views.size();
    }

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

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        container.addView(views.get(position),0);
        return views.get(position);
    }

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

注意:引入的图尽量是jpg格式

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值