android - 使用平移动画将 ImageView 从当前位置移动到固定位置

标签 android translate-animation

我想使用平移动画将 ImageView 从其当前位置移动到屏幕上的某个固定位置。 另外我想知道翻译动画是如何工作的以及它接受哪些参数?

我的一段代码是...

       RelativeLayout.LayoutParams lParams = (LayoutParams) spreadImage
                .getLayoutParams();
       TranslateAnimation ta

        ta = new TranslateAnimation(lParams.leftMargin,
                randomLeftMarginsList.get(currentSpreadIndex),
                lParams.topMargin,

        ta.setAnimationListener(this);
        ta.setDuration(ApplicationConstant.PUZZLE_GAME_IMAGE_SPREADING_TIME);
        spreadImage.startAnimation(ta);

提前致谢。

最佳答案

Translate Animation 控制布局或按钮或应用动画的任何 View 的位置和位置。它可以在 x 方向或 y 方向移动对象。

语法:

 TranslateAnimation transAnimation= new TranslateAnimation(fromXposition, toXPosition, fromYPosition, toYPosition);

fromXposition- 动画开始的 x 坐标

toXPosition- 动画结束的 x 坐标

fromYPosition- 动画开始位置的 y 坐标。

toYPosition- 动画结束的 y 坐标。

1) 如果我们只想在X 方向 平移,那么我们将fromYPositiontoYPosition 设置为零。

2)如果我们只想在Y 方向 平移,那么我们将fromXPositiontoXPosition 设置为零。

还有另一种方法,我们在 res 文件夹中创建一个 anim 文件夹。在这个文件夹中,我们添加了动画 xml。我们使用了一个翻译标签,我们在其中指定了属性值。

在下面的xml中

android:duration 定义动画的执行时间

android:repeatCount 指定次数。动画应该重复的次数,

android:fromYDelta 定义动画开始的 y 坐标

android:toYDelta 定义动画结束的 y 坐标。

line_translate.xml

 <set  xmlns:android=”http://schemas.android.com/apk/res/android”>
<translate android:duration=”300″ android:repeatCount=”1 android:fromYDelta=”0.0″ android:toYDelta=”174.0″ />

代码:

  Animation lineTranslate;
//loading xml from anim folder
  Animation localAnimation = AnimationUtils.loadAnimation(this, R.anim.line_translate);
   //You can now apply the animation to a view
    view.startAnimation(transAnimation);

翻译动画可以改变对象的视觉外观,但不能改变对象本身。也就是说,如果您将平移动画应用于 View ,它会移动到一个新位置,但不会触发其点击事件,而点击事件仍会在其之前的位置触发。发生这种情况是因为 View 仍处于其原始位置。

为了克服这个问题,我们可以使用实际移动对象的ObjectAnimation。对象动画是唯一实际移动对象的动画。您可以使用 ObjectAnimator 创建翻译动画。

  ObjectAnimator transAnimation= ObjectAnimator.ofFloat(view, propertyName, fromX, toX);
  transAnimation.setDuration(3000);//set duration
  transAnimation.start();//start animation

view - 这是应用动画的 View

propertyName-正在设置动画的属性。

FromX,toX-动画将随时间在其间设置动画的一组值。

希望这能给你很好的理解。

关于android - 使用平移动画将 ImageView 从当前位置移动到固定位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18864724/

相关文章:

java - Android 中如何将 ASCII 字符转换为十进制数字?

android - Activity com.act.hyd.app.EmtyActivity 泄漏了最初添加到此处的窗口 com.android.internal.policy.impl.PhoneWindow$DecorView@416e8bb8

android - 即使我们在 android 中移动数英里之外,位置管理器也无法正确检测到位置

android - 翻译后旋转动画在 Android 上不起作用

c - 在 C 中使用 OpenGL 3 动画场景

slider 上的 jQuery + css3 过渡

android - Android 设备的时间会跳跃吗?

android - FireBase 存储图像上传 Android

android - 当 android :targetSdkVersion ="17" not specified in manifest. xml for Android 4.1.2 (Samsung Galaxy s2 gt-i9100) 时, View (按钮)不可见

java - Android TranslateAnimation 到中心