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

用 ImageSwitcher 打造手勢翻頁的引導畫面

編輯:關於 Android 程式設計

本文分享一個以 ImageSwitcher 實作 App 引導頁的完整範例,供大家參考。

效果圖:

佈局程式碼

整體用 FrameLayout 承載:底層是 ImageSwitcher 負責顯示圖片,上層疊一條用來放指示點的 LinearLayout:

<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ImageSwitcher
        android:id="@+id/imageSwitcher"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    </ImageSwitcher>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <LinearLayout
            android:id="@+id/ll_view"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="30dp"
            android:gravity="center_horizontal"
            android:orientation="horizontal">
        </LinearLayout>
    </RelativeLayout>
</FrameLayout>

頁面程式碼

核心邏輯是:實作 ViewFactory 提供 ImageView,再用 onTouch 比對按下與抬起時的 X 座標,判斷使用者是往左還是往右滑,決定要換到下一張還是上一張:

public class ImageSwitcherActivity extends Activity
        implements ViewSwitcher.ViewFactory, View.OnTouchListener {

    private int[] imgIds;              // 圖片 id 數組
    private int currentPosition;       // 當前選中的圖片 id 序號
    private ImageSwitcher mImageSwitcher;
    private float downX;               // 按下點的 X 坐標
    private ImageView[] tips;          // 點點數組
    private LinearLayout linearLayout; // 裝載點點的容器

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_switcher);

        imgIds = new int[]{R.drawable.bg, R.drawable.c2, R.drawable.c3, R.drawable.c4,
                R.drawable.c5, R.drawable.c6, R.drawable.c7, R.drawable.c8, R.drawable.c9};

        mImageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
        mImageSwitcher.setFactory(this);
        mImageSwitcher.setOnTouchListener(this);

        linearLayout = (LinearLayout) findViewById(R.id.ll_view);
        tips = new ImageView[imgIds.length];

        for (int i = 0; i < imgIds.length; i++) {
            ImageView mImageView = new ImageView(this);
            tips[i] = mImageView;
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams.rightMargin = 3;
            layoutParams.leftMargin = 3;
            mImageView.setBackgroundResource(R.drawable.page_indicator_unfocused);
            linearLayout.addView(mImageView, layoutParams);
        }

        // 上一個界面傳過來的位置
        currentPosition = getIntent().getIntExtra("position", 0);
        mImageSwitcher.setImageResource(imgIds[currentPosition]);
        setImageBackground(currentPosition);
    }

    // 設置選中的 tip 的背景
    private void setImageBackground(int selectItems) {
        for (int i = 0; i < tips.length; i++) {
            if (i == selectItems) {
                tips[i].setBackgroundResource(R.drawable.page_indicator_focused);
            } else {
                tips[i].setBackgroundResource(R.drawable.page_indicator_unfocused);
            }
        }
    }

    @Override
    public View makeView() {
        final ImageView i = new ImageView(this);
        i.setBackgroundColor(0xff000000);
        i.setScaleType(ImageView.ScaleType.CENTER_CROP);
        i.setLayoutParams(new ImageSwitcher.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        return i;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = event.getX();
                break;

            case MotionEvent.ACTION_UP:
                float lastX = event.getX();

                // 抬起的 X 坐標大於按下時,顯示上一張
                if (lastX > downX) {
                    if (currentPosition > 0) {
                        mImageSwitcher.setInAnimation(
                                AnimationUtils.loadAnimation(getApplication(), R.anim.left_in));
                        mImageSwitcher.setOutAnimation(
                                AnimationUtils.loadAnimation(getApplication(), R.anim.right_out));
                        currentPosition--;
                        mImageSwitcher.setImageResource(imgIds[currentPosition % imgIds.length]);
                        setImageBackground(currentPosition);
                    } else {
                        Toast.makeText(getApplication(), "已經是第一張",
                                Toast.LENGTH_SHORT).show();
                    }
                }

                // 抬起的 X 坐標小於按下時,顯示下一張
                if (lastX < downX) {
                    if (currentPosition < imgIds.length - 1) {
                        mImageSwitcher.setInAnimation(
                                AnimationUtils.loadAnimation(getApplication(), R.anim.right_in));
                        mImageSwitcher.setOutAnimation(
                                AnimationUtils.loadAnimation(getApplication(), R.anim.lift_out));
                        currentPosition++;
                        mImageSwitcher.setImageResource(imgIds[currentPosition]);
                        setImageBackground(currentPosition);
                    } else {
                        Toast.makeText(getApplication(), "到了最後一張",
                                Toast.LENGTH_SHORT).show();
                    }
                }
                break;
        }
        return true;
    }
}

幾個細節值得留意:makeView() 中把 ImageView 的 scaleType 設為 CENTER_CROP,能讓引導圖在不同解析度下都填滿畫面;每個方向各自準備一組 in/out 動畫檔,翻頁的過場才會自然;而 setImageBackground() 則負責同步更新底部指示點的高亮狀態。

以上就是本文的全部內容,希望對大家的學習有所幫助,也歡迎多多支持本站。

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