Android實現(xiàn)檢測手機多點觸摸點數(shù)
本文實例為大家分享了Android檢測手機多點觸摸點數(shù)的具體代碼,供大家參考,具體內(nèi)容如下
說明:手指每點擊一個地方,在那個地方就畫一個圓
第一種方式:
效果圖:
Java代碼:
首先我們要寫一個繪圓類
package com.example.myapplication; ? /** ?* Created by Administrator on 2017/7/9 0009. ?*/ ? import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; ? import java.util.Random; ? /** ?* 一個圓類 ?*/ public class Circle { ? ? ? public float x; ? ? ? ? ? //圓心點的x坐標 ? ? public float y; ? ? ? ? ? //圓心點的y坐標 ? ? public int r=100; ? ? ? ? //圓的半徑 ? ? public int pointId; ? ? ?//員的下標 ? ? //初始化顏色 ? ? int red; ? ? int green; ? ? int blue; ? ? Random random=new Random();//初始化隨機數(shù) ? ? ? public Circle(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.setColor(Color.rgb(red,green,blue)); ? ? ? ? canvas.drawCircle(x,y,r,paint); ? ? } ? }
然后有一個自定義的控件類
package com.example.myapplication; ? import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.view.SurfaceView; import android.view.View; ? import java.util.ArrayList; import java.util.List; ? /** ?* Created by Administrator on 2017/7/9 0009. ?*/ ? public class MyView extends View { ?? ? ? //定義個圓的集合 ? ? private 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(); ? ? ? ? for (Circle circle : circles) { ? ? ? ? ? ? circle.drawSelf(canvas,paint); ? ? ? ? } ? ? } ? ? ? @Override ? ? public boolean onTouchEvent(MotionEvent event) { ? ? ?//獲取手指的行為 ? ? ? ? int ?action=event.getAction(); ? ? ? ? int ?action_code=action&0xff; ? ? ?//手指的下標index ? ? ? ? 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; ? ? ? ? } ? ? ? ? ? ? //單點觸摸時用action判斷 ? ? ? ? //多點觸摸時用action_code判斷 ? ? ?switch (action_code){ ? ? ? ? ?case MotionEvent.ACTION_DOWN: //按下 ? ? ? ? ? ? ?//實例化圓 ? ? ? ? ? ? ?Circle circle=new Circle(x,y,pointId); ? ? ? ? ? ? ?//將圓添加到集合中 ? ? ? ? ? ? ?circles.add(circle); ? ? ? ? ? ? ?break; ? ? ? ? ?case MotionEvent.ACTION_UP: ? //抬起 ? ? ? ? ? ? ?//找到具體的圓將它從集合中移除即可 ? ? ? ? ? ? ?circles.remove(get(pointId)); ? ? ? ? ? ? ?break; ? ? ? ? ?case MotionEvent.ACTION_MOVE: //移動 ? ? ? ? ? ? //找到具體的圓,時時修改圓心點坐標即可 ? ? ? ? ? ? ?for (int i = 0; i <event.getPointerCount(); i++) { ? ? ? ? ? ? ? ? ?int id=event.getPointerId(i);//得打具體圓的id ? ? ? ? ? ? ? ? ?//從新給圓賦值圓心點坐標 ? ? ? ? ? ? ? ? ?get(id).x=event.getX(i); ? ? ? ? ? ? ? ? ?get(id).y=event.getY(i); ? ? ? ? ? ? ?} ? ? ? ? ? ? ?break; ? ? ?} ? ? ? ?//重新調(diào)用onDraw 重繪 ? ? ? invalidate(); ? ? ? ? //子線程中重新繪制 postInvalidate(); ? ? ? ? return true; } ? //定義一個方法,通過pointId返回具體的圓 ? ? public Circle get(int pointId){ ? ? ? ? ? for (Circle circle : circles) { ? ? ? ? ? ? if(circle.pointId==pointId){ ? ? ? ? ? ? ? ? return ?circle; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? return ?null; ? ? } }
使用的話,就在Activity的setContentView(new MTView); 或者在XML文件中以控件的形式
第二種方式:
效果圖:
寫一個Java類
package com.example.myapplication; ? import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceView; ? /** ?* Created by Administrator on 2017/7/9 0009. ?*/ ? public class MTView extends SurfaceView implements SurfaceHolder.Callback{ ? ? private static final int MAX_TOUCHPOINTS = 10; ? ? private static final String START_TEXT = "請隨便觸摸屏幕進行測試"; ? ? private Paint textPaint = new Paint(); ? ? private Paint touchPaints[] = new Paint[MAX_TOUCHPOINTS]; ? ? private int colors[] = new int[MAX_TOUCHPOINTS]; ? ? ? private int width, height; ? ? private float scale = 1.0f; ? ? ? public MTView(Context context) { ? ? ? ? super(context); ? ? ? ? SurfaceHolder holder = getHolder(); ? ? ? ? holder.addCallback(this); ? ? ? ? setFocusable(true); // 確保我們的View能獲得輸入焦點 ? ? ? ? setFocusableInTouchMode(true); // 確保能接收到觸屏事件 ? ? ? ? init(); ? ? } ? ?private void init(){ ? ? ? ?// 初始化10個不同顏色的畫筆 ? ? ? ?textPaint.setColor(Color.WHITE); ? ? ? ?colors[0] = Color.BLUE; ? ? ? ?colors[1] = Color.RED; ? ? ? ?colors[2] = Color.GREEN; ? ? ? ?colors[3] = Color.YELLOW; ? ? ? ?colors[4] = Color.CYAN; ? ? ? ?colors[5] = Color.MAGENTA; ? ? ? ?colors[6] = Color.DKGRAY; ? ? ? ?colors[7] = Color.WHITE; ? ? ? ?colors[8] = Color.LTGRAY; ? ? ? ?colors[9] = Color.GRAY; ? ? ? ?for (int i = 0; i < MAX_TOUCHPOINTS; i++) { ? ? ? ? ? ?touchPaints[i] = new Paint(); ? ? ? ? ? ?touchPaints[i].setColor(colors[i]); ? ? ? ?} ? ?} ? ? ? /** ? ? ?* 處理觸屏事件 ? ? ?*/ ? ? @Override ? ? public boolean onTouchEvent(MotionEvent event) { ? ? ? ? // 獲得屏幕觸點數(shù)量 ? ? ? ? int pointerCount = event.getPointerCount(); ? ? ? ? if (pointerCount > MAX_TOUCHPOINTS) { ? ? ? ? ? ? pointerCount = MAX_TOUCHPOINTS; ? ? ? ? } ? ? ? ? ? // 鎖定Canvas,開始進行相應的界面處理 ? ? ? ? Canvas c = getHolder().lockCanvas(); ? ? ? ? if (c != null) { ? ? ? ? ? ? c.drawColor(Color.BLACK); ? ? ? ? ? ? if (event.getAction() == MotionEvent.ACTION_UP) { ? ? ? ? ? ? ? ? // 當手離開屏幕時,清屏 ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? // 在每一個觸點上繪制一個十字和坐標信息 ? ? ? ? ? ? ? ? for (int i = 0; i < pointerCount; i++) { ? ? ? ? ? ? ? ? ? ? int id = event.getPointerId(i); ? ? ? ? ? ? ? ? ? ? int x = (int) event.getX(i); ? ? ? ? ? ? ? ? ? ? int y = (int) event.getY(i); ? ? ? ? ? ? ? ? ? ? drawCrosshairsAndText(x, y, touchPaints[id], i, id, c); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? // 在每一個觸點上繪制一個圓 ? ? ? ? ? ? ? ? for (int i = 0; i < pointerCount; i++) { ? ? ? ? ? ? ? ? ? ? int id = event.getPointerId(i); ? ? ? ? ? ? ? ? ? ? int x = (int) event.getX(i); ? ? ? ? ? ? ? ? ? ? int y = (int) event.getY(i); ? ? ? ? ? ? ? ? ? ? drawCircle(x, y, touchPaints[id], c); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? ? // 畫完后,unlock ? ? ? ? ? ? getHolder().unlockCanvasAndPost(c); ? ? ? ? } ? ? ? ? return true; ? ? } ? ? ? /** ? ? ?* 畫十字及坐標信息 ? ? ?* ? ? ?* @param x ? ? ?* @param y ? ? ?* @param paint ? ? ?* @param ptr ? ? ?* @param id ? ? ?* @param c ? ? ?*/ ? ? private void drawCrosshairsAndText(int x, int y, Paint paint, int ptr, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?int id, Canvas c) { ? ? ? ? c.drawLine(0, y, width, y, paint); ? ? ? ? c.drawLine(x, 0, x, height, paint); ? ? ? ? int textY = (int) ((15 + 20 * ptr) * scale); ? ? ? ? c.drawText("x" + ptr + "=" + x, 10 * scale, textY, textPaint); ? ? ? ? c.drawText("y" + ptr + "=" + y, 70 * scale, textY, textPaint); ? ? ? ? c.drawText("id" + ptr + "=" + id, width - 55 * scale, textY, textPaint); ? ? } ? ? /** ? ? ?* 畫圓 ? ? ?* ? ? ?* @param x ? ? ?* @param y ? ? ?* @param paint ? ? ?* @param c ? ? ?*/ ? ? private void drawCircle(int x, int y, Paint paint, Canvas c) { ? ? ? ? c.drawCircle(x, y, 40 * scale, paint); ? ? } ? ? ? @Override ? ? public void surfaceCreated(SurfaceHolder holder) { ? ? ? } ? ? /** ? ? ?* 進入程序時背景畫成黑色,然后把START_TEXT寫到屏幕 ? ? ?*/ ? ? @Override ? ? public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { ? ? ? ? this.width = width; ? ? ? ? this.height = height; ? ? ? ? if (width > height) { ? ? ? ? ? ? this.scale = width / 480f; ? ? ? ? } else { ? ? ? ? ? ? this.scale = height / 480f; ? ? ? ? } ? ? ? ? textPaint.setTextSize(14 * scale); ? ? ? ? Canvas c = getHolder().lockCanvas(); ? ? ? ? if (c != null) { ? ? ? ? ? ? c.drawColor(Color.BLACK); ? ? ? ? ? ? float tWidth = textPaint.measureText(START_TEXT); ? ? ? ? ? ? c.drawText(START_TEXT, width / 2 - tWidth / 2, height / 2, textPaint); ? ? ? ? ? ? getHolder().unlockCanvasAndPost(c); ? ? ? ? } ? ? } ? ? ? @Override ? ? public void surfaceDestroyed(SurfaceHolder holder) { ? ? ? } ? }
使用的話,就在Activity的setContentView(new MTView);
一個圓逐漸擴大半徑
Java 代碼
package com.example.myapplication; ? /** ?* Created by Administrator on 2017/7/9 0009. ?*/ ? import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; ? import java.util.Random; ? /** ?* 一個圓類 ?*/ public class Circle { ? ? ? public MyView myView; ? ? public float x; ? ? ? ? ? //圓心點的x坐標 ? ? public float y; ? ? ? ? ? //圓心點的y坐標 ? ? public int r; ? ? ? ? //圓的半徑 ? ? public int pointId; ? ? ?//員的下標 ? ? //初始化顏色 ? ? int red; ? ? int green; ? ? int blue; ? ? Random random=new Random();//初始化隨機數(shù) ? ? ? public Circle(float x,float y,int r,int pointId){ ? ? ? ? this.x=x; ? ? ? ? this.y=y; ? ? ? ? this.r=r; ? ? ? ? this.pointId=pointId; ? ? ? ? red=random.nextInt(255); ? ? ? ? green=random.nextInt(255); ? ? ? ? blue=random.nextInt(255); ? ? } ? ? public void drawSelf(Canvas canvas, Paint paint){ ? ? ? ? paint.setColor(Color.rgb(red,green,blue)); ? ? ? ? paint.setStyle(Paint.Style.STROKE); ? ? ? ? canvas.drawCircle(x,y,r,paint); ? ? } }
自定義控件類
package com.example.myapplication; ? import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.view.SurfaceView; import android.view.View; ? import java.util.ArrayList; import java.util.List; ? /** ?* Created by Administrator on 2017/7/9 0009. ?*/ ? public class MyView extends View { ? ? ? //定義個圓的集合 ? ? private List<Circle> circles=new ArrayList<>(); ? ? //定義個一個全局變量 ? ? int pointId; ? ? float X; ? ? float Y; ? ? int r=10; ? ? ? 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 (Circle circle : circles) { ? ? ? ? ? ? circle.drawSelf(canvas,paint); ? ? ? ? } ? ? } ? ? @Override ? ? public boolean onTouchEvent(MotionEvent event) { ? ? ? ? //獲取手指的行為 ? ? ? ? int ?action=event.getAction(); ? ? ? ? int ?action_code=action&0xff; ? ? ? ? //手指的下標index ? ? ? ? int pointIndex=action>>8; ? ? ? ? //獲取手值的坐標 ? ? ? ? float x=event.getX(pointIndex); ? ? ? ? ? ? float y=event.getY(pointIndex); ? ? ? ? X=x; ? ? ? ? Y=y; ? ? ? ? Log.i("aaa","X :"+X+"Y :"+Y); ? ? ? ? //獲取手值的名字(ID) ? ? ? ?pointId=event.getPointerId(pointIndex); ? ? ? ? Log.i("mmmm","pointId"+pointId); ? ? ? ? if(action_code>=5){ ? ? ? ? ? ? action_code-=5; ? ? ? ? } ? ? ? ? //單點觸摸時用action判斷 ? ? ? ? //多點觸摸時用action_code判斷 ? ? ? ? switch (action_code){ ? ? ? ? ? ? case MotionEvent.ACTION_DOWN: //按下后 // ? ? ? ? ? ? ? ?//實例化圓 // ? ? ? ? ? ? ? ?Circle circle=new Circle(x,y,r,pointId); // ? ? ? ? ? ? ? ?//將圓添加到集合中 // ? ? ? ? ? ? ? ?circles.add(circle); ? ? ? ? ? ? ? ? //調(diào)用線程控制雨滴 ? ? ? ? ? ? ? ? new MyThread().start(); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case MotionEvent.ACTION_UP: ? //抬起 ? ? ? ? ? ? ? ? //找到具體的圓將它從集合中移除即可 ? ? ? ? ? ? ? // ?circles.remove(get(pointId)); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case MotionEvent.ACTION_MOVE: //移動 ? ? ? ? ? ? ? ? //找到具體的圓,時時修改圓心點坐標即可 // ? ? ? ? ? ? ? ?for (int i = 0; i <event.getPointerCount(); i++) { // ? ? ? ? ? ? ? ? ? ?int id=event.getPointerId(i);//得打具體圓的id // ? ? ? ? ? ? ? ? ? ?//從新給圓賦值圓心點坐標 // ? ? ? ? ? ? ? ? ? ?get(id).x=event.getX(i); // ? ? ? ? ? ? ? ? ? ?get(id).y=event.getY(i); // ? ? ? ? ? ? ? ?} ? ? ? ? ? ? ? ? break; ? ? ? ? } ? ? ? ? ? //重新調(diào)用onDraw 重繪 ? ? ? ?// invalidate(); ? ? ? ? //子線程中重新繪制 postInvalidate(); ? ? ? ? return true; ? ? } ? ? ? //定義一個方法,通過pointId返回具體的圓 ? ? public Circle get(int pointId){ ? ? ? ? for (Circle circle : circles) { ? ? ? ? ? ? if(circle.pointId==pointId){ ? ? ? ? ? ? ? ? return ?circle; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? return ?null; ? ? } ? ? ? class MyThread extends ?Thread{ ? ? ? ? @Override ? ? ? ? public void run() { ? ? ? ? ? ? //實例化圓(x y就是手值的坐標 r是默認大小,手指下標) ? ? ? ? ? ? Circle circle=new Circle(X,Y,r,pointId); ? ? ? ? ? ? //將圓添加到集合中 ? ? ? ? ? ? circles.add(circle); ? ? ? ? ? ? //執(zhí)行下降 ? ? ? ? ? ? while(true){ ? ? ? ? ? ? ? ? //拿到圓的圓心個 ? ? ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? ? ? System.out.println("子線程名稱:::"+Thread.currentThread().getName()); ? ? ? ? ? ? ? ? ? ? Circle c=get(pointId); //得到剛剛畫的圓 ? ? ? ? ? ? ? ? ? ? c.x=X; ? ? ? ? ? ? ? ? ? // ?c.y=c.y+10; ? ? ? ? ? ? ? ? ? ? c.r=c.r+5; ? ? ? ? ? ? ? ? ? ? Log.i("bbbb"," X坐標"+c.x+"Y坐標 "+c.y); ? ? ? ? ? ? ? ? ? ? sleep(200); ?//20毫秒 ? ? ? ? ? ? ? ? ? ?postInvalidate(); ? ? ? ? ? ? ? ? } catch (InterruptedException e) { ? ? ? ? ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? } ? ? ? ? ? } ? ? } }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android仿淘寶頭條基于TextView實現(xiàn)上下滾動通知效果
這篇文章主要介紹了Android TextView實現(xiàn)上下滾動通知效果,需要的朋友可以參考下2017-03-03android開發(fā)之橫向滾動/豎向滾動的ListView(固定列頭)
由于項目需要,我們需要一個可以橫向滾動的,又可以豎向滾動的 表格;經(jīng)過幾天的研究終于搞定,感興趣的朋友可以了解下哦2013-01-01詳解androidstudio項目上傳到github方法以及步驟
在使用studio開發(fā)的項目過程中有時候我們想將項目發(fā)布到github上,studio其實是自帶這種功能的,那么如何使用呢,下面我們就一起來了解一下2019-01-01詳解Android中通過Intent類實現(xiàn)組件間調(diào)用的方法
Intent能夠實現(xiàn)應用間的數(shù)據(jù)交互與通訊,將實現(xiàn)者和調(diào)用者解耦,接下來就來詳解Android中通過Intent類實現(xiàn)組件間調(diào)用的方法,需要的朋友可以參考下2016-05-05