上下移动 ImageView 的动画

Animation for moving an ImageView up and down

我是 Android 的动画新手,搜索了很多关于在 ImageView 上上下移动的内容,但是关于从左到右移动的内容很多。我找不到可以上下移动的tut。谁能告诉我如何实现这一目标? 我在这里添加一些代码,我从 YouTube 上学到了将图像从左向右移动(imga 是 ImageView)-

 Animation img = new TranslateAnimation(Animation.ABSOLUTE, 150, Animation.ABSOLUTE, Animation.ABSOLUTE);
            img.setDuration(3000);
            img.setFillAfter(true);

            imga.startAnimation(img);

使用此 xml 制作动画 anim/up_down.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:interpolator="@android:anim/linear_interpolator" >


<!-- Move -->

<translate
    android:duration="800"
    android:fillAfter="true"
    android:fromYDelta="0%p"
    android:toYDelta="70%p" />

<translate
    android:duration="800"
    android:fillAfter="true"
    android:fromYDelta="0%p"
    android:startOffset="800"
    android:toYDelta="-70%p" />


</set>

在Java Class:

// Animation
Animation animUpDown;

 // load the animation
animUpDown = AnimationUtils.loadAnimation(getApplicationContext(),
            R.anim.up_dwon);

  // start the animation
  view.startAnimation(animUpDown);

您需要了解图像或对象移动背后的逻辑,这些图像或对象将在轴上从 x 到 x delta 或从 y 到 y delta 进行动画处理,其中

  • x=n 变量到 x=-n 变量中的值表示图像将移动 从右到左增量
  • x= -n 变量到 x=n 变量中的值表示图像将移动
    从左到右 delta
  • y=n 变量到 y=-n 变量中的值表示图像将移动 从上到下增量
  • y=-n 变量到 y=n 变量中的值表示图像将移动 从下到上增量

下图带你看10门标准数学

注意:这里的n被认为是顶点的DP或点。