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

Android 自定義控件實作:星圖式飛入飛出動畫 StellarMap

編輯:關於 Android 程式設計

前言

在 Android 開發裡,想要讓界面變得與眾不同,往往需要一些別出心裁的自定義控件。本文要介紹的 StellarMap,就是一個能實現「飛入飛出」效果的星圖式控件。它把一批數據以隨機散佈的方式鋪滿整個屏幕,並在切換分組時搭配放大、縮小的過渡動畫,非常適合用於標籤雲、關鍵詞推薦這類場景。

先來看看最終的運行效果:

基本用法

整體調用方式相當直觀:先設置數據適配器,再指定默認選中的分組,最後配置屏幕的分割規則即可。

StellarMap stellarMap = (StellarMap) findViewById(R.id.stellar);
// 設置數據
RecommendAdapter adapter = new RecommendAdapter();
stellarMap.setAdapter(adapter);
// 首頁選中
stellarMap.setGroup(0, true);
// 拆分屏幕
stellarMap.setRegularity(15, 20);

與之配套的 Adapter 需要實現分組、計數、取視圖以及切換動畫等幾個關鍵方法:

class RecommendAdapter implements Adapter {
    /** 默認組數 */
    public static final int PAGESIZE = 15;

    @Override
    public int getGroupCount() {
        int groupCount = data.size() / PAGESIZE;
        if (data.size() % PAGESIZE != 0) {
            return groupCount + 1;
        }
        return groupCount;
    }

    @Override
    public int getCount(int group) {
        if (data.size() % PAGESIZE != 0) {
            if (group == getGroupCount() - 1) {
                return data.size() % PAGESIZE;
            }
        }
        return PAGESIZE;
    }

    @Override
    public View getView(int group, int position, View convertView) {
        TextView tv = new TextView(MainActivity.this);
        int index = group * PAGESIZE + position;
        tv.setText(data.get(index));
        // 隨機大小 14-17
        Random random = new Random();
        tv.setTextSize(random.nextInt(4) + 14);
        // 隨機顏色
        int argb = Color.argb(255, random.nextInt(190) + 30, random.nextInt(190) + 30, random.nextInt(190) + 30);
        tv.setTextColor(argb);
        return tv;
    }

    @Override
    public int getNextGroupOnPan(int group, float degree) {
        if (group == getGroupCount() - 1) { group = -1; }
        return group + 1;
    }

    @Override
    public int getNextGroupOnZoom(int group, boolean isZoomIn) {
        if (group == getGroupCount() - 1) { group = -1; }
        return group + 1;
    }
}

逐個說明一下這幾個回調:getGroupCount 返回總共分成多少組;getCount 返回每一組包含多少個元素;getView 負責生成具體的列表項;getNextGroupOnPan 返回下一次需要播放放大動畫的分組;getNextGroupOnZoom 則返回下一次需要播放縮小動畫的分組。

StellarMap 的核心實現

StellarMap 繼承自 FrameLayout,先來看看自定義 View 必須實現的三個構造方法:

public StellarMap(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}
public StellarMap(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}
public StellarMap(Context context) {
    super(context);
    init();
}

初始化方法中,我們維護了分組數量、隱藏組與顯示組的角標,並創建了兩個 RandomLayout(繼承自 ViewGroup,用於把子 View 隨機擺放),隨後掛上手勢監聽、觸摸監聽以及四個過渡動畫:

private void init() {
    mGroupCount = 0;
    mHidenGroupIndex = -1;
    mShownGroupIndex = -1;
    mHidenGroup = new RandomLayout(getContext());
    mShownGroup = new RandomLayout(getContext());
    addView(mHidenGroup, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    mHidenGroup.setVisibility(View.GONE);
    addView(mShownGroup, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    mGestureDetector = new GestureDetector(this);
    setOnTouchListener(this);
    // 設置動畫
    mZoomInNearAnim = AnimationUtil.createZoomInNearAnim();
    mZoomInNearAnim.setAnimationListener(this);
    mZoomInAwayAnim = AnimationUtil.createZoomInAwayAnim();
    mZoomInAwayAnim.setAnimationListener(this);
    mZoomOutNearAnim = AnimationUtil.createZoomOutNearAnim();
    mZoomOutNearAnim.setAnimationListener(this);
    mZoomOutAwayAnim = AnimationUtil.createZoomOutAwayAnim();
    mZoomOutAwayAnim.setAnimationListener(this);
}

設置 Adapter 時,會先記錄分組數量,並為兩個子 Group 分別構建各自的適配器:

public void setAdapter(Adapter adapter) {
    mAdapter = adapter;
    mGroupCount = mAdapter.getGroupCount();
    if (mGroupCount > 0) {
        mShownGroupIndex = 0;
    }
    setChildAdapter();
}

private void setChildAdapter() {
    if (null == mAdapter) { return; }
    mHidenGroupAdapter = new RandomLayout.Adapter() {
        @Override public View getView(int position, View convertView) {
            return mAdapter.getView(mHidenGroupIndex, position, convertView);
        }
        @Override public int getCount() {
            return mAdapter.getCount(mHidenGroupIndex);
        }
    };
    mHidenGroup.setAdapter(mHidenGroupAdapter);
    mShownGroupAdapter = new RandomLayout.Adapter() {
        @Override public View getView(int position, View convertView) {
            return mAdapter.getView(mShownGroupIndex, position, convertView);
        }
        @Override public int getCount() {
            return mAdapter.getCount(mShownGroupIndex);
        }
    };
    mShownGroup.setAdapter(mShownGroupAdapter);
}

整個控件的靈魂在於 switchGroup 方法:它通過交換顯示組與隱藏組的角標及適配器,完成顯示與隱藏的切換,並啟動過渡動畫。

public void setGroup(int groupIndex, boolean playAnimation) {
    switchGroup(groupIndex, playAnimation, mZoomInNearAnim, mZoomInAwayAnim);
}

private void switchGroup(int newGroupIndex, boolean playAnimation, Animation inAnim, Animation outAnim) {
    if (newGroupIndex < 0 || newGroupIndex >= mGroupCount) { return; }
    mHidenGroupIndex = mShownGroupIndex;
    mShownGroupIndex = newGroupIndex;
    // 交換兩個 Group
    RandomLayout temp = mShownGroup;
    mShownGroup = mHidenGroup;
    mShownGroup.setAdapter(mShownGroupAdapter);
    mHidenGroup = temp;
    mHidenGroup.setAdapter(mHidenGroupAdapter);
    mShownGroup.refresh();
    mShownGroup.setVisibility(View.VISIBLE);
    if (playAnimation) {
        if (mShownGroup.hasLayouted()) {
            mShownGroup.startAnimation(inAnim);
        }
        mHidenGroup.startAnimation(outAnim);
    } else {
        mHidenGroup.setVisibility(View.GONE);
    }
}

setRegularity 用來設置屏幕在橫縱方向上的分割規則,其核心在於 onLayout 裡的區域分配邏輯:先將屏幕劃分成若干網格區域,再為每個子 View 隨機挑選一個尚未超載的區域擺放,同時判斷是否與其他 View 重疊,重疊則更換區域。由於代碼較長,關鍵處已補充註釋,這裡不再逐行贅述。

手勢交互

最後,控件借助手勢判斷用戶的滑動方向,從而在放大與縮小動畫之間切換:

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    int centerX = getMeasuredWidth() / 2;
    int centerY = getMeasuredWidth() / 2;
    int x1 = (int) e1.getX() - centerX;
    int y1 = (int) e1.getY() - centerY;
    int x2 = (int) e2.getX() - centerX;
    int y2 = (int) e2.getY() - centerY;
    if ((x1 * x1 + y1 * y1) > (x2 * x2 + y2 * y2)) {
        zoomOut();
    } else {
        zoomIn();
    }
    return true;
}

不論放大還是縮小,最終都會回到 switchGroup 方法,完成分組的切換與動畫銜接。至此,一個兼具隨機佈局與炫酷動畫的星圖控件就完成了。

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