抽取类实现左右滑动Activity,及滑动简单动画

    xiaoxiao2021-12-10  7


    多个Activity中的共同方法提取到BaseActivity中实现左右滑动效果;


    创建BaseActivity继承Activity

    package com.android.demo.gesturedetector; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.GestureDetector; import android.view.GestureDetector.SimpleOnGestureListener; import android.view.MotionEvent; import android.widget.Toast; public abstract class BaseActivity extends Activity { // 定义全局变量 mGestureDecetor private GestureDetector mGestureDetector; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mGestureDetector = new GestureDetector(getApplicationContext(), new SimpleOnGestureListener() { // 重写手势监听器的onFling()方法,用来监听滑动屏幕的手势 @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { // e1 :手指按下的移动事件. float e1X = e1.getRawX(); float e1Y = e1.getRawY(); // e2 : 手指移动的动作事件. float e2X = e2.getRawX(); float e2Y = e2.getRawY(); // 指滑动的X轴方向的如果小于59,无效果,单位是px if (Math.abs(e2X - e1X) < 50) { Toast.makeText(getApplicationContext(), "左右滑动小于50px", 0).show(); return false; // 比较e2,e1得到的Y值,获取两数绝对值判断是否上下滑动 } else if (Math.abs(e2Y - e1Y) > 200) { Toast.makeText(getApplicationContext(), "手势上下滑动", 0) .show(); return false; // 比较e2,e1得到的Y值,获取两数绝对值判断是否左右滑动 } else if (Math.abs(e2X - e1X) > 50) { // 判断是否左滑 if ((e2X - e1X) > 0) { Toast.makeText(getApplicationContext(), "右滑", 0) .show(); tolast(); // 判断是否右滑 } else { Toast.makeText(getApplicationContext(), "左滑", 0) .show(); tofirst(); } return true; } else { return super.onFling(e1, e2, velocityX, velocityY); } } }); } // 重写OnTouchEvent()识别手势监听器 @Override public boolean onTouchEvent(MotionEvent event) { return mGestureDetector.onTouchEvent(event); } /** * 定义抽象方法,子类继承父类后重写方法实现左滑 * 左滑进入新的Activity */ public abstract void tofirst(); /** * * 定义抽象方法,子类继承父类后重写方法实现左滑 *右滑进入新的Activity */ public abstract void tolast(); /** * open一个新的Activity * * @param clazz * 要进入得Activity.class */ public void openNewActivity(Class<?> clazz) { Intent intent = new Intent(getApplicationContext(), clazz); startActivity(intent); } }

    定义第一个界面继承BaseActivity

    package com.android.demo.gesturedetector; import android.os.Bundle; public class MainActivity extends BaseActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } // 重写父类方法进入右滑界面 @Override public void tolast() { openNewActivity(SecondActivity.class); // 添加Activity替换的动画 overridePendingTransition(R.anim.entertranstnim, R.anim.exittranstnim); } @Override public void tofirst() { }

    定义第二个界面继承BaseActivity

    package com.android.demo.gesturedetector; import android.os.Bundle; public class SecondActivity extends BaseActivity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); } @Override public void tofirst() { openNewActivity(MainActivity.class); //添加Activity替换的动画 overridePendingTransition(R.anim.exittranstnim, R.anim.entertranstnim); } @Override public void tolast() { } }

    不要忘记在清单文件中配置activity节点

    <activity android:name="com.android.demo.gesturedetector.SecondActivity" > </activity>

    最后在res文件目录下创建anim文件夹,创建xml文件实现动画–代码如下

    移入动画

    <?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="100" android:fromXDelta="100%p" android:fromYDelta="0" android:toXDelta="0" android:toYDelta="0" > </translate>

    移出动画

    <?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="100" android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="-100%p" android:toYDelta="0" > </translate>

    附github源码地址

    转载请注明原文地址: https://ju.6miu.com/read-700210.html

    最新回复(0)