Android实现手机多点触摸画圆

作者:宋峥清 时间:2022-03-23 06:23:16 

本文实例为大家分享了Android实现手机多点触摸画圆的具体代码,供大家参考,具体内容如下

静态效果图:(多个手指按下和抬起的状态)

Android实现手机多点触摸画圆

代码实现部分:

1、先写个实体类,设置相关的属性

package com.zking.laci.android19_pointstouch;
?
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
?
import java.util.Random;
?
/**
?* Created by Laci on 2017/7/9.
?*/
?
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;
?
/**
?* Created by Laci on 2017/7/9.
?*/
?
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));

最后打开真机测试就可以啦!

来源:https://blog.csdn.net/pang_ping/article/details/74905380

标签:Android,触摸,画圆
0
投稿

猜你喜欢

  • C#获取任务栏显示进程的方法

    2023-08-28 11:09:53
  • C#中out参数、ref参数与值参数的用法及区别

    2022-10-16 17:15:39
  • Android编程之DatePicker和TimePicke简单时间监听用法分析

    2022-07-04 00:42:24
  • 详解mysql插入数据后返回自增ID的七种方法

    2023-07-01 21:32:20
  • Android View移动的六种方法小结

    2023-07-06 02:43:39
  • Android监听Home键和Back键的区别介绍

    2022-09-11 11:21:03
  • Java 类与对象超基础讲解

    2023-06-12 00:03:22
  • java保证对象在内存中唯一性的实现方法

    2023-11-27 21:30:03
  • C#子线程更新UI控件的方法实例总结

    2022-08-31 15:40:23
  • Android提高Service优先级的方法分析

    2023-01-09 09:05:51
  • Java中多线程下载图片并压缩能提高效率吗

    2023-08-06 07:40:10
  • 浅谈java7增强的try语句关闭资源

    2022-04-29 18:57:29
  • java提取json中某个数组的所有值方法

    2022-04-25 16:02:04
  • C# protobuf自动更新cs文件

    2021-10-08 10:16:28
  • mybatis使用pagehelper插件过程详解

    2023-02-16 18:11:11
  • Spring JPA find分页示例详解

    2023-05-09 00:36:46
  • SpringCloudAlibaba整合Feign实现远程HTTP调用的简单示例

    2023-11-19 16:16:05
  • java获取包下被指定注解的类过程解析

    2023-08-08 11:12:27
  • spring boot org.junit.jupiter.api不存在的解决

    2023-07-11 18:34:16
  • C++超详细讲解贪心策略的设计及解决会场安排问题

    2022-07-26 12:08:04
  • asp之家 软件编程 m.aspxhome.com