在活动之间滑动
Swipe Between Activities
我有 2 个活动,我想通过滑动在它们之间切换,我对 google 做了很多研究,但找不到解决方案,因为我正在使用位图(图像)我在 Activity 的 onCreate() 方法中编写了大部分代码,是否有任何解决方案,或者我如何将 activity 像它一样转换成片段
有一些图书馆适合您:
您可以使用 GestureDetector 来完成。以下是示例片段。
// You can change values of below constants as per need.
private static final int MIN_DISTANCE = 100;
private static final int MAX_OFF_PATH = 200;
private static final int THRESHOLD_VELOCITY = 100;
private GestureDetector mGestureDetector;
// write below code in onCreate method
mGestureDetector = new GestureDetector(context, new SwipeDetector());
// Set touch listener to parent view of activity layout
// Make sure that setContentView is called before setting touch listener.
findViewById(R.id.parent_view).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// Let gesture detector handle the event
return mGestureDetector.onTouchEvent(event);
}
});
// Define a class to detect Gesture
private class SwipeDetector extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (e1 != null && e2 != null) {
float dy = e1.getY() - e2.getY();
float dx = e1.getX() - e2.getX();
// Right to Left swipe
if (dx > MIN_DISTANCE && Math.abs(dy) < MAX_OFF_PATH &&
Math.abs(velocityX) > THRESHOLD_VELOCITY) {
// Add code to change activity
return true;
}
// Left to right swipe
else if (-dx > MIN_DISTANCE && Math.abs(dy) < MAX_OFF_PATH &&
Math.abs(velocityX) > THRESHOLD_VELOCITY) {
// Below is sample code to show left to right swipe while launching next activity
currentActivity.overridePendingTransition(R.anim.right_in, R.anim.right_out);
startActivity(new Intent(currentActivity,NextActivity.class));
return true;
}
}
return false;
}
}
//Below are sample animation xml files.
anim/right_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="500"
android:fromXDelta="-100%p"
android:toXDelta="0" />
</set>
anim/right_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="500"
android:fromXDelta="0"
android:toXDelta="100%p" />
</set>
我有 2 个活动,我想通过滑动在它们之间切换,我对 google 做了很多研究,但找不到解决方案,因为我正在使用位图(图像)我在 Activity 的 onCreate() 方法中编写了大部分代码,是否有任何解决方案,或者我如何将 activity 像它一样转换成片段
有一些图书馆适合您:
您可以使用 GestureDetector 来完成。以下是示例片段。
// You can change values of below constants as per need.
private static final int MIN_DISTANCE = 100;
private static final int MAX_OFF_PATH = 200;
private static final int THRESHOLD_VELOCITY = 100;
private GestureDetector mGestureDetector;
// write below code in onCreate method
mGestureDetector = new GestureDetector(context, new SwipeDetector());
// Set touch listener to parent view of activity layout
// Make sure that setContentView is called before setting touch listener.
findViewById(R.id.parent_view).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// Let gesture detector handle the event
return mGestureDetector.onTouchEvent(event);
}
});
// Define a class to detect Gesture
private class SwipeDetector extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (e1 != null && e2 != null) {
float dy = e1.getY() - e2.getY();
float dx = e1.getX() - e2.getX();
// Right to Left swipe
if (dx > MIN_DISTANCE && Math.abs(dy) < MAX_OFF_PATH &&
Math.abs(velocityX) > THRESHOLD_VELOCITY) {
// Add code to change activity
return true;
}
// Left to right swipe
else if (-dx > MIN_DISTANCE && Math.abs(dy) < MAX_OFF_PATH &&
Math.abs(velocityX) > THRESHOLD_VELOCITY) {
// Below is sample code to show left to right swipe while launching next activity
currentActivity.overridePendingTransition(R.anim.right_in, R.anim.right_out);
startActivity(new Intent(currentActivity,NextActivity.class));
return true;
}
}
return false;
}
}
//Below are sample animation xml files.
anim/right_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="500"
android:fromXDelta="-100%p"
android:toXDelta="0" />
</set>
anim/right_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="500"
android:fromXDelta="0"
android:toXDelta="100%p" />
</set>