您的位置:首页 > 移动开发 > Android开发

Android自定义View实现转盘旋转的效果

2013-04-12 16:30 996 查看
onDraw函数在界面刷新时会被调用,通过线程控制可以实现动画的效果,这里提供一个用自定义View实现的类似幸运转盘的例子。

一、自定义的转盘View

01 package com.demo;

02 import android.content.Context;

03 import android.content.res.Resources;

04 import android.graphics.Bitmap;

05 import android.graphics.BitmapFactory;

06 import android.graphics.Canvas;

07 import android.graphics.Matrix;

08 import android.util.AttributeSet;

09 import android.view.View;

10 //自定义的转盘View

11 public class zhuanpanView extends View implements Runnable{

12 //界面需要的图片

13 private Bitmap panpic;

14 private Bitmap panhandpic;

15 //旋转矩阵

16 private Matrix panRotate=new Matrix();

17 //平移矩阵

18 private Matrix panhandTrans=new Matrix();

19 private int x=0;

20 private boolean ifRotate=false;

21 public zhuanpanView(Context context, AttributeSet attrs) {

22 super(context, attrs);

23 Resources r=context.getResources();

24 //设置指针平移矩阵为按向量(160,160-指针的高度)平移

25 panhandTrans.setTranslate(160, 160-76);

26 //生成图片

27 panpic=BitmapFactory.decodeStream(r.openRawResource(R.drawable.pan));

28 panhandpic=BitmapFactory.decodeStream(r.openRawResource(R.drawable.panhand));

29 //用线程来刷新界面

30 Thread thread=new Thread(this);

31 thread.start();

32 }

33 //重写View类的onDraw()函数

34 @Override

35 protected void onDraw(Canvas canvas) {

36 //设置转盘旋转矩阵为以(160,160)坐标为圆心,旋转X角度

37 panRotate.setRotate(x, 160, 160);

38 canvas.drawBitmap(panpic, panRotate, null);

39 canvas.drawBitmap(panhandpic, panhandTrans, null);

40 }

41 //重写的run函数,用来控制转盘转动

42 public void run() {

43 try {

44 while(true){

45 if(ifRotate){

46 this.x+=25;

47 //这个函数强制UI线程刷新界面

48 this.postInvalidate();

49 Thread.sleep(50);

50 }

51 }

52 } catch (InterruptedException e) {

53 e.printStackTrace();

54 }

55 }

56 public void startRotate(){

57 this.ifRotate=true;

58 }

59 public void stopRotate(){

60 this.ifRotate=false;

61 }

62 } 二、主Activity

01 package com.demo;

02

03 import android.app.Activity;

04 import android.os.Bundle;

05 import android.view.View;

06 import android.view.View.OnClickListener;

07 import android.widget.Button;

08 public class Main extends Activity {

09 /** Called when the activity is first created. */

10 private void findViewAndButton(){

11 //自定义的View

12 final zhuanpanView panView=(zhuanpanView) this.findViewById(R.id.zhuanpanView);

13 //开始旋转的按钮

14 Button startButton=(Button) this.findViewById(R.id.startButton);

15 startButton.setOnClickListener(new OnClickListener(){

16 public void onClick(View v) {

17 panView.startRotate();

18 }

19 });

20 //停止旋转的按钮

21 Button stopButton=(Button) this.findViewById(R.id.stopButton);

22 stopButton.setOnClickListener(new Button.OnClickListener(){

23 public void onClick(View v) {

24 panView.stopRotate();

25 }

26 });

27 }

28 @Override

29 public void onCreate(Bundle savedInstanceState) {

30 super.onCreate(savedInstanceState);

31 setContentView(R.layout.main);

32 findViewAndButton();

33 }

34 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐