編輯:關於 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 繼承自 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 方法,完成分組的切換與動畫銜接。至此,一個兼具隨機佈局與炫酷動畫的星圖控件就完成了。
微信電腦版誤發訊息怎麼辦?撤回功能操作教學
微信電腦版可撤回誤發訊息。在聊天視窗中找到訊息,按右鍵選擇撤回,對話中會顯示已撤回提示。 此功能可減少打錯字或傳錯檔案的困擾。
移植 u-boot 2012.04.1 到 Mini2440(二):NorFlash 的識別與匹配
上一篇文章裡,我們完成了新建單板、時鐘與 SDRAM 的初始化,串口已經能正常印出啟動資訊,但畫面上還掛著一行錯誤提示。 這一篇就來把它解決掉。 一、從哪裡查起 直接搜
MPAndroidChart 折線圖實戰:資料綁定、樣式設定與常見坑位
從資料到折線圖 本文以 MPAndroidChart 的 LineChart 為例,整理折線圖在實際專案中的資料綁定、樣式設定與常見問題。 LineChartTools
打造 App 首次安裝的新手浮層引導 View
我們第一次安裝某些 App 時,常會看到一個覆蓋在原界面之上的浮層,用來標註關鍵操作、講解整體架構,幫助用戶更快上手。先看兩張效果圖: 這就是我們要實現的效果。梳理一