一個(gè)簡(jiǎn)單啟動(dòng)畫面和引導(dǎo)頁

常常會(huì)有一些APP開發(fā)需要用到啟動(dòng)畫面或者引導(dǎo)頁面,啟動(dòng)頁面正常都是一些簡(jiǎn)單的Logo或者圖片之類,主要是能實(shí)現(xiàn)一些后臺(tái)數(shù)據(jù)的加載。而引導(dǎo)頁面可以通過左右滑動(dòng)方式傳達(dá)信息(APP介紹,新功能,插入廣告等)給用戶。

簡(jiǎn)單實(shí)現(xiàn)啟動(dòng)畫面

兩種方法的實(shí)現(xiàn):

一 建立一個(gè)activity,展示啟動(dòng)畫面,然后再跳轉(zhuǎn)到主Activity。
public class WelcomeActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //設(shè)置無標(biāo)題
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //設(shè)置全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_welcome);
        new Handler().postDelayed(new Runnable() {//一秒后跳轉(zhuǎn)到主頁面
            public void run() {
                // 跳轉(zhuǎn)至 MainActivity
                Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
                startActivity(intent);
                WelcomeActivity.this.finish();//如果沒有finish掉,按回退鍵就會(huì)回到歡迎界面不合理。
            }
        }, 1000);
    }
}

布局layout.activity_welcome.xml:只添加一張背景圖片

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@mipmap/timg">
</LinearLayout>
二 在同一個(gè)activity展示啟動(dòng)畫面,后進(jìn)行隱藏。
public class Welcome2Activity extends Activity implements Animation.AnimationListener {
    LinearLayout welcomelinearLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome2);
        AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);//2秒漸變
        animation.setDuration(2000);
        welcomelinearLayout = (LinearLayout) findViewById(R.id.welcome);
        welcomelinearLayout.setAnimation(animation);
        animation.setAnimationListener(this);

    }

    @Override
    public void onAnimationStart(Animation animation) {
        welcomelinearLayout.setVisibility(View.VISIBLE);//開始時(shí)進(jìn)行顯示
    }

    @Override
    public void onAnimationEnd(Animation animation) {
        welcomelinearLayout.setVisibility(View.GONE);  //結(jié)束時(shí)進(jìn)行隱藏起來
    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }
}

布局layout.activity_welcome2.xml

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

    <LinearLayout
        android:id="@+id/welcome"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@mipmap/timg"
        android:orientation="vertical"></LinearLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="Welcome To MainActivity!"
        android:textSize="24sp" />
</LinearLayout>

簡(jiǎn)單實(shí)現(xiàn)引導(dǎo)頁

這里是用ViewPager實(shí)現(xiàn)引導(dǎo)頁。

介紹:引導(dǎo)頁有四個(gè)可以左右滑動(dòng)的頁面,最后一個(gè)頁面有一個(gè)按鈕點(diǎn)擊可進(jìn)入主頁面。

分別創(chuàng)建4個(gè)子布局welcome_item1.xml,welcome_item2.xml,welcome_item3.xml都是一樣布局background設(shè)置不一樣圖片而已

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/timg"
    android:orientation="vertical">

</LinearLayout>

welcome_item4.xml也就多一個(gè)按鈕

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/timg4"
    android:orientation="vertical">

    <Button
        android:id="@+id/Bt_start"
        android:layout_width="200dp"
        android:layout_height="80dp"
        android:layout_centerInParent="true"
        android:layout_margin="10dp"
        android:text="進(jìn)入主頁面"
        android:textSize="20sp" />
</RelativeLayout>

接下來我們來看看主布局activity_welcome4.xml,主頁面分兩個(gè)模塊一個(gè)是ViewPager還一個(gè)是要實(shí)現(xiàn)底部的小圓點(diǎn)功能:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v4.view.ViewPager
        android:id="@+id/welcome_vp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v4.view.ViewPager>

    <LinearLayout
        android:id="@+id/spot_linearLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="24dp"
        android:orientation="horizontal"></LinearLayout>
</RelativeLayout>

接下來編寫小圓點(diǎn)的selector,分為選中和沒選中兩種狀態(tài)。

<?xml version="1.0" encoding="UTF-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="true" android:drawable="@drawable/dark_dot" />
    <item android:state_enabled="false" android:drawable="@drawable/white_dot" />
</selector>

dark_dot.xml

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

white_dot

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

接下來就是activity部分,先上全碼

public class Welcome4Activity extends Activity implements ViewPager.OnPageChangeListener {
    private ViewPager welcome_vp;
    private ArrayList<View> ViewList;
    private Button Bt_start;
    private View view1, view2, view3, view4;
    //底部小點(diǎn)圖片
    private ImageView[] dots;
    //引導(dǎo)頁個(gè)數(shù)
    private int Number=4;
    //記錄當(dāng)前選中位置
    private int currentIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //設(shè)置無標(biāo)題
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //設(shè)置全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_welcome4);
        initData();
        //初始化底部小點(diǎn)
        initDots();
        welcome_vp = (ViewPager) findViewById(R.id.welcome_vp);
        WelcomeVPAdapter adapter = new WelcomeVPAdapter();
        welcome_vp.setAdapter(adapter);
        welcome_vp.addOnPageChangeListener(this);
        Bt_start = (Button) view4.findViewById(R.id.Bt_start);
        Bt_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goToActivity(MainActivity.class.getName());
                finish();
            }
        });

    }

    //初始化底部小點(diǎn)
    private void initDots() {
        LinearLayout spot_linearLayout = (LinearLayout) findViewById(R.id.spot_linearLayout);
        //根據(jù)個(gè)數(shù)添加對(duì)應(yīng)小點(diǎn)的ImageView
        for (int i = 0; i < Number; i++) {
            ImageView iv = new ImageView(this);
            iv.setImageResource(R.drawable.dot);
            iv.setPadding(30, 30, 30, 30);
            spot_linearLayout.addView(iv);
        }
        dots = new ImageView[Number];
        //循環(huán)取得小點(diǎn)圖片
        for (int i = 0; i < Number; i++) {
            dots[i] = (ImageView) spot_linearLayout.getChildAt(i);
            dots[i].setEnabled(true);//全部設(shè)置顏色為不選中狀態(tài)
        }
        currentIndex = 0;
        dots[currentIndex].setEnabled(false);//默認(rèn)第一個(gè)選中狀態(tài)
    }

    private void setCurDot(int positon) {
        if (positon < 0 || positon > Number - 1 || currentIndex == positon) {
            return;
        }
        dots[positon].setEnabled(false);
        dots[currentIndex].setEnabled(true);
        currentIndex = positon;
    }

    //初始化數(shù)據(jù)
    private void initData() {
        ViewList = new ArrayList<View>();
        view1 = LayoutInflater.from(this).inflate(R.layout.welcome_item1, null);
        view2 = LayoutInflater.from(this).inflate(R.layout.welcome_item2, null);
        view3 = LayoutInflater.from(this).inflate(R.layout.welcome_item3, null);
        view4 = LayoutInflater.from(this).inflate(R.layout.welcome_item4, null);
        ViewList.add(view1);
        ViewList.add(view2);
        ViewList.add(view3);
        ViewList.add(view4);
    }

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

    }

    @Override
    public void onPageSelected(int position) {
        setCurDot(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


    class WelcomeVPAdapter extends PagerAdapter {

        //返回可以滑動(dòng)的VIew的個(gè)數(shù)
        @Override
        public int getCount() {
            return ViewList.size();
        }

        //滑動(dòng)切換的時(shí)候銷毀當(dāng)前的組件
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(ViewList.get(position));
        }

        //將當(dāng)前視圖添加到container中并返回當(dāng)前View視圖
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(ViewList.get(position));
            return ViewList.get(position);
        }

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

    protected void goToActivity(String className) {
        Intent intent = new Intent();
        intent.setClassName(getBaseContext(), className);
        startActivity(intent);
    }

}

首先肯定是初始化數(shù)據(jù)咯
這里有三個(gè)個(gè)模塊的初始化(點(diǎn)擊按鈕的就不說了)

  • view的初始化
 //初始化數(shù)據(jù)
    private void initData() {
        ViewList = new ArrayList<View>();
        view1 = LayoutInflater.from(this).inflate(R.layout.welcome_item1, null);
        view2 = LayoutInflater.from(this).inflate(R.layout.welcome_item2, null);
        view3 = LayoutInflater.from(this).inflate(R.layout.welcome_item3, null);
        view4 = LayoutInflater.from(this).inflate(R.layout.welcome_item4, null);
        ViewList.add(view1);
        ViewList.add(view2);
        ViewList.add(view3);
        ViewList.add(view4);
    }
  • 小圓點(diǎn)的初始化
  //初始化底部小點(diǎn)
    private void initDots() {
        LinearLayout spot_linearLayout = (LinearLayout) findViewById(R.id.spot_linearLayout);
        //動(dòng)態(tài)添加小圓點(diǎn)的ImageView
        for (int i = 0; i < Number; i++) {
            ImageView iv = new ImageView(this);
            iv.setImageResource(R.drawable.dot);
            iv.setPadding(30, 30, 30, 30);
            spot_linearLayout.addView(iv);
        }
        dots = new ImageView[Number];
        //循環(huán)取得小點(diǎn)圖片
        for (int i = 0; i < Number; i++) {
            dots[i] = (ImageView) spot_linearLayout.getChildAt(i);
            dots[i].setEnabled(true);//全部設(shè)置顏色為不選中狀態(tài)
        }
        currentIndex = 0;
        dots[currentIndex].setEnabled(false);//默認(rèn)第一個(gè)選中狀態(tài)
    }

我們知道ViewPager類需要一個(gè)PagerAdapter適配器類給它提供數(shù)據(jù)
所以


    class WelcomeVPAdapter extends PagerAdapter {

        //返回可以滑動(dòng)的VIew的個(gè)數(shù)
        @Override
        public int getCount() {
            return ViewList.size();
        }

        //滑動(dòng)切換的時(shí)候銷毀當(dāng)前的組件
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(ViewList.get(position));
        }

        //將當(dāng)前視圖添加到container中并返回當(dāng)前View視圖
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(ViewList.get(position));
            return ViewList.get(position);
        }

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

然后我們還需要實(shí)現(xiàn)了OnPageChangeListener接口,對(duì)ViewPager滑動(dòng)事件作出相應(yīng)的反應(yīng)。

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

    }

    @Override
    public void onPageSelected(int position) {
        setCurDot(position);//在這里我們進(jìn)行小點(diǎn)狀態(tài)的修改
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

ok一個(gè)簡(jiǎn)單的引導(dǎo)頁實(shí)現(xiàn)了!

1.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,351評(píng)論 25 708
  • 二零一六年九月三日,我孤身一人來到北京,這個(gè)我曾一直在夢(mèng)中到達(dá)過的城市,我來自寧夏一個(gè)偏遠(yuǎn)的地區(qū),從小老師就...
    韓小帥閱讀 385評(píng)論 0 0
  • 分享些開心的事兒 出門半年的我奇跡般的在村里過女神節(jié),沒錯(cuò)就是村里,鎮(zhèn)上一個(gè)小村。 聚美優(yōu)品的東西也到了,快遞真及...
    最愛顛茄小F閱讀 218評(píng)論 0 0
  • 一、生命周期相關(guān) 與Activity最為密切的就是其生命周期的相關(guān)內(nèi)容了,生命周期圖鎮(zhèn)樓: 雖然看起來比較簡(jiǎn)單,但...
    Damon__________閱讀 349評(píng)論 0 0

友情鏈接更多精彩內(nèi)容