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

Android 自定義 View:左右滑動選擇出生年份

編輯:關於 Android 程式設計

在一些個人資料頁面中,出生年份的選擇通常採用滑動效果。用戶左右滑動時,中間數字會放大,兩側數字會縮小,最終停留在選中的年份上。這種效果可以通過自定義 View 實現。

實現思路

  1. 自定義屬性,例如字體大小、文字顏色與線條顏色。
  2. 重寫 dispatchTouchEvent 判斷左右滑動。
  3. 在 onTouchEvent 中更新中間與兩側數字大小。
  4. 在 onDraw 中繪製刻度線與年份文字。
  5. 通過接口回調把選中年份返回給 Activity。

自定義屬性

<resources>
    <attr name="titleSize" format="dimension" />
    <attr name="titleColor" format="color" />
    <attr name="lineColor" format="color" />

    <declare-styleable name="MyScrollView">
        <attr name="titleSize" />
        <attr name="titleColor" />
        <attr name="lineColor" />
    </declare-styleable>
</resources>

判斷左右滑動

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    int action = ev.getAction();
    int x = (int) ev.getX();
    int y = (int) ev.getY();

    switch (action) {
        case MotionEvent.ACTION_DOWN:
            xDown = x;
            yDown = y;
            break;
        case MotionEvent.ACTION_MOVE:
            xMove = x;
            yMove = y;
            dx = xMove - xDown;
            int dy = yMove - yDown;

            if (xMove > xDown && Math.abs(dx) > mTouchSlop * 2 && Math.abs(dy) < mTouchSlop) {
                state = 1;
            }

            if (xMove < xDown && Math.abs(dx) > mTouchSlop * 2 && Math.abs(dy) < mTouchSlop) {
                state = 2;
            }
            break;
    }
    return super.dispatchTouchEvent(ev);
}

處理滑動變化

@Override
public boolean onTouchEvent(MotionEvent ev) {
    switch (ev.getAction()) {
        case MotionEvent.ACTION_MOVE:
            if (state == 1) {
                bigTxtSize = bigTxtSize - 1;
                oneSize = oneSize + 1;
                postInvalidate();
            }
            if (state == 2) {
                bigTxtSize = bigTxtSize - 1;
                thirdSize = thirdSize + 1;
                postInvalidate();
            }
            break;
        case MotionEvent.ACTION_UP:
            if (state == 1) {
                size = size - 1;
                listener.OnScroll(size);
            }
            if (state == 2) {
                size = size + 1;
                listener.OnScroll(size);
            }
            state = 0;
            break;
    }
    return true;
}

繪製年份

繪製前可以先在草稿上標注起始點。中間文字使用較大字號,兩側文字使用較小字號,並通過循環繪製多個年份。

lineX = getWidth() / 10;

for (int i = 0; i < 5; i++) {
    if (i == 2) {
        mPaint.setTextSize(bigTxtSize);
        canvas.drawText(bigText, lineX - mBound.width() / 2, getHeight() / 2, mPaint);
    } else {
        mPaint.setTextSize(textSize - 15);
        canvas.drawText(String.valueOf(txtSize), lineX - mTxtBound.width() / 2, getHeight() / 2, mPaint);
    }
    txtSize++;
    lineX += getWidth() / 5;
}

接口回調

public interface OnScrollListener {
    void OnScroll(int size);
}

在 Activity 中實現接口,即可同步更新上方顯示的年份文字。這種自定義 View 可以廣泛應用於日期選擇、城市選擇或任何需要滑動選值的場景。

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