Welcome 微信登录

首页 / 操作系统 / Linux / Android应用实例之跟随手指的小球——自定义View应用

实现的功能:手指在屏幕上滑动,变幻颜色的小球始终跟随手指移动。实现的思路:1)自定义View,在onDraw中画圆作为小球;2)重写自定义View的onTouchEvent方法,记录触屏坐标,用新的坐标重新绘制小球。关键技术点:自定义View应用、触摸事件处理、canvas绘图、Paint应用第一步:新建一个工程,命名为BallViewDemo,Activity命名为BallActivity。第二步:编写自定义View类BallView,本例中将BallView作为BallActivity的内部类,BallActivity代码如下:
  1. package com.zyg.customview.ball;  
  2.   
  3. import java.util.Random;  
  4.   
  5. import Android.app.Activity;  
  6. import android.content.Context;  
  7. import android.graphics.Canvas;  
  8. import android.graphics.Color;  
  9. import android.graphics.Paint;  
  10. import android.os.Bundle;  
  11. import android.view.Display;  
  12. import android.view.MotionEvent;  
  13. import android.view.View;  
  14. import android.view.Window;  
  15. import android.view.WindowManager;  
  16.   
  17. public class BallActivity extends Activity {  
  18.     private int screenW;        //屏幕宽度   
  19.     private int screenH;        //屏幕高度   
  20.     @Override  
  21.     public void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         Display dis = this.getWindowManager().getDefaultDisplay();  
  24.         // 设置全屏   
  25.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  26.         this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
  27.                 WindowManager.LayoutParams.FLAG_FULLSCREEN);  
  28.         // 获取屏幕宽度   
  29.         screenW = dis.getWidth();  
  30.         // 获取屏幕高度   
  31.         screenH = dis.getHeight();  
  32.           
  33.         setContentView(new BallView(this));  
  34.     }  
  35.       
  36.     //自定义绘图类   
  37.     class BallView extends View{  
  38.         private Paint paint;        //定义画笔   
  39.         private float cx = 50;      //圆点默认X坐标   
  40.         private float cy = 50;      //圆点默认Y坐标   
  41.         private int radius = 20;  
  42.         //定义颜色数组   
  43.         private int colorArray[] = {Color.BLACK,Color.BLACK,Color.GREEN,Color.YELLOW, Color.RED};  
  44.         private int paintColor = colorArray[0]; //定义画笔默认颜色   
  45.           
  46.         public BallView(Context context) {  
  47.             super(context);  
  48.             //初始化画笔   
  49.             initPaint();  
  50.         }  
  51.         private void initPaint(){  
  52.             paint = new Paint();  
  53.             //设置消除锯齿   
  54.             paint.setAntiAlias(true);  
  55.             //设置画笔颜色   
  56.             paint.setColor(paintColor);  
  57.         }  
  58.           
  59.         //重写onDraw方法实现绘图操作   
  60.         @Override  
  61.         protected void onDraw(Canvas canvas) {  
  62.             super.onDraw(canvas);  
  63.             //将屏幕设置为白色   
  64.             canvas.drawColor(Color.WHITE);  
  65.             //修正圆点坐标   
  66.             revise();  
  67.             //随机设置画笔颜色   
  68.             setPaintRandomColor();  
  69.             //绘制小圆作为小球   
  70.             canvas.drawCircle(cx, cy, radius, paint);  
  71.         }  
  72.           
  73.         //为画笔设置随机颜色   
  74.         private void setPaintRandomColor(){  
  75.             Random rand = new Random();  
  76.             int randomIndex = rand.nextInt(colorArray.length);  
  77.             paint.setColor(colorArray[randomIndex]);  
  78.         }  
  79.           
  80.         //修正圆点坐标   
  81.         private void revise(){  
  82.             if(cx <= radius){  
  83.                 cx = radius;  
  84.             }else if(cx >= (screenW-radius)){  
  85.                 cx = screenW-radius;  
  86.             }  
  87.             if(cy <= radius){  
  88.                 cy = radius;  
  89.             }else if(cy >= (screenH-radius)){  
  90.                 cy = screenH-radius;  
  91.             }  
  92.         }  
  93.           
  94.         @Override  
  95.         public boolean onTouchEvent(MotionEvent event) {  
  96.             switch (event.getAction()) {  
  97.             case MotionEvent.ACTION_DOWN:  
  98.                 // 按下   
  99.                 cx = (int) event.getX();  
  100.                 cy = (int) event.getY();  
  101.                 // 通知重绘   
  102.                 postInvalidate();   //该方法会调用onDraw方法,重新绘图   
  103.                 break;  
  104.             case MotionEvent.ACTION_MOVE:  
  105.                 // 移动   
  106.                 cx = (int) event.getX();  
  107.                 cy = (int) event.getY();  
  108.                 // 通知重绘   
  109.                 postInvalidate();  
  110.                 break;  
  111.             case MotionEvent.ACTION_UP:  
  112.                 // 抬起   
  113.                 cx = (int) event.getX();  
  114.                 cy = (int) event.getY();  
  115.                 // 通知重绘   
  116.                 postInvalidate();  
  117.                 break;  
  118.             }  
  119.               
  120.             /* 
  121.              * 备注1:此处一定要将return super.onTouchEvent(event)修改为return true,原因是: 
  122.              * 1)父类的onTouchEvent(event)方法可能没有做任何处理,但是返回了false。 
  123.              * 2)一旦返回false,在该方法中再也不会收到MotionEvent.ACTION_MOVE及MotionEvent.ACTION_UP事件。 
  124.              */  
  125.             //return super.onTouchEvent(event);   
  126.             return true;    
  127.         }  
  128.     }  
  129. }  
main.xml与AndroidManifest.xml未作修改,不再贴出~备注:代码中的备注1介绍???onTouchEvent方法在实际开发中的一个Bug的解决方法,详见代码。第三步:运行程序,效果如下:下一篇将用自定义SurfaceView代替自定义View实现该实例功能(http://www.linuxidc.com/Linux/2011-10/44495.htm),并总结一下自定义View与自定义SurfaceView区别与应用场景。