編輯:關於 Android 程式設計

美團的下拉刷新包含三個狀態。第一是「下拉刷新」(pull to refresh),此時一個綠色橢圓會隨著下拉距離動態縮放;第二是「放開刷新」(release to refresh),是一段幀動畫,效果為小人從躺臥到起身站立;第三是「刷新中」(refreshing),同樣是幀動畫,呈現搖頭的姿態。
其中第二、三個狀態實作起來並不難,就是兩段幀動畫;第一個狀態則可以透過自定義 View 來實現。
思路是這樣的:為橢圓形賦予一個從 0 變到 1 的進度值,再據此對橢圓進行縮放。為了便於調試,我先用一個 SeekBar 來模擬下拉距離的進度。

從美團 APK 解壓後,我們可以取得這張橢圓圖片:

關鍵在於:由於第二、三階段的娃娃圖片尺寸一致,而第一階段的橢圓尺寸與之不同,因此我們需要以娃娃圖片作為基準,來決定橢圓圖片的寬高,確保三個階段 View 的尺寸保持一致。
public class MeiTuanRefreshFirstStepView extends View {
private Bitmap initialBitmap;
private int measuredWidth;
private int measuredHeight;
private Bitmap endBitmap;
private float mCurrentProgress;
private Bitmap scaledBitmap;
public MeiTuanRefreshFirstStepView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
public MeiTuanRefreshFirstStepView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public MeiTuanRefreshFirstStepView(Context context) {
super(context);
init(context);
}
private void init(Context context) {
// 橢圓形圖片
initialBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pull_image);
// 以第二階段娃娃圖片的尺寸為基準,確保各階段 View 尺寸一致
endBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pull_end_image_frame_05);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// 依娃娃圖片的寬高比例計算高度
setMeasuredDimension(measureWidth(widthMeasureSpec),
measureWidth(widthMeasureSpec) * endBitmap.getHeight() / endBitmap.getWidth());
}
private int measureWidth(int widMeasureSpec) {
int result = 0;
int size = MeasureSpec.getSize(widMeasureSpec);
int mode = MeasureSpec.getMode(widMeasureSpec);
if (mode == MeasureSpec.EXACTLY) {
result = size;
} else {
result = endBitmap.getWidth();
if (mode == MeasureSpec.AT_MOST) {
result = Math.min(result, size);
}
}
return result;
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
measuredWidth = getMeasuredWidth();
measuredHeight = getMeasuredHeight();
// 依娃娃圖片等比例縮放橢圓圖片
scaledBitmap = Bitmap.createScaledBitmap(initialBitmap, measuredWidth,
measuredWidth * initialBitmap.getHeight() / initialBitmap.getWidth(), true);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 對畫布縮放以實現橢圓的縮放效果
canvas.scale(mCurrentProgress, mCurrentProgress, measuredWidth / 2, measuredHeight / 2);
canvas.drawBitmap(scaledBitmap, 0, measuredHeight / 4, null);
}
/** 設定縮放比例,0 為最小、1 為最大 */
public void setCurrentProgress(float currentProgress) {
mCurrentProgress = currentProgress;
}
}
接著在 Activity 中,用 SeekBar 驅動進度變化:
public class MyActivity extends Activity {
private MeiTuanRefreshFirstStepView mFirstView;
private SeekBar mSeekBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
mSeekBar = (SeekBar) findViewById(R.id.seekbar);
mFirstView = (MeiTuanRefreshFirstStepView) findViewById(R.id.first_view);
mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
// 計算 0~1 的當前進度
float currentProgress = (float) i / (float) seekBar.getMax();
mFirstView.setCurrentProgress(currentProgress);
mFirstView.postInvalidate();
}
@Override public void onStartTrackingTouch(SeekBar seekBar) {}
@Override public void onStopTrackingTouch(SeekBar seekBar) {}
});
}
}
第二個狀態是一段幀動畫。同樣為了保證 View 尺寸統一,我們也自定義 View,只是讓它與第一階段的寬高保持一致即可。
public class MeiTuanRefreshSecondStepView extends View {
private Bitmap endBitmap;
public MeiTuanRefreshSecondStepView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle); init();
}
public MeiTuanRefreshSecondStepView(Context context, AttributeSet attrs) {
super(context, attrs); init();
}
public MeiTuanRefreshSecondStepView(Context context) {
super(context); init();
}
private void init() {
endBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pull_end_image_frame_05);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(measureWidth(widthMeasureSpec),
measureWidth(widthMeasureSpec) * endBitmap.getHeight() / endBitmap.getWidth());
}
private int measureWidth(int widthMeasureSpec) {
int result = 0;
int size = MeasureSpec.getSize(widthMeasureSpec);
int mode = MeasureSpec.getMode(widthMeasureSpec);
if (mode == MeasureSpec.EXACTLY) {
result = size;
} else {
result = endBitmap.getWidth();
if (mode == MeasureSpec.AT_MOST) {
result = Math.min(result, size);
}
}
return result;
}
}
用 XML 定義這組幀動畫:
幀動畫的啟動與停止:
mSecondView = (MeiTuanRefreshSecondStepView) headerView.findViewById(R.id.second_view); mSecondView.setBackgroundResource(R.drawable.pull_to_refresh_second_anim); secondAnim = (AnimationDrawable) mSecondView.getBackground(); // 啟動 secondAnim.start(); // 停止 secondAnim.stop();
第三個狀態的原理與第二個相同,透過自定義 View 確保三個階段的尺寸一致,再於 XML 中定義一組搖頭的幀動畫,啟動與停止方式與第二階段完全一致。
最後,我們把三個狀態串接進一個自定義的 ListView。整體流程需要定義幾個狀態:DONE(隱藏)、PULL_TO_REFRESH(下拉刷新)、RELEASE_TO_REFRESH(放開刷新)、REFRESHING(刷新中)。在 onTouchEvent 中,透過記錄起始 Y 座標與當前位移,計算 headerView 的 padding 與第一狀態 View 的進度值;當手指抬起時,再依當前狀態決定平滑收合或滑出並回調刷新介面。整套手勢結束後,別忘了把記錄座標的標記復位,以便下一次手勢正常觸發。
一切就緒後,只需為自定義 ListView 設定 Adapter 與刷新監聽即可。由於刷新完成後需要更新 UI,务必在主線程中呼叫 setOnRefreshComplete(),並配合 Handler 在背景執行緒模擬資料載入後切回主線程刷新。
public class MainActivity extends Activity implements OnMeiTuanRefreshListener {
private MeiTuanListView mListView;
private List mDatas;
private ArrayAdapter mAdapter;
private final static int REFRESH_COMPLETE = 0;
// 刷新完成後需更新 UI,必須在主線程處理
private Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case REFRESH_COMPLETE:
mListView.setOnRefreshComplete();
mAdapter.notifyDataSetChanged();
mListView.setSelection(0);
break;
default: break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (MeiTuanListView) findViewById(R.id.listview);
String[] data = new String[]{"hello world", "hello world", /* ... */};
mDatas = new ArrayList(Arrays.asList(data));
mAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, mDatas);
mListView.setAdapter(mAdapter);
mListView.setOnMeiTuanRefreshListener(this);
}
@Override
public void onRefresh() {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(3000); // 模擬網路請求
mDatas.add(0, "new data");
mHandler.sendEmptyMessage(REFRESH_COMPLETE);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
用 ExpandableListView 打造微信風格的通訊錄界面
微信的通訊錄有個很有意思的設計:頂部「新的朋友、群聊、標籤、公眾號」是固定的一塊,下方好友名單則按字母分組、預設全展開且點標題不會收合。用 ExpandableListV
Android 自訂九宮格手勢密碼完整實作(含源碼下載)
手勢密碼幾乎是每一支 Android 手機的標配功能,它用九個圓點串連出使用者記憶中的圖形,比數字密碼更直覺、也更有安全感。本文延續先前的自訂 UI 系列,帶大家從零打造
Android 影像處理實作:用 Options Menu 完成懷舊、浮雕、模糊、光照與素描效果
「隨手拍」這個專案想做成類似美圖秀秀那樣、底部一排效果按鈕的樣式。在動手做自訂 UI 之前,先用系統原生的 Options Menu 把幾種基本的影像處理效果跑起來,是個
徹底搞懂 BaseAdapter 的 getView:ListView 為什麼只創建一屏 ItemView
先說結論:ListView 從不為每一條數據都造一個 View ListView 是 Android 開發裡出鏡率最高的控件之一,而與它配套的資料管理者就是 Adapte