加入收藏 | 设为首页 | 会员中心 | 我要投稿 厦门站长网 (https://www.0592zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 教程 > 正文

Android graphics画图的点击事件解决

发布时间:2021-11-30 12:36:57 所属栏目:教程 来源:互联网
导读:这个示例可能在项目中不会遇到,我也不知道用来做什么,但还是写出来了,希望给大家一些提示。 package com.jacp.test; import Android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; impo
这个示例可能在项目中不会遇到,我也不知道用来做什么,但还是写出来了,希望给大家一些提示。
 
package com.jacp.test;   
  
import Android.app.Activity;   
import android.content.Context;   
import android.graphics.Canvas;   
import android.graphics.Color;   
import android.graphics.Paint;   
import android.graphics.Rect;   
import android.graphics.RectF;   
import android.os.Bundle;   
import android.util.DisplayMetrics;   
import android.view.MotionEvent;   
import android.view.View;   
import android.widget.Toast;   
  
public class DrawSimpleCircle extends Activity {   
  
    public void onCreate(Bundle savedInstanceState) {   
        super.onCreate(savedInstanceState);   
           
        DisplayMetrics metrics = new DisplayMetrics();   
        this.getWindowManager().getDefaultDisplay().getMetrics(metrics);   
           
        // 屏幕的分辨率   
        int width = metrics.widthPixels;   
        int height = metrics.heightPixels;   
           
        setContentView(new MyCircle(this, width, height));   
    }   
       
    class MyCircle extends View {   
           
        private Context context;   
           
        /**  
         * 屏幕的宽  
         */  
        private int width;   
           
        /**  
         * 屏幕的高  
         */  
        private int height;   
           
        /**  
         *  颜色区分区域  
         */  
        private int[] colors = new int[] { Color.BLACK, Color.BLUE, Color.CYAN,   
                Color.GREEN, Color.GRAY, Color.MAGENTA, Color.RED, Color.LTGRAY};   
        private String[] colorStrs = new String[] {   
                "黑色", "蓝色", "青绿色", "绿色", "灰色", "洋红色", "红色", "浅灰色"};   
           
        /**  
         * 大园半径  
         */  
        private float bigR;   
           
        /**  
         * 小圆半径  
         */  
        private float litterR;   
           
        /**  
         * 屏幕中间点的X坐标  
         */  
        private float centerX;   
           
        /**  
         * 屏幕中间点的Y坐标  
         */  
        private float centerY;   
           
        public MyCircle(Context context, int width, int height) {   
            super(context);   
            this.context = context;   
            this.width = width;   
            this.height = height;   
            setFocusable(true);   
               
            System.out.println("width="+width+"<---->height="+height);   
            // 设置两个圆的半径   
            bigR = (width - 20)/2;   
            litterR = bigR/2;   
               
            centerX = width/2;   
            centerY = height/2;   
        }   
  
        @Override  
        protected void onDraw(Canvas canvas) {   
            // 画背景颜色   
            Paint bg = new Paint();   
            bg.setColor(Color.WHITE);   
            Rect bgR = new Rect(0, 0, width, height);   
            canvas.drawRect(bgR, bg);   
               
            float start = 0F;   
            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);   
            for(int i = 0; i < 4; i ++) {   
                //注意一定要先画大圆,再画小圆,不然看不到效果,小圆在下面会被大圆覆盖   
                // 画大圆   
                RectF bigOval = new RectF(centerX - bigR, centerY - bigR,    
                        centerX + bigR, centerY + bigR);   
                paint.setColor(colors[i]);   
                canvas.drawArc(bigOval, start, 90, true, paint);   
                   
                // 画小圆   
                RectF litterOval = new RectF(centerX - litterR, centerY - litterR,    
                        centerX + litterR, centerY + litterR);   
                paint.setColor(colors[i+2]);   
                canvas.drawArc(litterOval, start, 90, true, paint);   
                   
                start += 90F;   
            }   
               
            super.onDraw(canvas);   
        }   
           
        @Override  
        public boolean onTouchEvent(MotionEvent event) {   
            // 获取点击屏幕时的点的坐标   
            float x = event.getX();   
            float y = event.getY();   
            whichCircle(x, y);   
            return super.onTouchEvent(event);   
        }   
  
        /**  
         * 确定点击的点在哪个圆内  
         * @param x  
         * @param y  
         */  
        private void whichCircle(float x, float y) {   
            // 将屏幕中的点转换成以屏幕中心为原点的坐标点   
            float mx = x - centerX;   
            float my = y - centerY;   
            float result = mx * mx + my * my;   
               
            StringBuilder tip = new StringBuilder();   
            tip.append("您点击了");   
            // 高中的解析几何   
            if(result <= litterR*litterR) {// 点击的点在小圆内   
                tip.append("小圆的");   
                tip.append(colorStrs[whichZone(mx, my)+2]);   
                tip.append("区域");   
            } else if(result <= bigR * bigR) {// 点击的点在大圆内   
                tip.append("大圆的");   
                tip.append(colorStrs[whichZone(mx, my)]);   
                tip.append("区域");   
            } else {// 点不在作作区域   
                tip.append("作用区域以外的区域");   
            }   
               
            Toast.makeText(context, tip, Toast.LENGTH_SHORT).show();   
        }   
           
        /**  
         * 判断点击了圆的哪个区域  
         * @param x  
         * @param y  
         * @return  
         */  
        private int whichZone(float x, float y) {   
            // 简单的象限点处理   
            // 第一象限在右下角,第二象限在左下角,代数里面的是逆时针,这里是顺时针   
            if(x > 0 && y > 0) {   
                return 0;   
            } else if(x > 0 && y < 0) {   
                return 3;   
            } else if(x < 0 && y < 0) {   
                return 2;   
            } else if(x < 0 && y > 0) {   
                return 1;   
            }   
               
            return -1;   
        }   
           
    }   
       
}   
其中用数学问题解决了点击坐标点位置的问题,使用问题简单化。其实这只是一个简单的示例,有兴趣的读者可以研究一下,当将每个圆分成八块区域时,怎样判断区域位置;当有一个半径数组,即有N个半径不同的圆,又怎么判断点击的是哪一个圆;当横竖屏切换时,怎么控制圆心在屏幕中央,怎样不让圆的半径超出屏幕,若超出,怎样添加左右上下滚动条。其中还有最后一个问题没解决,以后有时间再贴上来。

(编辑:厦门站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读