Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 小項目:Walker 歡迎頁與注冊引導頁(四)

編輯:關於 Android 程式設計

本節要做的是 App 的「第一印象」:一個帶淡入動畫的歡迎頁,接一段可左右滑動的引導頁,最後由引導頁末屏的 Go 按鈕跳轉到登錄界面。

目標效果:

運行後先出現圖一的 walker 歡迎界面,畫面由模糊漸漸變清晰;停留數秒後自動跳轉到圖二的引導界面,底部有四個小圓點指示當前頁,第四屏右下角有一個 Go 按鈕,點擊進入登錄頁。

一、建立 Activity

新建兩個 Activity:WelcomeActivity 與 GiudeActivity(源碼裡拼寫如此,保持一致以免引用出錯),各自配一份 xml 布局。

activity_welcome.xml

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:id="@+id/layoutwelcome"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/welcome_bg"
    tools:context=".WelcomeActivity">
</RelativeLayout>

二、四屏引導頁布局

新建 view1_of_pager.xml ~ view4_of_pager.xml。前三屏結構相同,只是背景圖不同;第四屏右下角多一個文字為 Go 的 TextView。

view1_of_pager.xml

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/guide_1"
    android:orientation="vertical">
</LinearLayout>

view4_of_pager.xml

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/guide_4">

    <TextView
        android:id="@+id/tvGo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="16dp"
        android:layout_marginRight="19dp"
        android:background="@drawable/start_select"
        android:gravity="center"
        android:text="Go"
        android:textColor="#ffffff"
        android:textSize="30sp"/>
</RelativeLayout>

三、Go 按鈕的點擊樣式

用 selector 加兩個 shape 就能做出按下反饋。

start_select.xml

<selector xmlns:android="https://schemas.android.com/apk/res/android">
    <item android:state_pressed="false" android:drawable="@drawable/start_before"/>
    <item android:state_pressed="true"  android:drawable="@drawable/start_after"/>
</selector>

start_before.xml(未按下)

<shape xmlns:android="https://schemas.android.com/apk/res/android" android:shape="oval">
    <solid android:color="#2362a7"/>
    <size android:width="80dp" android:height="80dp"/>
</shape>

start_after.xml(按下)

<shape xmlns:android="https://schemas.android.com/apk/res/android" android:shape="oval">
    <solid android:color="#333333"/>
    <size android:width="80dp" android:height="80dp"/>
</shape>

四、底部指示圓點

同樣用兩個 oval shape 區分選中與未選中狀態。

bg_point_select.xml(選中)

<shape xmlns:android="https://schemas.android.com/apk/res/android" android:shape="oval">
    <solid android:color="#427ab7"/>
    <size android:width="8dp" android:height="8dp"/>
</shape>

bg_point.xml(未選中)

<shape xmlns:android="https://schemas.android.com/apk/res/android" android:shape="oval">
    <solid android:color="@android:color/black"/>
    <size android:width="8dp" android:height="8dp"/>
</shape>

activity_giude.xml 放置四個圓點:

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".GiudeActivity">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="40dp"
        android:orientation="horizontal">

        <ImageView android:id="@+id/ivGuide_dot1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bg_point_selected"/>
        <ImageView android:id="@+id/ivGuide_dot2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bg_point"/>
        <ImageView android:id="@+id/ivGuide_dot3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bg_point"/>
        <ImageView android:id="@+id/ivGuide_dot4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bg_point"/>
    </LinearLayout>
</RelativeLayout>

五、編寫適配器

GuideAdapter.java

public class GuideAdapter extends PagerAdapter {
    private List<View> views;

    public GuideAdapter(List<View> views) {
        this.views = views;
    }

    @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));
        return views.get(position);
    }

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

六、歡迎頁動畫

WelcomeActivity.java

public class WelcomeActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        RelativeLayout layoutWelcome = (RelativeLayout) findViewById(R.id.layoutwelcome);

        // 透明度從 0.1 渐变到 1.0
        AlphaAnimation alphaAnimation = new AlphaAnimation(0.1f, 1.0f);
        alphaAnimation.setDuration(3000);
        layoutWelcome.startAnimation(alphaAnimation);

        alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override public void onAnimationStart(Animation animation) { }
            @Override public void onAnimationRepeat(Animation animation) { }

            @Override
            public void onAnimationEnd(Animation animation) {
                startActivity(new Intent(WelcomeActivity.this, GiudeActivity.class));
                finish();   // 銷毀本頁,避免按返回鍵又回到歡迎頁
            }
        });
    }
}

這裡的關鍵是監聽 onAnimationEnd 並在其中 finish 掉自己,否則用戶從登錄頁一路返回,會又看到歡迎動畫。

七、引導頁翻動與跳轉

GiudeActivity.java(核心片段)

public class GiudeActivity extends Activity {
    private List<View> guideViews;
    private ViewPager vpGuide;
    private int[] guide_dots = {R.id.ivGuide_dot1, R.id.ivGuide_dot2, R.id.ivGuide_dot3, R.id.ivGuide_dot4};
    private ImageView[] dots;
    private TextView tvGo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_giude);
        initGuideViews();
        initDots();
        setListeners();
    }

    private void initGuideViews() {
        vpGuide = (ViewPager) findViewById(R.id.vpGuide);
        guideViews = new ArrayList<>();
        LayoutInflater inflater = LayoutInflater.from(this);
        guideViews.add(inflater.inflate(R.layout.view1_of_pager, null));
        guideViews.add(inflater.inflate(R.layout.view2_of_pager, null));
        guideViews.add(inflater.inflate(R.layout.view3_of_pager, null));

        // 第四屏需要拿到 tvGo,所以单独 inflate 出來
        View view4 = inflater.inflate(R.layout.view4_of_pager, null);
        guideViews.add(view4);
        tvGo = (TextView) view4.findViewById(R.id.tvGo);

        vpGuide.setAdapter(new GuideAdapter(guideViews));
    }

    private void initDots() {
        dots = new ImageView[4];
        for (int i = 0; i < dots.length; i++) {
            dots[i] = (ImageView) findViewById(guide_dots[i]);
        }
    }

    private void setListeners() {
        vpGuide.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float offset, int offsetPixels) { }

            @Override
            public void onPageSelected(int position) {
                // 切換圓點選中態
                for (int i = 0; i < dots.length; i++) {
                    dots[i].setBackgroundResource(
                        i == position ? R.drawable.bg_point_selected : R.drawable.bg_point);
                }
            }

            @Override public void onPageScrollStateChanged(int state) { }
        });

        tvGo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(GiudeActivity.this, LoginActivity.class));
                finish();
            }
        });
    }
}

記得在清單里把 WelcomeActivity 配為 LAUNCHER 入口,並給兩個 Activity 都加上合適的 theme。

八、小結

這一節涉及的技術點都不難,但組合起來剛好覆蓋了幾個高頻場景:AlphaAnimation 做啟動過渡、ViewPager + PagerAdapter 拼引導流、shape 與 selector 做無圖形狀與點擊反饋、以及通過 OnPageChangeListener 同步指示器狀態。把這套模板沉澱下來,之後任何 App 的引導頁都能快速套用。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved