Ric*_*ick 3 android swipe android-activity
我想有一个介绍应用程序的活动,并且要关闭你必须从右向左滑动它(如果没有太多的工作,可能会有一个平滑的动画),那么我已经拥有应用程序的另一部分,就是这样ActionBar tabs + Swipe Views.我已经阅读了一些Android指南,比如实现Swipe Views,但它们不是我的情况.请问你能帮帮我吗?[使用"平滑动画"我的意思是滑动必须跟随手指]
我通常使用手势监听器来实现这一点:
首先,在res/anim以下内容中定义翻译动画:
slide_in_left.xml:
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-50%p" android:toXDelta="0"
android:duration="@android:integer/config_longAnimTime"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="@android:integer/config_mediumAnimTime" />
</set>
Run Code Online (Sandbox Code Playgroud)
slide_in_right.xml:
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="50%p" android:toXDelta="0"
android:duration="@android:integer/config_longAnimTime"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="@android:integer/config_mediumAnimTime" />
</set>
Run Code Online (Sandbox Code Playgroud)
slide_out_left.xml:
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="-50%p"
android:duration="@android:integer/config_longAnimTime"/>
<alpha android:fromAlpha="1.0" android:toAlpha="0.0"
android:duration="@android:integer/config_mediumAnimTime" />
</set>
Run Code Online (Sandbox Code Playgroud)
slide_out_right.xml:
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="50%p"
android:duration="@android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="1.0" android:toAlpha="0.0"
android:duration="@android:integer/config_mediumAnimTime" />
</set>
Run Code Online (Sandbox Code Playgroud)
然后在您当前的活动类中:
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
try {
float slope = (e1.getY() - e2.getY()) / (e1.getX() - e2.getX());
float angle = (float) Math.atan(slope);
float angleInDegree = (float) Math.toDegrees(angle);
// left to right
if (e1.getX() - e2.getX() > 20 && Math.abs(velocityX) > 20) {
if ((angleInDegree < 45 && angleInDegree > -45)) {
startActivity(new Intent(CurrentActivitiy.this, NextActivity.class);
CurrentActivity.this.overridePendingTransition(
R.anim.slide_in_left, R.anim.slide_out_right);
finish();
}
// right to left fling
} else if (e2.getX() - e1.getX() > 20
&& Math.abs(velocityX) > 20) {
if ((angleInDegree < 45 && angleInDegree > -45)) {
startActivity(new Intent(CurrentActivitiy.this, NextActivity.class);
CurrentActivity.this.overridePendingTransition(
R.anim.slide_in_right, R.anim.slide_out_left);
finish();
}
}
return true;
} catch (Exception e) {
// nothing
}
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以注册任何视图以接收/收听手势:
final GestureDetector gestureDetector = new GestureDetector(new MyGestureDetector());
//the parent layout
findViewById(R.id.parent_layout).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) return false;
return false;
}
});
//an image view
findViewById(R.id.image_view).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) return false;
return false;
}
});
// a text view
findViewById(R.id.text_view).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) return false;
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4545 次 |
| 最近记录: |