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


猜你喜欢
- Main方法如下:static void Main(string[] args){ dynamic st
- 点击窗体任意位置移动窗体:需要添加命名空间:using System.Runtime.InteropServices;private con
- 一、需求C# 项目生成 dll,在反编译工具下,好比皇帝的新装,dll 内部的代码看的一清二楚,在这里推荐一个工具ConfuserEx,可以
- AuthenticationProvider解析首先进入到AuthenticationProvider源码中可以看到它只是个简单的接口里面也
- 1、什么是 IOC?IOC-Inversion of Control,即控制反转。它不是什么技术,而是一种设计思想。传统的创建对象的方法是直
- 1、项目启动时报错如下Description:The bean 'securityManager', defined in
- 本实例主要实现下面三个基本功能1、C#开发windows服务2、禁止QQ等程序运行3、为windows服务创建自动安装程序下面针对这三个基本
- 效果图片重写DataGridView的OnRowPostPaint方法或者直接在DataGridView的RowPostPaint事件里写,
- 我们今天来聊下如何做实时通讯(先给知识点,实现原理,最后给出实现实时通信的具体代码--使用工具 android studio)现在先说下用到
- 本文以案例形式分析了Android中TelephonyManager类的用法。分享给大家供大家参考。具体如下:目录结构:main.xml布局
- 装饰器模式概述装饰器模式(Decorator Pattern)也称为包装模式(Wrapper Pattern),属于结构型模式。它是指在不改
- 发展历史Gradle 的依赖管理是一个从开始接触 Android 开发就一直伴随着我们的问题(作者是Andro
- 一.概述在微服务框架中,一个由客户端发起的请求在后端系统中会经过多个不同的的服务节点调用来协同产生最后的请求结果,每一个前段请求都会形成一条
- 1、spring 框架解决字符串编码问题:过滤器 CharacterEncodingFilter(filter-name)2、在web.xm
- IDEA自定义pom依赖抽离公共代码,代码解耦,减少重复第一步: 抽离公共部分的代码第二步: 点击右侧工具栏的maven,刷新,点击skip
- 这篇文章主要介绍了如何使用HttpClient发送java对象到服务器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学
- Dialog的基本方法//创建DialogAlertDialog.Builder builder = new AlertDialog.Bui
- pom配置<?xml version="1.0" encoding="UTF-8"?>&
- 一、前言学习概述:学习四种不同类型的方法应用、方法被调用时的内存图、重载学习目标:熟练掌握方法的应用以及重载二、定义与调用1.概述定义:方法
- 同步器简介 学习以来对线程的操作有很大的改观,从c/c++的mute