android實(shí)現(xiàn)多點(diǎn)觸摸應(yīng)用
本文實(shí)例為大家分享了android實(shí)現(xiàn)多點(diǎn)觸摸應(yīng)用的具體代碼,供大家參考,具體內(nèi)容如下
JhkMultiTouchActivity.java
package com.android.forlinx; ? import android.app.Activity; import android.os.Bundle; import android.view.Window; import android.view.WindowManager; ? public class JhkMultiTouchActivity extends Activity { ? ? /** Called when the activity is first created. */ ? ? @Override ? ? public void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ?// ? setContentView(R.layout.main); ? ? ? ?? ? ? ? //隱藏標(biāo)題欄 ? ? ? ? ? ? ? ? ? requestWindowFeature(Window.FEATURE_NO_TITLE); ? ? ? ? ? ? ? ? //設(shè)置成全屏 ? ? ? ? ? ? ? ? ? getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, ? ? ? ? ? ? ? ? ? ? ? ? WindowManager.LayoutParams.FLAG_FULLSCREEN); ? ? ? ? ? ? ? ? //設(shè)置為上面的MTView ? ? ? ? ? ? ? ? setContentView(new MTView(this)); ? ? ? } }
MTView.java
package com.android.forlinx; ? ? import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Typeface; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceView; ? public class MTView extends SurfaceView implements SurfaceHolder.Callback { ? ?? ?private static final int MAX_TOUCHPOINTS = 10; ?? ?private static final String START_TEXT = "請(qǐng)隨便觸摸屏幕進(jìn)行測(cè)試"; ?? ?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能獲得輸入焦點(diǎn) ?? ??? ?setFocusableInTouchMode(true); // 確保能接收到觸屏事件 ?? ??? ?init(); ?? ?} ? ?? ?private void init() { ?? ??? ?// 初始化10個(gè)不同顏色的畫筆 ?? ??? ?textPaint.setColor(Color.GREEN); ?? ??? ?textPaint.setTypeface(null); ?? ??? ?textPaint.setAlpha(200); ?? ??? ?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]); ?? ??? ??? ?touchPaints[i].setAlpha(50); ?? ??? ?} ?? ?} ? ?? ?/* ?? ? * 處理觸屏事件 ?? ? */ ?? ?@Override ?? ?public boolean onTouchEvent(MotionEvent event) { ?? ??? ?// 獲得屏幕觸點(diǎn)數(shù)量 ?? ??? ?int pointerCount = event.getPointerCount(); ?? ??? ?if (pointerCount > MAX_TOUCHPOINTS) { ?? ??? ??? ?pointerCount = MAX_TOUCHPOINTS; ?? ??? ?} ?? ??? ? ?? ??? ?// 鎖定Canvas,開始進(jìn)行相應(yīng)的界面處理 ?? ??? ?Canvas c = getHolder().lockCanvas(); ?? ??? ?if (c != null) { ?? ??? ??? ?c.drawColor(Color.BLACK); ?? ??? ??? ?if (event.getAction() == MotionEvent.ACTION_UP) { ?? ??? ??? ??? ?// 當(dāng)手離開屏幕時(shí),清屏 ?? ??? ??? ?} else { ?? ??? ??? ??? ?// 先在屏幕上畫一個(gè)十字,然后畫一個(gè)圓 ?? ??? ??? ??? ?for (int i = 0; i < pointerCount; i++) { ?? ??? ??? ??? ??? ?// 獲取一個(gè)觸點(diǎn)的坐標(biāo),然后開始繪制 ?? ??? ??? ??? ??? ?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; ?? ?} ? ?? ?/** ?? ? * 畫十字及坐標(biāo)信息 ?? ? * ?? ? * @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); ?? ?} ? ?? ?/* ?? ? * 進(jìn)入程序時(shí)背景畫成黑色,然后把“START_TEXT”寫到屏幕 ?? ? */ ?? ?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); ?? ??? ?} ?? ?} ? ?? ?public void surfaceCreated(SurfaceHolder holder) { ?? ?} ? ?? ?public void surfaceDestroyed(SurfaceHolder holder) { ?? ?} ? }
效果圖
以上就是本文的全部內(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檢測(cè)手機(jī)多點(diǎn)觸摸點(diǎn)數(shù)的方法
- Android實(shí)現(xiàn)檢測(cè)手機(jī)多點(diǎn)觸摸點(diǎn)數(shù)
- android實(shí)現(xiàn)多點(diǎn)觸摸效果
- Android實(shí)現(xiàn)多點(diǎn)觸摸操作
相關(guān)文章
Android PopupWindow全屏詳細(xì)介紹及實(shí)例代碼
這篇文章主要介紹了 Android PopupWindow全屏詳細(xì)介紹及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-12-12Android使用viewpager實(shí)現(xiàn)畫廊式效果
這篇文章主要為大家詳細(xì)介紹了Android使用viewpager實(shí)現(xiàn)畫廊式效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08Android滑動(dòng)優(yōu)化高仿QQ6.0側(cè)滑菜單(滑動(dòng)優(yōu)化)
之前的實(shí)現(xiàn)只是簡單的可以顯示和隱藏左側(cè)的菜單,但是特別生硬,而且沒有任何平滑的趨勢(shì),那么今天就來優(yōu)化一下吧,加上平滑效果,而且可以根據(jù)手勢(shì)滑動(dòng)的方向來判斷是否是顯示和隱藏2016-02-02深入解讀Android開發(fā)中Activity的生命周期
這篇文章主要介紹了Android開發(fā)中Activity的生命周期,包括Activity的停止和銷毀等重要內(nèi)容,非常推薦!需要的朋友可以參考下2015-12-12Android編程簡單解析JSON格式數(shù)據(jù)的方法示例
這篇文章主要介紹了Android編程簡單解析JSON格式數(shù)據(jù)的方法,結(jié)合實(shí)例形式分析了Android編程解析json格式數(shù)據(jù)的實(shí)現(xiàn)方法與相關(guān)操作技巧,需要的朋友可以參考下2017-08-08Android實(shí)現(xiàn)系統(tǒng)打印功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)系統(tǒng)打印功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12Android實(shí)現(xiàn)多級(jí)列表中的新建功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)多級(jí)列表中的新建功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06Android?autojs隨時(shí)翻譯剪貼板單詞實(shí)現(xiàn)示例
這篇文章主要為大家介紹了Android?autojs隨時(shí)翻譯剪貼板單詞,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09Android?音頻波形圖實(shí)現(xiàn)效果示例
這篇文章主要為大家介紹了Android?音頻波形圖實(shí)現(xiàn)效果示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08