利用Android自定义View实现转盘旋转的效果
2010-05-31 14:27:00 来源:WEB开发网thread.start();
}
//重写View类的onDraw()函数
@Override
protected void onDraw(Canvas canvas) {
//设置转盘旋转矩阵为以(160,160)坐标为圆心,旋转X角度
panRotate.setRotate(x, 160, 160);
canvas.drawBitmap(panpic, panRotate, null);
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;
}
}
二、主Activity
view plaincopy to clipboardprint?
package com.test.www;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity {
/** Called when the activity is first created. */
private void findViewAndButton(){
//自定义的View
final zhuanpanView panView=(zhuanpanView) this.findViewById(R.id.zhuanpanView);
//开始旋转的按钮
Button startButton=(Button) this.findViewById(R.id.startButton);
startButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
panView.startRotate();
}
});
//停止旋转的按钮
Button stopButton=(Button) this.findViewById(R.id.stopButton);
stopButton.setOnClickListener(new Button.OnClickListener(){
更多精彩
赞助商链接