private ScrollTextView tv_rough; private GestureDetector mGesture; tv_rough = (ScrollTextView) findViewById(R.id.tv_rough); tv_rough.setOnTouchListener(this); mGesture = new GestureDetector(this, this); @Override public boolean onTouch(View v, MotionEvent event) { return mGesture.onTouchEvent(event); }另外,也可在当前视图或当前Activity中重写onTouchEvent方法,在该方法中由GestureDetector接管触摸事件。
@Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_MOVE: getParent().requestDisallowInterceptTouchEvent(true); ...//响应手势 break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: ...//处理完毕 getParent().requestDisallowInterceptTouchEvent(false); break; } }
import android.annotation.SuppressLint; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.widget.ScrollView; @SuppressLint("ClickableViewAccessibility") public class CustomScrollView extends ScrollView { private final static String TAG = "CustomScrollView"; private float mOffsetX; private float mOffsetY; private float mLastPosX; private float mLastPosY; public CustomScrollView(Context context) { super(context); } public CustomScrollView(Context context, AttributeSet attr) { super(context, attr); setFadingEdgeLength(0); } @Override public boolean dispatchTouchEvent(MotionEvent event) { return super.dispatchTouchEvent(event); } @Override public boolean onTouchEvent(MotionEvent event) { return super.onTouchEvent(event); } @Override public boolean onInterceptTouchEvent(MotionEvent event) { Log.d(TAG, "onInterceptTouchEvent"); boolean result = false; switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mOffsetX = 0.0F; mOffsetY = 0.0F; mLastPosX = event.getX(); mLastPosY = event.getY(); result = super.onInterceptTouchEvent(event); // false传给子控件 break; default: float thisPosX = event.getX(); float thisPosY = event.getY(); mOffsetX += Math.abs(thisPosX - mLastPosX); // x轴偏差 mOffsetY += Math.abs(thisPosY - mLastPosY); // y轴偏差 mLastPosX = thisPosX; mLastPosY = thisPosY; if (mOffsetX < 3 && mOffsetY < 3) { result = false; // false传给子控件(点击事件) } else if (mOffsetX < mOffsetY) { result = true; // true不传给子控件(垂直滑动) } else { result = false; // false传给子控件 } break; } return result; } }
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">ScrollTextView控件的源码如下:</span>
import android.content.Context; import android.util.AttributeSet; import android.widget.Scroller; import android.widget.TextView; public class ScrollTextView extends TextView { private static final String TAG = "ScrollTextView"; private Scroller mScroller; public ScrollTextView(Context context, AttributeSet attrs) { super(context, attrs); mScroller = new Scroller(context); } public void smoothScrollTo(int fx, int fy) { int dx = fx - mScroller.getFinalX(); int dy = fy - mScroller.getFinalY(); smoothScrollBy(dx, dy); } public void smoothScrollBy(int dx, int dy) { //设置滚动偏移量,注意正数是往左滚往上滚,负数才是往右滚往下滚 mScroller.startScroll(mScroller.getFinalX(), mScroller.getFinalY(), -dx, -dy); //调用invalidate()才能保证computeScroll()会被调用 invalidate(); } @Override public void computeScroll() { //先判断mScroller滚动是否完成 if (mScroller.computeScrollOffset()) { //这里调用View的scrollTo()完成实际的滚动 scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); //刷新页面 postInvalidate(); } super.computeScroll(); } }
import com.example.exmgesture.widget.ScrollTextView; import android.annotation.SuppressLint; import android.app.Activity; import android.os.Bundle; import android.view.GestureDetector; import android.view.GestureDetector.OnGestureListener; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; @SuppressLint("ClickableViewAccessibility") public class RoughActivity extends Activity implements OnTouchListener,OnGestureListener { private final static String TAG = "RoughActivity"; private ScrollTextView tv_rough; private GestureDetector mGesture; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_rough); tv_rough = (ScrollTextView) findViewById(R.id.tv_rough); tv_rough.setOnTouchListener(this); mGesture = new GestureDetector(this, this); } @Override public boolean dispatchTouchEvent(MotionEvent ev) { // TODO Auto-generated method stub return super.dispatchTouchEvent(ev); } @Override public boolean onTouch(View v, MotionEvent event) { return mGesture.onTouchEvent(event); } @Override public boolean onDown(MotionEvent e) { return true; } @Override public void onShowPress(MotionEvent e) { } @Override public boolean onSingleTapUp(MotionEvent e) { tv_rough.setText("您轻轻点击了一下"); return false; } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { return true; } @Override public void onLongPress(MotionEvent e) { } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { tv_rough.setText("您拖动我啦"); float offsetX = e2.getRawX() - e1.getRawX(); float offsetY = e2.getRawY() - e1.getRawY(); tv_rough.smoothScrollBy((int)offsetX, (int)offsetY); return false; } }