package com.cc.view; import com.cc.R; import com.cc.R.layout; import com.cc.R.menu; import android.opengl.Visibility; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.Context; import android.util.Log; import android.view.GestureDetector; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.GestureDetector.SimpleOnGestureListener; import android.view.Menu; import android.view.View; import android.view.View.OnTouchListener; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; public class GestureDetectorAty extends Activity { private Button mButton; private GestureDetector mGestureDetector; private LinearLayout mLayout; private TextView mCancel; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.aty_gesture_detector); mLayout = (LinearLayout) findViewById(R.id.displayrecord); mCancel = (TextView) findViewById(R.id.tv_cancelrecord); mGestureDetector = new GestureDetector(this, new MyOnGestureListener( this)); mButton = (Button) findViewById(R.id.btn_textgesture); mButton.setFocusableInTouchMode(true); mButton.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: { break; } case MotionEvent.ACTION_MOVE: { final float p1x = event.getX(); break; } case MotionEvent.ACTION_UP: { mLayout.setVisibility(View.GONE); mCancel.setVisibility(View.GONE); break; } default: break; } mGestureDetector.onTouchEvent(event); // 必定要返回true,否則獲取不到完整的事件 return true; } }); } public class MyOnGestureListener extends SimpleOnGestureListener { private GestureDetectorAty mActivity; public MyOnGestureListener(Context context) { this.mActivity = (GestureDetectorAty) context; } @Override public void onLongPress(MotionEvent e) { // TODO Auto-generated method stub super.onLongPress(e); } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { float p2y = e2.getY(); float p1y = e1.getY(); if (p1y - p2y > 50) { if (e2.getX() - e1.getX() > -50 | e2.getX() - e1.getX() < 50) { } } // TODO Auto-generated method stub return super.onFling(e1, e2, velocityX, velocityY); } // 按住 且爲達到onLongPress以前 調用 一旦調用就不會有onSingleTapUp // 點擊了觸摸屏,可是沒有移動和彈起的動做。onShowPress和onDown的區別在於 // onDown是,一旦觸摸屏按下,就立刻產生onDown事件,可是onShowPress是onDown事件產生後, // 一段時間內,若是沒有移動鼠標和彈起事件,就認爲是onShowPress事件。 @Override public void onShowPress(MotionEvent e) { // TODO Auto-generated method stub super.onShowPress(e); } // 按下的時候觸發 @Override public boolean onDown(MotionEvent e) { // TODO Auto-generated method stub mLayout.setVisibility(View.VISIBLE); return super.onDown(e); } // 離開屏幕的一剎那 // 輕擊觸摸屏後,彈起。若是這個過程當中產生了onLongPress、onScroll和onFling事件,就不會 // 產生onSingleTapUp事件。 @Override public boolean onSingleTapUp(MotionEvent e) { // TODO Auto-generated method stub isScroll = true; mLayout.setVisibility(View.GONE); return super.onSingleTapUp(e); } @Override public boolean onSingleTapConfirmed(MotionEvent e) { // TODO Auto-generated method stub return super.onSingleTapConfirmed(e); } private boolean isScroll = true; // 滾動事件,當在觸摸屏上迅速的移動,會產生onScroll。由ACTION_MOVE產生 // e1:第1個ACTION_DOWN MotionEvent // e2:最後一個ACTION_MOVE MotionEvent // distanceX:距離上次產生onScroll事件後,X抽移動的距離 // distanceY:距離上次產生onScroll事件後,Y抽移動的距離 @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { // TODO Auto-generated method stub if (e1.getY() - e2.getY() > 50) { if (e2.getX() - e1.getX() > -50 | e2.getX() - e1.getX() < 50) { if (isScroll) { isScroll = false; mCancel.setVisibility(View.VISIBLE); mLayout.setVisibility(View.GONE); } } } else if (e1.getY() - e2.getY() < 10) { if (e2.getX() - e1.getX() > -50 | e2.getX() - e1.getX() < 50) { isScroll = true; if (isScroll) { mCancel.setVisibility(View.GONE); mLayout.setVisibility(View.VISIBLE); } } } return super.onScroll(e1, e2, distanceX, distanceY); } } }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".GestureDetectorAty" > <Button android:id="@+id/btn_textgesture" android:layout_width="100dp" android:layout_height="100dp" android:layout_centerInParent="true" android:text="@string/app_name" /> <LinearLayout android:id="@+id/displayrecord" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/btn_textgesture" android:layout_centerHorizontal="true" android:layout_marginBottom="50dp" android:orientation="horizontal" android:visibility="gone" > <ImageView android:id="@+id/arrow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/icon" /> <TextView android:id="@+id/tv_displayrecord" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="開始錄製" android:textColor="@color/green" /> </LinearLayout> <TextView android:id="@+id/tv_cancelrecord" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/btn_textgesture" android:layout_centerHorizontal="true" android:layout_marginBottom="150dp" android:background="@color/red" android:visibility="gone" android:text="取消錄製" android:textColor="@color/white" /> </RelativeLayout>