Android檢測(cè)手機(jī)多點(diǎn)觸摸點(diǎn)數(shù)的方法
本文實(shí)例為大家分享了Android檢測(cè)手機(jī)多點(diǎn)觸摸點(diǎn)數(shù)的具體代碼,供大家參考,具體內(nèi)容如下
多點(diǎn)觸摸點(diǎn)數(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;//手指的名字 ? ? //三原色(每一個(gè)int類型的取值都是255) ? ? int red; ? ? int green; ? ? int blue; ? ? //隨機(jī)數(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è)置顏色隨機(jī) ? ? ? ? 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{ ? ? //全局變量(單點(diǎn)觸摸) // ? ?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(); ? ? ? ? //畫圓(單點(diǎn)觸摸) // ? ? ? ?canvas.drawCircle(x,y,r,paint); ? ? ? ? for (Circle circle : circles) { ? ? ? ? ? ? //把畫布和畫筆傳進(jìn)去(運(yù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單點(diǎn)觸摸是012,多點(diǎn)觸摸562 ? ? ? ? ? ? //case 5://多點(diǎn)觸摸的按下時(shí)5 ? ? ? ? ? ? case MotionEvent.ACTION_DOWN://0按下 ? ? ? ? ? ? ? ? //實(shí)例化圓 ? ? ? ? ? ? ? ? Circle circle=new Circle(x,y,id); ? ? ? ? ? ? ? ? //將圓添加到集合中 ? ? ? ? ? ? ? ? circles.add(circle); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case MotionEvent.ACTION_UP://1抬起 ? ? ? ? ? ? ? ? //調(diào)用拿圓的方法(拿到是哪個(gè)圓我集合就移除,然后重新繪制) ? ? ? ? ? ? ? ? circles.remove(get(id)); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case MotionEvent.ACTION_MOVE://2移動(dòng) ? ? ? ? ? ? ? ? //拿到所有手指的數(shù)量(循環(huán)所有的圓拿到他的ID,然后用現(xiàn)在的圓,給每一個(gè)圓的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),因?yàn)槲覀兪歉鶕?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; ? ? } ? ? ? //這個(gè)方法的目的就是拿圓 ? ? public Circle get(int id){ ? ? ? ? for (Circle circle : circles) { ? ? ? ? ? ? //判斷(拿到每一個(gè)圓的id等等于你傳過來的ID,如果是你想要得圓就return過去) ? ? ? ? ? ? if(circle.id==id){ ? ? ? ? ? ? ? ? return circle; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? return null; ? ? } }
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 解析Android開發(fā)中多點(diǎn)觸摸的實(shí)現(xiàn)方法
- android 多點(diǎn)觸摸圖片縮放的具體實(shí)現(xiàn)方法
- Android實(shí)現(xiàn)手勢(shì)滑動(dòng)多點(diǎn)觸摸放大縮小圖片效果
- Android實(shí)現(xiàn)手勢(shì)滑動(dòng)多點(diǎn)觸摸縮放平移圖片效果
- Android實(shí)現(xiàn)手勢(shì)滑動(dòng)多點(diǎn)觸摸縮放平移圖片效果(二)
- Android實(shí)現(xiàn)手機(jī)多點(diǎn)觸摸畫圓
- Android實(shí)現(xiàn)檢測(cè)手機(jī)多點(diǎn)觸摸點(diǎn)數(shù)
- android實(shí)現(xiàn)多點(diǎn)觸摸效果
- Android實(shí)現(xiàn)多點(diǎn)觸摸操作
- android實(shí)現(xiàn)多點(diǎn)觸摸應(yīng)用
相關(guān)文章
Android使用GPS獲取用戶地理位置并監(jiān)聽位置變化的方法
這篇文章主要介紹了Android使用GPS獲取用戶地理位置并監(jiān)聽位置變化的方法,實(shí)例分析了Android編程中GPS定位的實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下2015-12-12Android實(shí)現(xiàn)開機(jī)自動(dòng)啟動(dòng)Service或app的方法
這篇文章主要介紹了Android實(shí)現(xiàn)開機(jī)自動(dòng)啟動(dòng)Service或app的方法,結(jié)合實(shí)例形式分析了Android開機(jī)自啟動(dòng)程序的具體步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-07-07Android自定義控件實(shí)現(xiàn)不規(guī)則區(qū)域點(diǎn)擊事件
這篇文章主要為大家詳細(xì)介紹了Android自定義控件實(shí)現(xiàn)不規(guī)則區(qū)域點(diǎn)擊事件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Android實(shí)現(xiàn)旋轉(zhuǎn)動(dòng)畫的兩種方式案例詳解
這篇文章主要介紹了Android實(shí)現(xiàn)旋轉(zhuǎn)動(dòng)畫的兩種方式,需要的朋友可以參考下2021-08-085步教你快速寫一個(gè)android Router路由框架
本篇文章主要介紹了5步教你快速寫一個(gè)Router路由框架(詳細(xì)步驟),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11ANDROID中自定義對(duì)話框AlertDialog使用示例
這篇文章主要為大家詳細(xì)介紹了Android中自定義對(duì)話框AlertDialog使用示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12Android編程之內(nèi)存溢出解決方案(OOM)實(shí)例總結(jié)
這篇文章主要介紹了Android編程之內(nèi)存溢出解決方案(OOM),結(jié)合實(shí)例實(shí)例總結(jié)分析了Android編程過程中常見的內(nèi)存溢出情況與對(duì)應(yīng)的解決方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11android應(yīng)用開發(fā)之spinner控件的簡單使用
Android的控件有很多種,其中就有一個(gè)Spinner的控件,這個(gè)控件其實(shí)就是一個(gè)下拉顯示列表。本文通過腳本之家平臺(tái)給大家介紹android應(yīng)用開發(fā)之spinner控件的簡單使用,感興趣的朋友可以參考下2015-11-11Android實(shí)現(xiàn)使用微信登錄第三方APP的方法
這篇文章主要介紹了Android實(shí)現(xiàn)使用微信登錄第三方APP的方法,結(jié)合實(shí)例形式分析了Android微信登錄APP的操作步驟與具體功能實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-11-11