編輯:關於 Android 開發

下拉刷新的整體實現原理,我在上一篇《仿美團下拉刷新》裡已經講得很細,這篇專注於表盤指針的動畫。
汽車之家的下拉刷新可以拆成兩個狀態:

從 APK 裡扒出的兩張素材圖:

思路非常巧妙:先把表盤畫在畫布上,接著旋轉整個畫布,再把指針畫上去。我們眼睛看到的「指針在轉」,本質上是畫布在轉。
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 先畫表盤
canvas.drawBitmap(finalBackGroundBitmap, 0, 0, null);
// 旋轉畫布
canvas.rotate(mCurrentProgress * 2.7f, x / 2, y / 2);
// 在旋轉後的畫布上畫指針
canvas.drawBitmap(finalPointerBitmap, 0, 0, null);
}
自定義 View 的完整程式碼:
/**
* Created by zhangqi on 15/10/17.
*/
public class AutoHome extends View {
private Bitmap backGroundBitmap;
public Bitmap pointerBitmap;
private int x;
private int y;
private Bitmap finalBackGroundBitmap;
private Bitmap finalPointerBitmap;
private float mCurrentProgress;
public AutoHome(Context context) {
super(context);
init(context);
}
public AutoHome(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public AutoHome(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
backGroundBitmap = BitmapFactory.decodeResource(
context.getResources(), R.drawable.load_icon_dial2x);
pointerBitmap = BitmapFactory.decodeResource(
context.getResources(), R.drawable.load_icon_pointer2x);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(measureWidth(widthMeasureSpec), measureWidth(heightMeasureSpec));
x = getMeasuredWidth();
y = getMeasuredHeight();
finalBackGroundBitmap = Bitmap.createScaledBitmap(backGroundBitmap, x, y, false);
finalPointerBitmap = Bitmap.createScaledBitmap(pointerBitmap, x, y, false);
}
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 = backGroundBitmap.getWidth();
if (mode == MeasureSpec.AT_MOST) {
result = Math.min(result, size);
}
}
return result;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(finalBackGroundBitmap, 0, 0, null);
canvas.rotate(mCurrentProgress * 2.7f, x / 2, y / 2);
canvas.drawBitmap(finalPointerBitmap, 0, 0, null);
}
public void setCurrentProgress(float progress) {
mCurrentProgress = progress * 100;
}
}
在 Activity 裡用 SeekBar 模擬進度值,驗證效果:
public class MainActivity extends AppCompatActivity {
private SeekBar mSeekBar;
private AutoHome mAutoHome;
private float mCurrentProgress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSeekBar = (SeekBar) findViewById(R.id.seekbar);
mAutoHome = (AutoHome) findViewById(R.id.autohome);
mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
mCurrentProgress = (float) seekBar.getProgress() / (float) seekBar.getMax();
mAutoHome.setCurrentProgress(mCurrentProgress);
mAutoHome.invalidate();
}
@Override public void onStartTrackingTouch(SeekBar seekBar) { }
@Override public void onStopTrackingTouch(SeekBar seekBar) { }
});
}
}
第二個狀態是指針自己做一段旋轉動畫。做法是把指針拆成獨立的 View,表盤當作背景圖,然後對指針 View 執行 rotate 動畫即可。
/**
* Created by zhangqi on 15/10/27.
*/
public class PointerView extends View {
private int x;
private int y;
private Bitmap finalPointerBitmap;
private Bitmap pointerBitmap;
public PointerView(Context context) {
super(context);
init();
}
public PointerView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public PointerView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
pointerBitmap = BitmapFactory.decodeResource(
getResources(), R.drawable.load_icon_pointer2x);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(measureWidth(widthMeasureSpec), measureWidth(heightMeasureSpec));
x = getMeasuredWidth();
y = getMeasuredHeight();
finalPointerBitmap = Bitmap.createScaledBitmap(pointerBitmap, x, y, false);
}
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 = pointerBitmap.getWidth();
if (mode == MeasureSpec.AT_MOST) {
result = Math.min(result, size);
}
}
return result;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 讓指針的初始位置停在 270 度
canvas.rotate(270, x / 2, y / 2);
canvas.drawBitmap(finalPointerBitmap, 0, 0, null);
}
}
布局中把表盤圖片當作背景,指針 View 疊在上面:
<FrameLayout
android:id="@+id/anim_container"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_margin="15dp"
android:visibility="gone">
<com.zhangqi.autohomerefreshlistview.PointerView
android:id="@+id/anim_pointer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/load_icon_dial2x" />
</com.zhangqi.autohomerefreshlistview.PointerView>
</FrameLayout>
接著定義動畫資源,往復擺動營造「正在讀取」的感覺:
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="https://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromDegrees="0"
android:toDegrees="-150"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:repeatMode="reverse" />
mAutoHomeAnim = (PointerView) headerView.findViewById(R.id.anim_pointer);
animation = AnimationUtils.loadAnimation(context, R.anim.pointer_rotate);
// 執行動畫
mAutoHomeAnim.startAnimation(animation);
由於下拉刷新的核心邏輯與美團那篇一致,這裡只列狀態切換的差異部分——關鍵是兩個 View 的互斥顯示與動畫的清理:
private void changeHeaderByState(int state) {
switch (state) {
case DONE:
headerView.setPadding(0, -headerViewHeight, 0, 0);
// 顯示第一階段的表盤
mAutoHome.setVisibility(View.VISIBLE);
// 停止並隱藏第二階段的動畫
mAutoHomeAnim.clearAnimation();
mAnimContainer.setVisibility(View.GONE);
break;
case RELEASE_TO_REFRESH:
tv_pull_to_refresh.setText("放開刷新");
break;
case PULL_TO_REFRESH:
tv_pull_to_refresh.setText("下拉刷新");
mAutoHome.setVisibility(View.VISIBLE);
mAutoHomeAnim.clearAnimation();
mAnimContainer.setVisibility(View.GONE);
break;
case REFRESHING:
tv_pull_to_refresh.setText("正在刷新");
// 隱藏表盤,啟動指針動畫
mAutoHome.setVisibility(View.GONE);
mAnimContainer.setVisibility(View.VISIBLE);
mAutoHomeAnim.clearAnimation();
mAutoHomeAnim.startAnimation(animation);
break;
default:
break;
}
}
整個效果的關鍵其實就兩個技巧:用「旋轉畫布」代替「旋轉圖片」,以及用「兩個互斥 View + 狀態機」區分跟手階段與動畫階段。掌握這兩點,任何「表盤 / 儀錶」風格的下拉刷新都能如法炮製。
神馬視頻 Android 專案源碼概覽
神馬視頻是一個 TVLauncher 類專案,整合直播、回看、點播與設定等功能,適合研究電視盒或智慧電視介面的實作方式。導入專案時若遇到錯誤,可先檢查海思相關外部函式庫是
設計模式實戰:用遊戲角色捏臉的例子徹底搞懂裝飾者模式
先不解釋定義,先看痛點 概念性的定義往往抽象到讓人抓不到重點,也看不出「為什麼需要它」。每一個設計模式其實都是前輩在實戰中撞到牆之後的產物。所以這篇我們直接走一遍那面牆。
使用 AIDL 呼叫遠端服務設定系統時間
需求背景 某些裝置或專案需要由程式設定系統日期與時間,但一般應用沒有權限直接修改系統時間。可行做法是建立一個使用系統簽名的服務,讓具備權限的應用透過 AIDL 呼叫。
Android API 指南:深入 App Widgets 應用小工具
Android API 指南:深入 App Widgets 應用小工具 App Widget 是可以嵌入到其他應用(如主螢幕)中、並接受定期更新的微型應用程式。使用者介面