Android进阶篇-onTouchEvent的使用

这里通过演示一个可以拖动颜色球的例子来展示Android中onTouchEvent的使用以及自定义View。

实体类ColorBall:

[html]  view plain copy
  1. /**  
  2.  * @author gongchaobin  
  3.  *  
  4.  * 实体类 颜色球  
  5.  */  
  6. public class ColorBall  {  
  7.      private Bitmap img; //小球的图片   
  8.      private int coordX = 0; //画布上的X坐标  
  9.      private int coordY = 0; //画布上的Y坐标  
  10.      private int id;   
  11.      private static int count = 1;  
  12.      private boolean goRight = true;  
  13.      private boolean goDown = true;  
  14.    
  15.     public ColorBall(Context context, int drawable) {  
  16.         BitmapFactory.Options opts = new BitmapFactory.Options();  
  17.         opts.inJustDecodeBounds = true;  
  18.         img = BitmapFactory.decodeResource(context.getResources(), drawable);   
  19.         id=count;  
  20.         count++;  
  21.     }  
  22.       
  23.     public ColorBall(Context context, int drawable, Point point) {  
  24.         BitmapFactory.Options opts = new BitmapFactory.Options();  
  25.         opts.inJustDecodeBounds = true;  
  26.         img = BitmapFactory.decodeResource(context.getResources(), drawable);   
  27.         id=count;  
  28.         count++;  
  29.         coordXpoint.x;  
  30.         coordY = point.y;  
  31.     }  
  32.       
  33.     public static int getCount() {  
  34.         return count;  
  35.     }  
  36.       
  37.     void setX(int newValue) {  
  38.         coordX = newValue;  
  39.     }  
  40.       
  41.     public int getX() {  
  42.         return coordX;  
  43.     }  
  44.   
  45.     void setY(int newValue) {  
  46.         coordY = newValue;  
  47.    }  
  48.       
  49.     public int getY() {  
  50.         return coordY;  
  51.     }  
  52.       
  53.     public int getID() {  
  54.         return id;  
  55.     }  
  56.       
  57.     public Bitmap getBitmap() {  
  58.         return img;  
  59.     }  
  60.       
  61.     public void moveBall(int goX, int goY) {  
  62.         // check the borders, and set the direction if a border has reached  
  63.         if (coordX > 270){  
  64.             goRight = false;  
  65.         }  
  66.         if (coordX < 0){  
  67.             goRight = true;  
  68.         }  
  69.         if (coordY > 400){  
  70.             goDown = false;  
  71.         }  
  72.         if (coordY < 0){  
  73.             goDown = true;  
  74.         }  
  75.   
  76.         if (goRight){  
  77.             coordX += goX;  
  78.         }else{  
  79.             coordX -goX;  
  80.         }  
  81.         if (goDown){  
  82.             coordY += goY;  
  83.         }else{  
  84.             coordY -goY;  
  85.         }  
  86.     }  
  87.       
  88. }  
自定义DrawView:

[html]  view plain copy
  1. /**  
  2.  * @author gongchaobin  
  3.  *   
  4.  * 自定义View  
  5.  */  
  6. public class DrawView extends View {  
  7.    private ColorBall[] colorballs = new ColorBall[3];   
  8.    private int balID = 0;   
  9.       
  10.     public DrawView(Context context) {  
  11.         super(context);  
  12.         setFocusable(true);   
  13.           
  14.         /**在画布上设置三个初始的点*/  
  15.         Point point1 = new Point();  
  16.         point1.x = 50;  
  17.         point1.y = 20;  
  18.         Point point2 = new Point();  
  19.         point2.x = 100;  
  20.         point2.y = 20;  
  21.         Point point3 = new Point();  
  22.         point3.x = 150;  
  23.         point3.y = 20;  
  24.                          
  25.         /**New三个颜色球*/  
  26.         colorballs[0] = new ColorBall(context,R.drawable.bol_groen, point1);  
  27.         colorballs[1] = new ColorBall(context,R.drawable.bol_rood, point2);  
  28.         colorballs[2] = new ColorBall(context,R.drawable.bol_blauw, point3);  
  29.     }  
  30.       
  31.     @Override  
  32.     protected void onDraw(Canvas canvas) {  
  33.           
  34.         /**重绘颜色球*/  
  35.         for (ColorBall ball : colorballs) {  
  36.             canvas.drawBitmap(ball.getBitmap(), ball.getX(), ball.getY(), null);  
  37.         }  
  38.     }  
  39.       
  40.     public boolean onTouchEvent(MotionEvent event) {  
  41.         int eventaction = event.getAction();   
  42.           
  43.         int X = (int)event.getX();   
  44.         int Y = (int)event.getY();   
  45.   
  46.         switch (eventaction ) {   
  47.         /**当初始进来的时候 ,向下移动的状态*/  
  48.         case MotionEvent.ACTION_DOWN:   
  49.             balID = 0;  
  50.             for (ColorBall ball : colorballs) {  
  51.                 /**获取到小球的中心点*/  
  52.                     int centerX = ball.getX() + 25;  
  53.                     int centerY = ball.getY() + 25;  
  54.                       
  55.                     double radCircle  = Math.sqrt( (double) (((centerX-X)*(centerX-X)) + (centerY-Y)*(centerY-Y)));  
  56.                       
  57.                     /**如果移动的距离小于23,小球没动*/  
  58.                     if (radCircle < 23){  
  59.                         balID = ball.getID();  
  60.                         break;  
  61.                     }  
  62.               }  
  63.              break;   
  64.         /**balId>0,小球的移动状态*/  
  65.         case MotionEvent.ACTION_MOVE:   
  66.             if (balID > 0) {  
  67.                 colorballs[balID-1].setX(X-25);  
  68.                 colorballs[balID-1].setY(Y-25);  
  69.             }  
  70.             break;   
  71.         case MotionEvent.ACTION_UP:   
  72.              break;   
  73.         }   
  74.         /**invalidate方法 进行界面刷新重绘*/  
  75.         invalidate();   
  76.         return true;   
  77.     }  
  78. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值