編輯:關於 Android 程式設計
近期在開發一款 App 時,需要模擬一個電池電量的進度條,根據電量百分比填充對應區域,並讓圖片不斷上下滾動,呈現出手機充電時那種動態效果。於是我通過自定義 View 實現了這個電量進度條——只需替換圖片,就能達到各自想要的視覺效果。
核心思路是循環刷新界面,讓兩張圖片上下滾動,從而營造出持續流動的錯覺。
package com.example.battery;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
public class Battery extends View {
public float currentX = 80;
public float currentY = 80;
private float secondY = 80;
private Paint mPaint = new Paint();
private Context mContext;
private Handler mHandler;
private Bitmap mBmp;
private int speedTime = 20;
private float with = 200;
private float height = 50;
private float percentage = 0.5f;
public Battery(Context context) {
super(context);
this.mContext = context;
}
public Battery(Context context, AttributeSet set) {
super(context, set);
this.mContext = context;
init();
}
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
with = this.getWidth();
height = this.getHeight();
mPaint.setColor(Color.BLUE);
Resources res = mContext.getResources();
BitmapDrawable bmpDraw = (BitmapDrawable) res.getDrawable(R.drawable.loading_pic);
mBmp = bmpDraw.getBitmap();
canvas.clipRect(0, 0, with * percentage, height);
canvas.drawBitmap(mBmp, 0, currentY, mPaint);
canvas.drawBitmap(mBmp, 0, secondY, mPaint);
}
private void init() {
percentage = 0;
mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
currentX++;
currentY++;
if (mBmp != null && currentY > mBmp.getHeight()) {
currentY = -mBmp.getHeight();
}
if (mBmp != null) {
secondY = currentY + mBmp.getHeight();
if (secondY >= mBmp.getHeight()) {
secondY = currentY - mBmp.getHeight();
}
}
percentage = percentage + 0.003f;
if (percentage > 1) {
percentage = 0;
}
// 每次計算後發送消息進入下一次循環並刷新界面
mHandler.sendEmptyMessageDelayed(1, speedTime);
postInvalidate();
break;
}
super.handleMessage(msg);
postInvalidate();
}
};
// 首次循環刷新界面
mHandler.sendEmptyMessageDelayed(1, speedTime);
}
}
package com.example.battery;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:gravity="center" >
<com.example.battery.Battery
android:layout_width="300dp"
android:layout_height="10dp"
android:layout_gravity="center"
android:gravity="center"
android:padding="10dp" />
</LinearLayout>
整體而言,這個自定義 View 借助 Handler 週期性地更新坐標並調用 postInvalidate() 觸發重繪,配合 clipRect 控制填充比例,就實現了靈活的電池電量動態進度條。替換 loading_pic 這張圖片,即可自訂外觀。
Android 開機啟動流程剖析:從 Init 進程到 Launcher 亮起
Linux 內核完成初始化之後,控制权交給 Android 的 init 進程,接下來一連串的服務與應用才會依次啟動。本文以 Android 4.0 源碼為基礎,把這條啟
android:descendantFocusability 屬性詳解與焦點問題排查
開發中經常遇到這樣的困擾:自訂的 ListView 項目裡點擊沒有反應,無法取得焦點。這往往是由於項目中的子控制項搶走了焦點。本文詳細說明 descendantFocus
Android RocooFix 熱修復框架詳解
本文從熱修復技術的由來講起,回顧 Facebook 的 Dalvik 補丁經驗與 dex 分包方案,最後聚焦到 QQ 空間的熱補丁思路與 RocooFix 框架的實際使用
動手做個非 root 的 Android 任務管理器
先把話說清楚:不是系統級應用、又沒有 root 權限,自己做任務管理器的實際價值其實有限。就像在沒有 root 的手機上裝 LBE 這類手機助手,雖然也標榜一鍵清理記憶體