Move imageview after animation (update position)(动画后移动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);
这篇关于动画后移动imageview(更新位置)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:动画后移动imageview(更新位置)
基础教程推荐
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01