本文实例为大家分享了Android实现手机多点触摸画圆的具体代码,供大家参考,具体内容如下 静态效果图:(多个手指按下和抬起的状态) 代码实现部分: 1、先写个实体类,设置相关的属
本文实例为大家分享了Android实现手机多点触摸画圆的具体代码,供大家参考,具体内容如下
静态效果图:(多个手指按下和抬起的状态)
代码实现部分:
1、先写个实体类,设置相关的属性
package com.zking.laci.android19_pointstouch;
import android.graphics.canvas;
import android.graphics.Color;
import android.graphics.Paint;
import java.util.Random;
public class MyCircle {
public float x;
public float y;
public int r=100;
public int pointId;
int red;
int green;
int blue;
Random random=new Random();
public MyCircle(float x, float y, int pointId) {
this.x = x;
this.y = y;
this.pointId = pointId;
red=random.nextInt(255);
green=random.nextInt(255);
blue=random.nextInt(255);
}
public void drawSelf(Canvas canvas, Paint paint){
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.rgb(red,green,blue));
canvas.drawCircle(x,y,r,paint);
}
}
2、然后我们自己再写个java类,用来画圆的
package com.zking.laci.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;
public class MyView extends View {
List<MyCircle> lt=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();
for (MyCircle myCircle : lt) {
myCircle.drawSelf(canvas,paint);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//获取手指的行为
int action=event.getAction();
int action_code=action&0xff;
//手指的下标
int pointIndex=action>>8;
//获取手指的坐标
float x=event.getX(pointIndex);
float y=event.getY(pointIndex);
//获取手指的名字的ID
int pointId=event.getPointerId(pointIndex);
if(action_code>=5){
action_code-=5;
}
switch (action_code) {
case MotionEvent.ACTION_DOWN:
//实例化园
MyCircle myCircle=new MyCircle(x,y,pointId);
//将园添加到集合中
lt.add(myCircle);
break;
case MotionEvent.ACTION_UP:
lt.remove(get(pointId));
break;
case MotionEvent.ACTION_MOVE:
for (int i = 0; i <event.getPointerCount() ; i++) {
int id=event.getPointerId(i);
get(id).x=event.getX(i);
get(id).y=event.getY(i);
}
break;
}
//重新调用onDraw 重绘
invalidate();
return true;
}
public MyCircle get(int pointId){
for (MyCircle myCircle : lt) {
if(myCircle.pointId==pointId){
return myCircle;
}
}
return null;
}
}
3、最后我们在activity中改一句代码就可以了
setContentView(new MyView(this));
最后打开真机测试就可以啦!
--结束END--
本文标题: Android实现手机多点触摸画圆
本文链接: https://lsjlt.com/news/149064.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0