網(wǎng)站首頁 編程語言 正文
本文實例為大家分享了Android檢測手機多點觸摸點數(shù)的具體代碼,供大家參考,具體內(nèi)容如下
多點觸摸點數(shù)效果圖
Circle.java
package com.zking.administrator.g160628_android19_pointstouch;
?
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
?
import java.util.Random;
?
/**
?* Created by Administrator on 2017/7/9.
?*/
?
public class Circle {
? ? public float x;
? ? public float y;
? ? public ?int r=100;//半徑
? ? public int id;//手指的名字
? ? //三原色(每一個int類型的取值都是255)
? ? int red;
? ? int green;
? ? int blue;
? ? //隨機數(shù)
? ? Random random=new Random();
?
? ? public Circle(float x, float y, int id) {
? ? ? ? this.x = x;
? ? ? ? this.y = y;
? ? ? ? this.id = id;
? ? ? ? red=random.nextInt(255);
? ? ? ? green=random.nextInt(255);
? ? ? ? blue=random.nextInt(255);
? ? }
?
? ? //畫自己
? ? public void drawSelf(Canvas canvas, Paint paint){
? ? ? ? //設(shè)置顏色隨機
? ? ? ? paint.setColor(Color.rgb(red,green,blue));
? ? ? ? canvas.drawCircle(x,y,r,paint);
? ? }
}
MainActivity.java
package com.zking.administrator.g160628_android19_pointstouch;
?
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
?
public class MainActivity extends AppCompatActivity {
?
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(new MyView(this));
? ? }
}
MyView.java
package com.zking.administrator.g160628_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 Administrator on 2017/7/9.
?*/
?
public class MyView extends View{
? ? //全局變量(單點觸摸)
// ? ?private float x;
// ? ?private float y;
// ? ?private int r=100;
?
? ? //定義圓的集合
? ? List<Circle> circles=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();
? ? ? ? //畫圓(單點觸摸)
// ? ? ? ?canvas.drawCircle(x,y,r,paint);
? ? ? ? for (Circle circle : circles) {
? ? ? ? ? ? //把畫布和畫筆傳進(jìn)去(運行第一次進(jìn)入什么都沒有,所有默認(rèn)的圓也就沒了)
? ? ? ? ? ? circle.drawSelf(canvas,paint);
? ? ? ? }
? ? }
?
? ? @Override
? ? public boolean onTouchEvent(MotionEvent event) {
? ? ? ? ?//獲取手指的行為
? ? ? ? int action=event.getAction();
? ? ? ? int action_code=action&0xff;
? ? ? ? //手指的下標(biāo)Index
? ? ? ? int pointIndex=action>>8;//右移8
?
? ? ? ? //獲取手指的坐標(biāo)
? ? ? ? float x=event.getX(pointIndex);
? ? ? ? float y=event.getY(pointIndex);
? ? ? ? //獲取手指的名字(id)
? ? ? ? int id=event.getPointerId(pointIndex);
?
? ? ? ? if(action_code>=5){
? ? ? ? ? ? action_code-=5;
? ? ? ? }
?
? ? ? ? switch (action_code){//action_code單點觸摸是012,多點觸摸562
? ? ? ? ? ? //case 5://多點觸摸的按下時5
? ? ? ? ? ? case MotionEvent.ACTION_DOWN://0按下
? ? ? ? ? ? ? ? //實例化圓
? ? ? ? ? ? ? ? Circle circle=new Circle(x,y,id);
? ? ? ? ? ? ? ? //將圓添加到集合中
? ? ? ? ? ? ? ? circles.add(circle);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case MotionEvent.ACTION_UP://1抬起
? ? ? ? ? ? ? ? //調(diào)用拿圓的方法(拿到是哪個圓我集合就移除,然后重新繪制)
? ? ? ? ? ? ? ? circles.remove(get(id));
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case MotionEvent.ACTION_MOVE://2移動
? ? ? ? ? ? ? ? //拿到所有手指的數(shù)量(循環(huán)所有的圓拿到他的ID,然后用現(xiàn)在的圓,給每一個圓的xy重新賦值)
? ? ? ? ? ? ? ? for (int i = 0; i <event.getPointerCount() ; i++) {
? ? ? ? ? ? ? ? ? ? //根據(jù)下標(biāo)拿到ID
? ? ? ? ? ? ? ? ? ? int did=event.getPointerId(i);
? ? ? ? ? ? ? ? ? ? //根據(jù)ID拿到新的圓(i就是當(dāng)前手指的下標(biāo),因為我們是根據(jù)下標(biāo)去拿xy)
? ? ? ? ? ? ? ? ? ? get(did).x=event.getX(i);
? ? ? ? ? ? ? ? ? ? get(did).y=event.getY(i);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? //重新調(diào)用onDraw 重繪 在主線程調(diào)用
? ? ? ? invalidate();
? ? ? ? return true;
? ? }
?
? ? //這個方法的目的就是拿圓
? ? public Circle get(int id){
? ? ? ? for (Circle circle : circles) {
? ? ? ? ? ? //判斷(拿到每一個圓的id等等于你傳過來的ID,如果是你想要得圓就return過去)
? ? ? ? ? ? if(circle.id==id){
? ? ? ? ? ? ? ? return circle;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return null;
? ? }
}
原文鏈接:https://blog.csdn.net/XHui_Lin/article/details/74900697
相關(guān)推薦
- 2023-01-18 Python中的裝飾器使用_python
- 2022-06-28 python神經(jīng)網(wǎng)絡(luò)學(xué)習(xí)利用PyTorch進(jìn)行回歸運算_python
- 2022-02-18 取一個數(shù)組中想要的值,放到一個新的對象里
- 2022-12-06 nginx?pod?hook鉤子優(yōu)雅關(guān)閉示例詳解_nginx
- 2023-02-25 一文搞懂Python中is和==的區(qū)別_python
- 2022-04-02 C語言對冒泡排序進(jìn)行升級介紹_C 語言
- 2023-01-05 C#不提升自己程序的權(quán)限實現(xiàn)操作注冊表_C#教程
- 2023-07-04 Spring中@Transactional注解事務(wù)傳播行為propagation參數(shù)說明
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支