利用Android自定义View实现转盘旋转的效果
2010-05-31 14:27:00 来源:WEB开发网canvas.drawBitmap(panhandpic, panhandTrans, null);
}
//重写的run函数,用来控制转盘转动
public void run() {
try {
while(true){
if(ifRotate){
this.x+=25;
//这个函数强制UI线程刷新界面
this.postInvalidate();
Thread.sleep(50);
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void startRotate(){
this.ifRotate=true;
}
public void stopRotate(){
this.ifRotate=false;
}
}
package com.test.www;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.View;
//自定义的转盘View
public class zhuanpanView extends View implements Runnable{
//界面需要的图片
private Bitmap panpic;
private Bitmap panhandpic;
//旋转矩阵
private Matrix panRotate=new Matrix();
//平移矩阵
private Matrix panhandTrans=new Matrix();
private int x=0;
private boolean ifRotate=false;
public zhuanpanView(Context context, AttributeSet attrs) {
super(context, attrs);
Resources r=context.getResources();
//设置指针平移矩阵为按向量(160,160-指针的高度)平移
panhandTrans.setTranslate(160, 160-76);
//生成图片
panpic=BitmapFactory.decodeStream(r.openRawResource(R.drawable.pan));
panhandpic=BitmapFactory.decodeStream(r.openRawResource(R.drawable.panhand));
//用线程来刷新界面
Thread thread=new Thread(this);
更多精彩
赞助商链接