編輯:高級開發
我們曾就《android手勢識別VIEwFlipper觸摸動畫》做過詳細的講解,其實,android應用程序開發中,多點觸摸(Multitouch)不是那麼遙不可及,實現起來也很簡單。如果您對開發多點觸摸程序感興趣的話,那麼本文將是一個很好的開始,本例只需要兩個類就能實現多點觸摸。
首先來看看我們的視圖類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, intwidth,
- 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));
- }
- }
希望本文對您有所幫助。
droid 的擴展工程包含在external文件夾中,是一些經過修改後適應android系統的開源工程。有一些工程在主機上運行,也有些在目標機上運行。由於包含的擴展工程
用戶卻不會喜愛它們。在移動應用中,簡潔明快才是用戶希望看到的。試想一下,當用戶在手機上玩一個RPG游戲,並被華麗的3D效果充斥了整個界面,那麼他將完全無法著手進行下一個
下面文章著重講解界面的android UI部分,基本的操作大家還是了解的,把整個屏幕看做一個二維數組,每一個元素可以視為一個方塊,因此每個方格在游戲進行過程中可以處於不
android最近很火爆,這對android開發者來說對開發的壓力增大。為了更好的幫助廣大android開發者,Sprint將提供網絡資源支持。一般而言,手機操作系統廠