时间:2022-12-24 13:50:50 | 栏目:Android代码 | 点击:次
本文实例为大家分享了Android检测手机多点触摸点数的具体代码,供大家参考,具体内容如下
多点触摸点数效果图
Circle.java
package com.zking.administrator.g160628_android19_pointstouch; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import java.util.Random; /** * Created by Administrator on 2017/7/9. */ public class Circle { public float x; public float y; public int r=100;//半径 public int id;//手指的名字 //三原色(每一个int类型的取值都是255) int red; int green; int blue; //随机数 Random random=new Random(); public Circle(float x, float y, int id) { this.x = x; this.y = y; this.id = id; red=random.nextInt(255); green=random.nextInt(255); blue=random.nextInt(255); } //画自己 public void drawSelf(Canvas canvas, Paint paint){ //设置颜色随机 paint.setColor(Color.rgb(red,green,blue)); canvas.drawCircle(x,y,r,paint); } }
MainActivity.java
package com.zking.administrator.g160628_android19_pointstouch; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new MyView(this)); } }
MyView.java
package com.zking.administrator.g160628_android19_pointstouch; import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import java.util.ArrayList; import java.util.List; /** * Created by Administrator on 2017/7/9. */ public class MyView extends View{ //全局变量(单点触摸) // private float x; // private float y; // private int r=100; //定义圆的集合 List<Circle> circles=new ArrayList<>(); public MyView(Context context) { super(context); } public MyView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //画笔 Paint paint=new Paint(); //画圆(单点触摸) // canvas.drawCircle(x,y,r,paint); for (Circle circle : circles) { //把画布和画笔传进去(运行第一次进入什么都没有,所有默认的圆也就没了) circle.drawSelf(canvas,paint); } } @Override public boolean onTouchEvent(MotionEvent event) { //获取手指的行为 int action=event.getAction(); int action_code=action&0xff; //手指的下标Index int pointIndex=action>>8;//右移8 //获取手指的坐标 float x=event.getX(pointIndex); float y=event.getY(pointIndex); //获取手指的名字(id) int id=event.getPointerId(pointIndex); if(action_code>=5){ action_code-=5; } switch (action_code){//action_code单点触摸是012,多点触摸562 //case 5://多点触摸的按下时5 case MotionEvent.ACTION_DOWN://0按下 //实例化圆 Circle circle=new Circle(x,y,id); //将圆添加到集合中 circles.add(circle); break; case MotionEvent.ACTION_UP://1抬起 //调用拿圆的方法(拿到是哪个圆我集合就移除,然后重新绘制) circles.remove(get(id)); break; case MotionEvent.ACTION_MOVE://2移动 //拿到所有手指的数量(循环所有的圆拿到他的ID,然后用现在的圆,给每一个圆的xy重新赋值) for (int i = 0; i <event.getPointerCount() ; i++) { //根据下标拿到ID int did=event.getPointerId(i); //根据ID拿到新的圆(i就是当前手指的下标,因为我们是根据下标去拿xy) get(did).x=event.getX(i); get(did).y=event.getY(i); } break; } //重新调用onDraw 重绘 在主线程调用 invalidate(); return true; } //这个方法的目的就是拿圆 public Circle get(int id){ for (Circle circle : circles) { //判断(拿到每一个圆的id等等于你传过来的ID,如果是你想要得圆就return过去) if(circle.id==id){ return circle; } } return null; } }