編輯:關於Android編程
效果:一個手指實現(所有手勢事件)和(部分事件的);
A. 所有手勢
activity_main.xml
<TextView android:id="@+id/gesture" android:layout_width="match_parent" android:layout_height="150dp" android:gravity="center" android:text="單擊手勢變化" /><TextView android:id="@+id/doubleTap" android:layout_width="match_parent" android:layout_height="150dp" android:gravity="center" android:text="雙擊手勢變化" />
MainActivity.java
public class MainActivity extends Activity { private static final String LOG_TAG = "HelloGesture"; private GestureDetector mGestureDetector = null; private TextView mGestureTextView = null; private TextView mDoubleTapTextView = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mGestureTextView = (TextView) findViewById(R.id.gesture); mDoubleTapTextView = (TextView) findViewById(R.id.doubleTap); // 構造GestureDetector對象,傳入監聽器對象 mGestureDetector = new GestureDetector(this, mOnGestureListener); // 傳入雙擊監聽器對象 mGestureDetector.setOnDoubleTapListener(mDoubleTapListener); } @Override public boolean onTouchEvent(MotionEvent event) { // 在onTouchEvent方法中將事件傳遞給手勢檢測對象,否則手勢監聽對象中的回調函數是不會被調用的 mGestureDetector.onTouchEvent(event); return super.onTouchEvent(event); } private OnGestureListener mOnGestureListener = new OnGestureListener() { @Override public boolean onSingleTapUp(MotionEvent e) { Log.i(LOG_TAG, "onSingleTapUp: " + e.toString()); mGestureTextView.setText("onSingleTapUp: "); return false; } @Override public void onShowPress(MotionEvent e) { Log.i(LOG_TAG, "onShowPress: " + e.toString()); mGestureTextView.setText("onShowPress: "); } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { Log.i(LOG_TAG, "onScroll: " + e1.toString() + ", " + e2.toString()); mGestureTextView.setText("onScroll "); return false; } @Override public void onLongPress(MotionEvent e) { Log.i(LOG_TAG, "onLongPress: " + e.toString()); mGestureTextView.setText("onLongPress: "); } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { Log.i(LOG_TAG, "onFling: " + e1.toString() + ", " + e2.toString()); mGestureTextView.setText("onFling "); return false; } @Override public boolean onDown(MotionEvent e) { Log.i(LOG_TAG, "onDown: " + e.toString()); mGestureTextView.setText("onDown: "); return false; } }; private OnDoubleTapListener mDoubleTapListener = new OnDoubleTapListener() { @Override public boolean onSingleTapConfirmed(MotionEvent e) { Log.i("LOG_TAG", "onSingleTapConfirmed: " + e.toString()); mDoubleTapTextView.setText("onSingleTapConfirmed: "); return false; } @Override public boolean onDoubleTapEvent(MotionEvent e) { Log.i("LOG_TAG", "onDoubleTapEvent: " + e.toString()); mDoubleTapTextView.setText("onDoubleTapEvent: "); return false; } @Override public boolean onDoubleTap(MotionEvent e) { Log.i("LOG_TAG", "onDoubleTap: " + e.toString()); mDoubleTapTextView.setText("onDoubleTap: "); return false; } };}
B. 部分手勢
如果你僅僅只想處理幾種手勢,你可以選擇繼承GestureDetector.SimpleOnGestureListener類,而不是實現
GestureDetector.OnGestureListener接口
MainActivity.java
public class MainActivity extends Activity { private GestureDetector mGestureDetector = null; private TextView mGestureTextView = null; private TextView mDoubleTapTextView = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mGestureTextView = (TextView) findViewById(R.id.gesture); mDoubleTapTextView = (TextView) findViewById(R.id.doubleTap); // 構造GestureDetector對象,傳入監聽器對象 mGestureDetector = new GestureDetector(this, new MyGestureListener()); // 傳入雙擊監聽器對象 } @Override public boolean onTouchEvent(MotionEvent event) { // 在onTouchEvent方法中將事件傳遞給手勢檢測對象,否則手勢監聽對象中的回調函數是不會被調用的 this.mGestureDetector.onTouchEvent(event); return super.onTouchEvent(event); } class MyGestureListener extends GestureDetector.SimpleOnGestureListener { private static final String DEBUG_TAG = "Gestures"; @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { Log.d(DEBUG_TAG, "onFling: " + e1.toString() + ", " + e2.toString()); mGestureTextView.setText("onFling "); return false; } @Override public boolean onDown(MotionEvent e) { Log.d(DEBUG_TAG, "onDown: " + e.toString()); mGestureTextView.setText("onDown: "); return false; } }}
概念: java中單例模式是一種常見的設計模式,單例模式分三種:懶漢式單例、餓漢式單例、登記式單例三種。 單例模式有一下特點: 1、單例類只能有一個實例。
ProgressBar作用:當應在後台執行時,前台界面不會有任何信息,這時用戶根本不知道程序是否在執行,以及執行進度等,因些需要使用進度條來提示程序執行的進度.在Andr
一、情形描述在常使用的頁面布局中,為保持用戶一貫的使用風格,會保持頁面的整體風格相似。除開底部導航外,標題欄是使用頻率較高的另一種頁面布局。如圖所示:在程序猿&ldquo
顯示操作進度的對話框 1、使用上一篇創建的同一項目,在activity_main.xml文件中添加一個Button: 2、在MainActivity.ja