本篇我們來看看在 Android 上的觸碰偵測,觸碰偵測的方法主要是由 onTouchEvent( Motion event) 來觸發,只要你使用的類別有這個方法,就能很簡單的使用,以上篇的 HelloWorld 為例,加以修改
MainActivity.java
   1:  package com.example.helloworld;
   2:   
   3:  import android.app.Activity;
   4:  import android.content.Context;
   5:  import android.graphics.Color;
   6:  import android.graphics.Typeface;
   7:  import android.os.Bundle;
   8:  import android.util.Log;
   9:  import android.view.Gravity;
  10:  import android.view.MotionEvent;
  11:  import android.widget.TextView;
  12:   
13: public class MainActivity extends Activity {
  14:      
15: static final String tLog = "Trace Log";
  16:      
  17:      TestTextView ttv;
  18:      
19: int dx;// get touch down x position
20: int dy;// get touch down y position
21: int mx;// get touch move x position
22: int my;// get touch move y position
  23:      
  24:      @Override
25: public void onCreate(Bundle savedInstanceState) {
  26:          
  27:          super.onCreate(savedInstanceState);
28: // setContentView(R.layout.activity_main);
  29:          
30: ttv = new TestTextView(this);
  31:          
32: ttv.setTextColor(0xff888888); // set color
33: ttv.setTextSize(18.0f); // set size
34: ttv.setTypeface(Typeface.SERIF);// set typeface
35: ttv.setText(R.string.copy_right);// change text
  36:          ttv.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
  37:          setContentView(ttv);
  38:   
  39:          
  40:      }
  41:      
42: public boolean onTouchEvent(MotionEvent event){
  43:          
44: int touchCount = event.getPointerCount(); // get touchcount
45: Log.v(MainActivity.tLog, ""+touchCount);
  46:          
47: switch(event.getAction()){
  48:          
49: case MotionEvent.ACTION_DOWN://touch down
  50:                  ttv.setTextColor(Color.RED);
51: dx = (int) event.getX();// get x position
52: dy = (int) event.getY();// get y position
53: break;
54: case MotionEvent.ACTION_UP://touch leave
  55:                  ttv.setTextColor(0xff888888);
56: break;
57: case MotionEvent.ACTION_MOVE://touch move
  58:                  
59: mx = (int) event.getX();// get x position
60: my = (int) event.getY();// get y position
  61:                  
62: if(Math.abs(dx-mx)>20 || Math.abs(dy-my)>20)
  63:                  {
  64:                      ttv.setTextColor(Color.GREEN);
  65:                  }
  66:                  
67: break;
  68:          }
  69:          
70: return true;
  71:      }
  72:    
  73:  }
  74:   
  75:   
76: class TestTextView extends TextView{
  77:   
78: public TestTextView(Context context) {
  79:          super(context);
80: // TODO Auto-generated constructor stub
  81:      }
  82:      
  83:  }
第 17 行把 ttv 做為 MainActivity 的物件,這樣可以讓我在 onTouchEvent() 方便改變顏色
第 19 ~ 22 行用來儲存按下以及移動的座標,這是為了讓移動不這麼靈敏
第 42 ~ 71 行就是偵測觸碰點的方法
第 44 行取得觸碰的總數,不過後面沒用到
第 47 行取得觸碰點的行為,行為種類相當多,後面只示範比較基本的方法(按下移動離開),詳細請參考這裡
第 49 行為按下的行為,我把 ttv 改為紅色(第 50 行)
第 51 ~ 52 行取得按下時的座標
第 54 行為離開的行為, ttv 恢復顏色
第 57 行為移動的行為
第 59 ~ 60 行取得移動時的座標,
第 62 ~ 65 行當移動時的座標變動不太大時才改變顏色, 事實上你也可以直接使用第 64 行,它非常靈敏
結果為
正常狀態
按下狀態
移動狀態





 Posted in:  
0 意見:
張貼留言