我想用动画在两个活动之间进行翻译。我想当用户触摸页面顶部的图像时,图像转换到屏幕的底部(向下滑动)和从上到下的第二个活动移动的视图(向下滑动),这就像拖动移动同时运行。我不知道我怎么能实现这个?我使用这段代码。
slide_down.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<scale
android:duration="500"
android:fromXScale="1.0"
android:fromYScale="0.0"
android:interpolator="@android:anim/linear_interpolator"
android:toXScale="1.0"
android:toYScale="1.0" />
</set>
眠:
private OnTouchListener onTouchListener=new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainActivity.this,Test.class);
//overridePendingTransition(R.anim.slide_down, R.anim.slide_down);
startActivity(intent);
overridePendingTransition(R.anim.slide_down, R.anim.slide_down);
return false;
}
};
当我运行此代码并触摸图像时,屏幕变为黑色,然后第二个活动开始,然后动画运行。但是我想在第一个活动关闭时动画,第二个活动在第一个活动结束时开始
答案 0 :(得分:1)
你走在正确的道路上。
overridePendingTransition(R.anim.slide_in_top, R.anim.slide_out_bottom);
必须在活动的onCreate中定义,并定义该活动在进入和退出时的行为。
slide_in_top.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="200"
android:fromYDelta="-100%"
android:toYDelta="0%" />
slide_out_bottom.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="200"
android:fromYDelta="0%"
android:toYDelta="100%" />
修改强>
您只想要视图的动画,然后切换到另一个活动,对吗?
@Override
public boolean onTouch(View v, MotionEvent event) {
// first animate the view
TranslateAnimation anim = new TranslateAnimation(fromXDelta, toXDelta, fromYDelta, toYDelta)
anim.setDuration(duration);
v.startAnimation(anim);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// wait for the duration of the animation before switching acitivity
// remember to apply the overridePendingTransition to them
// if you want a transition animation on this too
// overridePendingTransition added to both onCreate of Test and MainActivity
Intent intent=new Intent(MainActivity.this,Test.class);
startActivity(intent);
}
}, duration); // <-- notice the wait for animation to complete
return false;
}