【问题标题】:Translate animation to correct position in Android将动画转换为 Android 中的正确位置
【发布时间】:2012-05-17 15:03:58
【问题描述】:
我在我的应用程序中为图像创建了动画。图像开始从中间到屏幕的左上角。现在我需要确保图像放置在左上角的所有设备中的正确位置。
目前它被放置在左上角的不同设备的不同位置。我该如何解决?
我的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:fillAfter="true">
<translate
android:fromXDelta="0%p"
android:toXDelta="-36%p"
android:fromYDelta="0%p"
android:toYDelta="-30.9%p"
android:duration="500"
android:fillAfter="true" />
</set>
谁能帮忙。
【问题讨论】:
标签:
android
android-layout
android-animation
【解决方案1】:
我得到了一些很好的结果,所以希望这对你或任何人有帮助。这个想法很简单。
将您想要动画的内容定位到 通过 xml 的最终位置,然后我使用 TranslateAnimation 并将其 from values 设置为任何浮点值并 设置为值 为 0。通过这种方式,小部件/视图动画并停留在您在 xml 中放置它的位置。
一个小例子,让TextView从屏幕外移动到它在xml中指定的位置。
//This is just for getting the from value, from where animation starts.
WindowManager wm = (WindowManager) mContext
.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int width = display.getWidth();
Animation moveRighttoLeft = new TranslateAnimation(width, 0, 0, 0);
moveRighttoLeft.setDuration(700);
AnimationSet animation = new AnimationSet(false);
animation.addAnimation(moveRighttoLeft);
myTextView.setAnimation(animation);
这里是xml的
<TextView
android:id="@+id/mytextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a Text View"
android:layout_marginLeft="30dp"/>