網站首頁 編程語言 正文
本文實例為大家分享了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
相關推薦
- 2023-04-24 NumPy矩陣乘法的實現_python
- 2022-07-25 C++文件的操作及小實驗示例代碼詳解_C 語言
- 2024-04-01 使用Vite安裝TailwindCSS
- 2022-07-26 論網頁檢查preview和response一個是漢字一個是亂碼怎么解決
- 2023-10-26 解決:NODE_ENV 不是內部或外部命令,也不是可運行的程序,或者批處理文件
- 2022-08-28 go?zero微服務實戰處理每秒上萬次的下單請求_Golang
- 2023-02-12 Jupyter?Notebook運行代碼無反應問題及解決方法_python
- 2022-09-19 Android自定義對話框的簡單實現_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支