本文實例講述了Android編程開辟之多點觸摸(Multitouch)完成辦法。分享給年夜家供年夜家參考,詳細以下:
假如您對開辟多點觸摸法式感興致的話,那末本文將是一個很好的開端,Android運用法式開辟中,多點觸摸不是那末高不可攀,完成起來也很簡略,本例只須要兩個類就可以完成多點觸摸。
起首來看看我們的視圖類MTView.java:
package com.ideasAndroid.demo; 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; 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) { // 取得屏幕觸點數目 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); } /* * 進入法式時配景畫成黑色,然後把“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) { } }
代碼中都做了響應的正文,這裡就不多說。
接上去看看我們的Activity,MultitouchVisible.java
package com.ideasandroid.demo; import android.app.Activity; import android.os.Bundle; import android.view.Window; import android.view.WindowManager; public class MultitouchVisible extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //隱蔽題目欄 requestWindowFeature(Window.FEATURE_NO_TITLE); //設置玉成屏 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); //設置為下面的MTView setContentView(new MTView(this)); } }
願望本文所述對年夜家Android法式設計有所贊助。
【IOS開辟之tableView點擊行跳轉並帶有“顯示”更多功效】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!