2

我在我的应用程序中为图像创建了动画。图像开始从中间到屏幕的左上角。现在我需要确保图像放置在左上角的所有设备中的正确位置。目前它被放置在左上角的不同设备的不同位置。我该如何解决?我的代码如下:

<?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>

任何人都可以请帮忙。

4

1 回答 1

0

我得到了一些很好的结果,所以希望这对你或任何人有帮助。这个想法很简单。

通过 xml 将您想要动画的内容定位到它的最终位置,然后我使用TranslateAnimation并将其从值设置为任何浮点值并将设置为0。以这种方式,小部件/视图动画并停留在您在 xml 中放置它的位置。

一个小例子,用于TextView将屏幕外部的 a 设置为在 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"/>
于 2012-06-27T19:07:16.730 回答