android多点触摸效果
殇丶 人气:01.获取点击xy轴的下标,实现触摸效果。
获取XY画一个圆并且自动从下变大,直到消失不见。
效果图如下:
代码如下:
1.写一个实体类,用于存写触摸点击的XY轴下表,并根据获得的下标用半径把圆画出来,半径默认为0
package com.example.android_pointstouch; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.os.Handler; import android.os.Message; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.View; import java.util.Random; /** * Created by Administrator on 2017/7/9 0009. */ public class Circle { public float x; public float y; public int r; public int pointId; private int red; private int green; private int blue; Random random=new Random(); public Circle(float x,float y,int r,int pointId){ this.x=x; this.y=y; this.r=r; this.pointId=pointId; this.red=random.nextInt(255); this.green=random.nextInt(255); this.blue=random.nextInt(255); } //画圆 public void drawSelf(Canvas canvas, Paint paint){ paint.setColor(Color.rgb(red,green,blue));//颜色 canvas.drawCircle(x,y,r,paint); paint.setStyle(Paint.Style.STROKE); } }
2.利用线程的调用改变圆半径的大小
package com.example.android_pointstouch; import android.content.Context; import android.content.Intent; import android.graphics.Canvas; import android.graphics.Paint; import android.os.Handler; import android.os.Message; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.view.View; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Created by Administrator on 2017/7/9 0009. */ public class MyButton extends View { private List<Circle> circleList=new ArrayList<>(); private float x; private float y; private int indexid; int i=0; public MyButton(Context context) { super(context); } public MyButton(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } public MyButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public MyButton(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(); paint.setStyle(Paint.Style.STROKE); for (Circle circle : circleList) { circle.r+=10; circle.drawSelf(canvas,paint); } } //触摸 @Override public boolean onTouchEvent(MotionEvent event) { int action = event.getAction(); //手指的下标Index int pointIndex = action >> 8; int action_code= action&0xff; x = event.getX(pointIndex);//X轴 y = event.getY(pointIndex);//Y轴 indexid = event.getPointerId(pointIndex);//触摸的下标 // Log.i("text","pointIndex="+pointIndex+"action_code="+action_code+"indexid="+indexid); if(action_code>=5){ action_code-=5; } switch (action_code){ case MotionEvent.ACTION_DOWN://点击 i++; Circle circle=new Circle(x,y,0,indexid);//进来一次就NEW一个对象 circleList.add(circle);//加入集合 new my().start();//启动线程 break; case MotionEvent.ACTION_UP://移开 //circleList.remove(delete(indexid)); break; case MotionEvent.ACTION_MOVE://移动 /* for (int i = 0; i <event.getPointerCount() ; i++) { int id= event.getPointerId(i); delete(id).x=event.getX(i); delete(id).y=event.getY(i); }*/ break; } invalidate(); return true; } //移除 public Circle delete(int indexid){ for (Circle circle : circleList) { if(circle.pointId==indexid){ return circle; } } return null; } //线程 改变圆大小 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } class my extends Thread{ @Override public void run() { for (int i = 0; i <10; i++) { try { sleep(110); postInvalidate();//回调 } catch (InterruptedException e) { e.printStackTrace(); } if(i==9){ circleList.remove(0); } } } } }
加载全部内容