Android實(shí)現(xiàn)手勢滑動(dòng)(左滑和右滑)
最近想實(shí)現(xiàn)Android左滑彈出菜單框,右滑消失菜單這個(gè)個(gè)功能。了解了一下Android 的滑動(dòng)事件,必須是在view組件或者Activity上實(shí)現(xiàn),同時(shí)必須實(shí)現(xiàn)OnTouchListener, OnGestureListener這個(gè)兩個(gè)接口。
public class MyRelativeLayout extends RelativeLayout implements GestureDetector.OnGestureListener{ private float mPosX, mPosY, mCurPosX, mCurPosY; private static final int FLING_MIN_DISTANCE = 20;// 移動(dòng)最小距離 private static final int FLING_MIN_VELOCITY = 200;// 移動(dòng)最大速度 //構(gòu)建手勢探測器 GestureDetector mygesture = new GestureDetector(this); public MyRelativeLayout(Context context){ super(context) } public MyRelativeLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } public MyRelativeLayout(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } @Override public boolean onTouchEvent(MotionEvent arg0) { // TODO Auto-generated method stub return mDetector.onTouchEvent(arg0); } @Override public boolean onSingleTapUp(MotionEvent e) { // TODO Auto-generated method stub return false; } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { // TODO Auto-generated method stub return false; } @Override public boolean onDown(MotionEvent e) { // TODO Auto-generated method stub return false; } @Override public void onShowPress(MotionEvent e) { // TODO Auto-generated method stub } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { // TODO Auto-generated method stub // e1:第1個(gè)ACTION_DOWN MotionEvent // e2:最后一個(gè)ACTION_MOVE MotionEvent // velocityX:X軸上的移動(dòng)速度(像素/秒) // velocityY:Y軸上的移動(dòng)速度(像素/秒) // X軸的坐標(biāo)位移大于FLING_MIN_DISTANCE,且移動(dòng)速度大于FLING_MIN_VELOCITY個(gè)像素/秒 //向左 if (e1.getY() - e2.getY() > FLING_MIN_DISTANCE){ // && Math.abs(velocityX) > FLING_MIN_VELOCITY) { collapse(); } //向上 if (e2.getY() - e1.getY() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) { } return false; } }
再添加一段實(shí)現(xiàn)手勢滑動(dòng)效果:
手勢滑動(dòng),其實(shí)也就是觸摸事件
public class PhoneGuard01 extends Activity { private GestureDetector mGestureDetector; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_phone_guard01); //創(chuàng)建手勢識(shí)別對象,并創(chuàng)建手勢識(shí)別的監(jiān)聽 mGestureDetector = new GestureDetector(this,new SimpleOnGestureListener(){ //這個(gè)方法需要自己去重寫 @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { float X1=e1.getX();//獲取按下去的坐標(biāo)點(diǎn),X軸 float X2=e2.getX();//獲取提起來的坐標(biāo)點(diǎn),Y軸 float Y1=e1.getY();//獲得按下去的Y軸坐標(biāo)點(diǎn) float Y2=e1.getY();//獲得提起來的Y軸坐標(biāo)點(diǎn) //Y的移動(dòng)距離,比X 的移動(dòng)距離要大,所以不做任何的操作 if(Math.abs(Y1-Y2)>Math.abs(X1-X2)){ return false; } if(X1>X2){//表示下一頁 nextPage(null); } return super.onFling(e1, e2, velocityX, velocityY); } }); } /** 下面代碼的意思就是說,把自己的手勢識(shí)別的觸摸事件, 讓父類去調(diào)用 */ //onTouchEvent(MotionEvent event)是繼承來自View對象的 @Override public boolean onTouchEvent(MotionEvent event) { //mGestureDetector.onTouchEvent(event)是GestureDetector自己本身的 mGestureDetector.onTouchEvent(event); return super.onTouchEvent(event); }//-----------------上面就是手勢識(shí)別的代碼實(shí)現(xiàn)------------------------------ //跳轉(zhuǎn)到下一個(gè)頁面 public void nextPage(View v){ Intent intent=new Intent(this,PhoneGuard02.class); startActivity(intent); finish(); }}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 基于android studio的layout的xml文件的創(chuàng)建方式2. 解決Android studio xml界面無法預(yù)覽問題3. 詳解Android studio 動(dòng)態(tài)fragment的用法4. 圖文詳解vue中proto文件的函數(shù)調(diào)用5. 什么是python的自省6. Spring Boot和Thymeleaf整合結(jié)合JPA實(shí)現(xiàn)分頁效果(實(shí)例代碼)7. Android如何加載Base64編碼格式圖片8. 使用Android studio查看Kotlin的字節(jié)碼教程9. Vuex localStorage的具體使用10. Vue封裝一個(gè)TodoList的案例與瀏覽器本地緩存的應(yīng)用實(shí)現(xiàn)

網(wǎng)公網(wǎng)安備