动画后移动imageview(更新位置)
我正在尝试在图像视图上从屏幕底部到中间进行翻译动画.动画完成后,我希望图像视图留在那里.我不想要 setFillAfter(true) 因为我想要更新 imageview 的实际位置.
I am trying to to do a translate animation on an image view from the bottom to the middle of the screen. Upon finish of the animation, I want the image view to stay there. I dont want the setFillAfter(true) because I want the actual position of the imageview to be updated.
我目前通过 2 个图像视图(一个在动画开始处,一个在结束处)来做到这一点,并且我使用 setVisibility 来实现这一点.这是做事的正确方法吗?这是我使用的代码:
I do it currently by having 2 image view (one at the start of animation and one at the end) and I play with the setVisibility to achieve this. Is this the correct way to do things? Here is the code I used:
<ImageView
android:id="@+id/ivStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="@drawable/typer_step_1"
android:gravity="center"
/>
<ImageView
android:id="@+id/ivMiddle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@drawable/typer_step_1"
android:gravity="center"
android:visibility="invisible"
/>
TranslateAnimation translate = new TranslateAnimation(0, mDestLoc1[0]-mSrcLoc1[0], 0, mDestLoc1[1]-mSrcLoc1[1]);
translate.setDuration(2000);
translate.setAnimationListener(new AnimationListener(){
@Override
public void onAnimationStart(Animation animation) {}
@Override
public void onAnimationEnd(Animation animation) {
ivMiddle.setVisibility(View.VISIBLE)
ivStart.setVisibility(View.INVISIBLE)
}
@Override
public void onAnimationRepeat(Animation animation) {}
});
ivStart.startAnimation(translate);
推荐答案
然后你必须为你的动画视图设置新的 LayoutParams
.动画完成后,在您的 onAnimationEnd
部分中,设置您的 View
的新位置.
Then you must set new LayoutParams
for your View which is animating. When animation finishes, in your onAnimationEnd
part, set new position of your View
.
TranslateAnimation translate = new TranslateAnimation(0, mDestLoc1[0]-mSrcLoc1[0], 0, mDestLoc1[1]-mSrcLoc1[1]);
translate.setDuration(2000);
translate.setAnimationListener(new AnimationListener(){
@Override
public void onAnimationStart(Animation animation) {}
@Override
public void onAnimationEnd(Animation animation) {
RelativeLayout.LayoutParams par = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
par.topMargin = mDestLoc1[1]-mSrcLoc1[1];
par.leftMargin = mDestLoc1[0]-mSrcLoc1[0];
view.setLayoutParams(par);
}
@Override
public void onAnimationRepeat(Animation animation) {}
});
view.startAnimation(translate);
相关文章