編輯:關於 Android 程式設計
專案裡需要一個定位效果:讓一個小圓點繞著某個圓心旋轉。翻了一下 Canvas 的 API,最直接的做法是 canvas.drawBitmap(bitmap, matrix, paint),也就是透過 Matrix 控制旋轉中心與角度。
下面做一個小 demo:小圓點順時針畫圓,畫滿一圈後再逆時針收回,用 Timer 驅動動畫。
package com.example.test;
import java.util.Timer;
import java.util.TimerTask;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.SurfaceHolder.Callback;
@SuppressLint("HandlerLeak")
class Surface extends SurfaceView implements Callback {
Bitmap bmp;
private Paint mPaint;
private SurfaceHolder mHolder;
int degree = 0;
Timer timer = new Timer();
boolean flag = true;
public Surface(Context context) {
super(context);
mHolder = getHolder();
mHolder.addCallback(this);
mPaint = new Paint();
mPaint.setColor(Color.GREEN);
mPaint.setAntiAlias(true); // 抗鋸齒
setFocusable(true);
setFocusableInTouchMode(true);
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.camera_r_local);
bmp = Bitmap.createScaledBitmap(bmp, 15, 15, false);
}
public Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
canvas_ok();
break;
}
super.handleMessage(msg);
}
};
public void canvas_ok() {
Canvas canvas = null;
try {
canvas = mHolder.lockCanvas();
canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR); // 清畫布
Matrix matrix = new Matrix();
matrix.postTranslate(130, 0);
matrix.postRotate(degree, 130, 130); // 以 (130,130) 為圓心旋轉
canvas.drawBitmap(bmp, matrix, mPaint);
} catch (Exception e) {
} finally {
if (canvas != null) {
mHolder.unlockCanvasAndPost(canvas);
}
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
canvas_ok();
timer.schedule(new TimerTask() {
@Override
public void run() {
if (flag) {
degree++;
if (degree == 360) {
flag = false;
}
} else {
degree--;
if (degree == -360 || degree == 0) {
flag = true;
}
}
Log.e("flag", degree + " : " + flag);
handler.sendEmptyMessage(0);
}
}, 50, 50);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (timer != null) {
timer.cancel();
timer = null;
}
}
}
把 Surface 直接塞進 RelativeLayout 當內容檢視即可。
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.widget.RelativeLayout;
public class Main7 extends Activity {
Surface surface;
RelativeLayout.LayoutParams laParams;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
surface = new Surface(this);
laParams = new RelativeLayout.LayoutParams(260, 260);
setContentView(surface, laParams);
}
}
Matrix 的操作順序很重要。這裡先 postTranslate 把圖位移到 (130, 0),再以 (130, 130) 為軸心旋轉,於是圖片實際上是繞著畫布中心走圓。如果想改成其他圓心,只要調整 postRotate 的第二、三個參數即可,不必更動位移。
另外,Timer 的工作執行緒與 UI 執行緒不同,所以每 50 毫秒更新角度後,必須透過 Handler 發訊息回主執行緒,再由主執行緒去 lockCanvas 繪製,否則會出錯。這也是為什麼程式裡會出現 handler.sendEmptyMessage(0) 這一步。
最後別忘了在 surfaceDestroyed 中 timer.cancel(),否則畫面離開後計時器仍在跑,很容易造成 Activity 無法被回收。
執行效果如下:
ListView 常用優化技巧彙編(Android 群英傳)
前言:ListView——列表,作為一個非常重要的顯示方式,不管在 Web 還是移動平台,都是展示信息的利器。在 Android 中,ListView 控件扛起了這副重擔
Android 自定義 View:手寫簽名板實作
概述 本文透過自定義 View 實現一個 Android 手寫簽名板,支持透明背景、貝塞爾平滑曲線,並能對最終結果進行邊緣空白裁剪。 自定義 LinePathView 核
給 ListView 的數據入場加動畫:LayoutAnimationController 用法詳解
要讓 ListView 的 item 帶著動畫依次登場,標準做法是 LayoutAnimationController:先準備好動畫集合,交給控制器,再用 ListVi
違章查詢 App 源碼分享
本文分享一個違章查詢小工具的源碼與測試安裝包,主要透過網路查詢介面取得資料,並將結果呈現在 App 畫面中。源碼地址:https://xiazai.jb51.net/20