Android graphics画图的点击事件处理


这个示例可能在项目中不会遇到,我也不知道用来做什么,但还是写出来了,希望给大家一些提示。

  1. package com.jacp.test;   
  2.   
  3. import Android.app.Activity;   
  4. import android.content.Context;   
  5. import android.graphics.Canvas;   
  6. import android.graphics.Color;   
  7. import android.graphics.Paint;   
  8. import android.graphics.Rect;   
  9. import android.graphics.RectF;   
  10. import android.os.Bundle;   
  11. import android.util.DisplayMetrics;   
  12. import android.view.MotionEvent;   
  13. import android.view.View;   
  14. import android.widget.Toast;   
  15.   
  16. public class DrawSimpleCircle extends Activity {   
  17.   
  18.     public void onCreate(Bundle savedInstanceState) {   
  19.         super.onCreate(savedInstanceState);   
  20.            
  21.         DisplayMetrics metrics = new DisplayMetrics();   
  22.         this.getWindowManager().getDefaultDisplay().getMetrics(metrics);   
  23.            
  24.         // 屏幕的分辨率   
  25.         int width = metrics.widthPixels;   
  26.         int height = metrics.heightPixels;   
  27.            
  28.         setContentView(new MyCircle(this, width, height));   
  29.     }   
  30.        
  31.     class MyCircle extends View {   
  32.            
  33.         private Context context;   
  34.            
  35.         /**  
  36.          * 屏幕的宽  
  37.          */  
  38.         private int width;   
  39.            
  40.         /**  
  41.          * 屏幕的高  
  42.          */  
  43.         private int height;   
  44.            
  45.         /**  
  46.          *  颜色区分区域  
  47.          */  
  48.         private int[] colors = new int[] { Color.BLACK, Color.BLUE, Color.CYAN,   
  49.                 Color.GREEN, Color.GRAY, Color.MAGENTA, Color.RED, Color.LTGRAY};   
  50.         private String[] colorStrs = new String[] {   
  51.                 "黑色""蓝色""青绿色""绿色""灰色""洋红色""红色""浅灰色"};   
  52.            
  53.         /**  
  54.          * 大园半径  
  55.          */  
  56.         private float bigR;   
  57.            
  58.         /**  
  59.          * 小圆半径  
  60.          */  
  61.         private float litterR;   
  62.            
  63.         /**  
  64.          * 屏幕中间点的X坐标  
  65.          */  
  66.         private float centerX;   
  67.            
  68.         /**  
  69.          * 屏幕中间点的Y坐标  
  70.          */  
  71.         private float centerY;   
  72.            
  73.         public MyCircle(Context context, int width, int height) {   
  74.             super(context);   
  75.             this.context = context;   
  76.             this.width = width;   
  77.             this.height = height;   
  78.             setFocusable(true);   
  79.                
  80.             System.out.println("width="+width+"<---->height="+height);   
  81.             // 设置两个圆的半径   
  82.             bigR = (width - 20)/2;   
  83.             litterR = bigR/2;   
  84.                
  85.             centerX = width/2;   
  86.             centerY = height/2;   
  87.         }   
  88.   
  89.         @Override  
  90.         protected void onDraw(Canvas canvas) {   
  91.             // 画背景颜色   
  92.             Paint bg = new Paint();   
  93.             bg.setColor(Color.WHITE);   
  94.             Rect bgR = new Rect(00, width, height);   
  95.             canvas.drawRect(bgR, bg);   
  96.                
  97.             float start = 0F;   
  98.             Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);   
  99.             for(int i = 0; i < 4; i ++) {   
  100.                 //注意一定要先画大圆,再画小圆,不然看不到效果,小圆在下面会被大圆覆盖   
  101.                 // 画大圆   
  102.                 RectF bigOval = new RectF(centerX - bigR, centerY - bigR,    
  103.                         centerX + bigR, centerY + bigR);   
  104.                 paint.setColor(colors[i]);   
  105.                 canvas.drawArc(bigOval, start, 90true, paint);   
  106.                    
  107.                 // 画小圆   
  108.                 RectF litterOval = new RectF(centerX - litterR, centerY - litterR,    
  109.                         centerX + litterR, centerY + litterR);   
  110.                 paint.setColor(colors[i+2]);   
  111.                 canvas.drawArc(litterOval, start, 90true, paint);   
  112.                    
  113.                 start += 90F;   
  114.             }   
  115.                
  116.             super.onDraw(canvas);   
  117.         }   
  118.            
  119.         @Override  
  120.         public boolean onTouchEvent(MotionEvent event) {   
  121.             // 获取点击屏幕时的点的坐标   
  122.             float x = event.getX();   
  123.             float y = event.getY();   
  124.             whichCircle(x, y);   
  125.             return super.onTouchEvent(event);   
  126.         }   
  127.   
  128.         /**  
  129.          * 确定点击的点在哪个圆内  
  130.          * @param x  
  131.          * @param y  
  132.          */  
  133.         private void whichCircle(float x, float y) {   
  134.             // 将屏幕中的点转换成以屏幕中心为原点的坐标点   
  135.             float mx = x - centerX;   
  136.             float my = y - centerY;   
  137.             float result = mx * mx + my * my;   
  138.                
  139.             StringBuilder tip = new StringBuilder();   
  140.             tip.append("您点击了");   
  141.             // 高中的解析几何   
  142.             if(result <= litterR*litterR) {// 点击的点在小圆内   
  143.                 tip.append("小圆的");   
  144.                 tip.append(colorStrs[whichZone(mx, my)+2]);   
  145.                 tip.append("区域");   
  146.             } else if(result <= bigR * bigR) {// 点击的点在大圆内   
  147.                 tip.append("大圆的");   
  148.                 tip.append(colorStrs[whichZone(mx, my)]);   
  149.                 tip.append("区域");   
  150.             } else {// 点不在作作区域   
  151.                 tip.append("作用区域以外的区域");   
  152.             }   
  153.                
  154.             Toast.makeText(context, tip, Toast.LENGTH_SHORT).show();   
  155.         }   
  156.            
  157.         /**  
  158.          * 判断点击了圆的哪个区域  
  159.          * @param x  
  160.          * @param y  
  161.          * @return  
  162.          */  
  163.         private int whichZone(float x, float y) {   
  164.             // 简单的象限点处理   
  165.             // 第一象限在右下角,第二象限在左下角,代数里面的是逆时针,这里是顺时针   
  166.             if(x > 0 && y > 0) {   
  167.                 return 0;   
  168.             } else if(x > 0 && y < 0) {   
  169.                 return 3;   
  170.             } else if(x < 0 && y < 0) {   
  171.                 return 2;   
  172.             } else if(x < 0 && y > 0) {   
  173.                 return 1;   
  174.             }   
  175.                
  176.             return -1;   
  177.         }   
  178.            
  179.     }   
  180.        
  181. }   

其中用数学问题解决了点击坐标点位置的问题,使用问题简单化。其实这只是一个简单的示例,有兴趣的读者可以研究一下,当将每个圆分成八块区域时,怎样判断区域位置;当有一个半径数组,即有N个半径不同的圆,又怎么判断点击的是哪一个圆;当横竖屏切换时,怎么控制圆心在屏幕中央,怎样不让圆的半径超出屏幕,若超出,怎样添加左右上下滚动条。其中还有最后一个问题没解决,以后有时间再贴上来。

Android

相关内容