问题描述
我试图做一个转换的动画从底部到屏幕中间的图像视图。一旦动画完成,我想要的图像视图呆在那里。我不想让setFillAfter(真),因为我想要的ImageView的实际位置进行更新。
我这样做,目前有2图像视图(一个在动画的起点和一个底),我与setVisibility玩到实现这一目标。这是做事情的正确方法?这里是code我用:
< ImageView的
机器人:ID =@ + ID / ivStart
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentBottom =真
机器人:layout_alignParentRight =真
机器人:背景=@可绘制/ typer_step_1
机器人:重力=中心
/>
< ImageView的
机器人:ID =@ + ID / ivMiddle
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentRight =真
机器人:layout_centerVertical =真
机器人:背景=@可绘制/ typer_step_1
机器人:重力=中心
机器人:能见度=隐形
/>
TranslateAnimation翻译=新TranslateAnimation(0,mDestLoc1 [0] -mSrcLoc1 [0],0,mDestLoc1 [1] -mSrcLoc1 [1]);
translate.setDuration(2000);
translate.setAnimationListener(新AnimationListener(){
@覆盖
公共无效onAnimationStart(动画动画){}
@覆盖
公共无效onAnimationEnd(动画动画){
ivMiddle.setVisibility(View.VISIBLE)
ivStart.setVisibility(View.INVISIBLE)
}
@覆盖
公共无效onAnimationRepeat(动画动画){}
});
ivStart.startAnimation(翻译);
那么你必须树立新的的LayoutParams
为您查看该动画。当动画完成,在你的 onAnimationEnd
部分,设置新的位置的查看
。
TranslateAnimation翻译=新TranslateAnimation(0,mDestLoc1 [0] -mSrcLoc1 [0],0,mDestLoc1 [1] -mSrcLoc1 [1]);
translate.setDuration(2000);
translate.setAnimationListener(新AnimationListener(){
@覆盖
公共无效onAnimationStart(动画动画){}
@覆盖
公共无效onAnimationEnd(动画动画){
RelativeLayout.LayoutParams面值=新RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
par.topMargin = mDestLoc1 [1] -mSrcLoc1 [1];
par.leftMargin = mDestLoc1 [0] -mSrcLoc1 [0];
view.setLayoutParams(PAR);
}
@覆盖
公共无效onAnimationRepeat(动画动画){}
});
view.startAnimation(翻译);
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.
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);
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);
这篇关于之后,动画(更新位置)移动的ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!