Android系列之手机多点触摸画圆

来源:互联网 发布:员工培训档案软件 编辑:程序博客网 时间:2024/06/06 01:38

1、先写个实体类,代表圆

  1. public class MyCircle {  
  2.     public float x;  
  3.     public float y;  
  4.     public int r=100;  
  5.     public int pointId;  
  6.     int red;  
  7.     int green;  
  8.     int blue;  
  9.     Random random=new Random();  
  10.   
  11.     public MyCircle(float x, float y, int pointId) {  
  12.         this.x = x;  
  13.         this.y = y;  
  14.         this.pointId = pointId;  
  15.         red=random.nextInt(255);  
  16.         green=random.nextInt(255);  
  17.         blue=random.nextInt(255);  
  18.     }  
  19.     public void drawSelf(Canvas canvas, Paint paint){  
  20.         paint.setStyle(Paint.Style.STROKE);  
  21.         paint.setColor(Color.rgb(red,green,blue));  
  22.         canvas.drawCircle(x,y,r,paint);  
  23.     }  
  24. }  
2、然后我们自己再写个java类,继承View  代表显示的界面

  1. public class MyView extends View {  
  2.   
  3.     List<MyCircle> lt=new ArrayList<>();  
  4.   
  5.   
  6.   
  7.     public MyView(Context context) {  
  8.         super(context);  
  9.     }  
  10.   
  11.     public MyView(Context context, @Nullable AttributeSet attrs) {  
  12.         super(context, attrs);  
  13.     }  
  14.   
  15.     public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {  
  16.         super(context, attrs, defStyleAttr);  
  17.     }  
  18.   
  19.     public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {  
  20.         super(context, attrs, defStyleAttr, defStyleRes);  
  21.     }  
  22.   
  23.     @Override  
  24.     protected void onDraw(Canvas canvas) {  
  25.         super.onDraw(canvas);  
  26.         Paint paint=new Paint();  
  27.         for (MyCircle myCircle : lt) {  
  28.             myCircle.drawSelf(canvas,paint);  
  29.         }  
  30.   
  31.     }  
  32.   
  33.     @Override  
  34.     public boolean onTouchEvent(MotionEvent event) {  
  35.         //获取手指的行为  
  36.         int action=event.getAction();  
  37.         int action_code=action&0xff;  
  38.         //手指的下标  
  39.         int pointIndex=action>>8;  
  40.   
  41.   
  42.         //获取手指的坐标  
  43.         float x=event.getX(pointIndex);  
  44.         float y=event.getY(pointIndex);  
  45.   
  46.   
  47.   
  48.         //获取手指的名字的ID  
  49.         int pointId=event.getPointerId(pointIndex);  
  50.         if(action_code>=5){  
  51.             action_code-=5;  
  52.         }  
  53.   
  54.         switch (action_code) {  
  55.             case MotionEvent.ACTION_DOWN:  
  56.                 //实例化园  
  57.                 MyCircle myCircle=new MyCircle(x,y,pointId);  
  58.                 //将园添加到集合中  
  59.                 lt.add(myCircle);  
  60.                 break;  
  61.             case MotionEvent.ACTION_UP:  
  62.                 lt.remove(get(pointId));  
  63.                 break;  
  64.             case MotionEvent.ACTION_MOVE:  
  65.                 for (int i = 0; i <event.getPointerCount() ; i++) {  
  66.                     int id=event.getPointerId(i);  
  67.                     get(id).x=event.getX(i);  
  68.                     get(id).y=event.getY(i);  
  69.                 }  
  70.                 break;  
  71.         }  
  72.   
  73.         //重新调用onDraw 重绘  
  74.         invalidate();  
  75.         return true;  
  76.     }  
  77.   
  78.     public MyCircle get(int pointId){  
  79.         for (MyCircle myCircle : lt) {  
  80.             if(myCircle.pointId==pointId){  
  81.                 return myCircle;  
  82.             }  
  83.         }  
  84.         return null;  
  85.     }  
  86.   
  87.   
  88. }  
最后我们在MainaActivity中改一句代码就可以了

  1. setContentView(new MyView(this));  

原创粉丝点击