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 }
一、自定义的转盘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 }
相关文章推荐
- 利用Android自定义View实现转盘旋转的效果
- Android自定义ImageView实现图片缩放滑动,双击放大缩小,多点触控旋转,圆角矩形,圆形和仿刮刮卡效果
- Android自定义View实现叶子飘动旋转效果(四)
- Android 自定义RecyclerView 实现真正的Gallery效果
- Android实现图片轮显效果——自定义ViewPager控件
- Android UI:看看Google官方自定义带旋转动画的ImageView-----RotateImageView怎么写(附 图片淡入淡出效果)
- android 自定义ImageView实现图片手势滑动,多点触摸放大缩小效果
- Android自定义view--SurfaceView实现墨迹天气的风车效果
- Android 自定义RecyclerView 实现真正的Gallery效果
- android 自定义ImageView实现图片手势滑动,多点触摸放大缩小效果
- android自定义View实现图片上传进度显示(仿手机QQ上传效果)
- Android自定义控件系列六:自定义ViewGroup(一)实现ViewPager效果
- Android-详解RecyclerView+BGARefreshLayout实现自定义下拉刷新、上拉加载和侧滑删除效果
- Android -- 自定义ViewGroup实现FlowLayout效果
- Android自定义View实现黑客帝国数字雨效果
- Android自定义View实现优惠券效果
- android 自定义View用三张图片实现七个音量等级的录音效果
- Android 自定义View——蒙版擦除效果实现
- Android 实现横向标题栏滚动效果(HorizontalScrollView + GridView + Viewpager + 自定义适配器)
- Android:自定义view 实现雷达扫描效果