編輯:關於android開發
參考:
http://blog.csdn.net/qq418716640/article/details/8508973
http://www.cnblogs.com/mengdd/p/3335508.html
效果:一個手指實現(所有手勢事件)和(部分事件的);
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;
}
}
}
使用Android studio分析內存洩露 使用Android studio分析內存洩露 This post is a permitted tra
ubuntu下安裝AndroidStudio 最近將電腦的操作系統換成了ubuntu,對於不習慣win8/win10的人來說ubuntu確實是一個不錯的選擇,基本的軟
AS下NDK開發(一),as下ndk開發搗鼓了一天的NDK,總結下。 Eclipse下開發ndk好像挺麻煩的樣子,看書上要下載Cygwin,eclipse還要下載插件CD
手機安全衛士——高級工具-電話歸屬地查詢,安全衛士歸屬地查詢AToolsActivity.java //高級工具 public class AtoolsActivity