【问题标题】:Move ImageView from its current position to fixed position using translate animation使用平移动画将 ImageView 从其当前位置移动到固定位置
【发布时间】:2013-09-22 18:28:20
【问题描述】:

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

我的代码是……

       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);

提前致谢。

【问题讨论】:

    标签: android translate-animation


    【解决方案1】:

    您可以使用NineOldAndroids。它有翻译动画的例子。

    【讨论】:

      【解决方案2】:

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

      语法:

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

      fromXposition- 动画应该开始的 x 坐标

      toXPosition- 动画结束的 x 坐标

      fromYPosition- 动画应该开始的 y 坐标。

      toYPosition- 动画结束的 y 坐标。

      1)如果我们只想在X direction 中进行翻译,那么我们将 fromYPositiontoYPosition 设置为零。

      2)如果我们只想在Y direction 中进行翻译,那么我们将 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);
      

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

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

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

      view - 这是要应用动画的视图

      propertyName-正在动画的属性。

      FromX,toX-动画将随时间变化的一组值。

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

      【讨论】:

      • 非常感谢!这对我有帮助!
      • 我读到的很酷的事情是,如果您不指定 fromX 值,它将使用视图的当前位置作为起始位置。 developer.android.com/guide/topics/graphics/…
      • 我注意到在执行ObjectAnimation 之后调用视图的getTop() 方法仍然返回视图的初始位置...视图被移动,附加到它的OnClickListener 工作正常翻译后,但布局参数似乎没有改变。知道为什么吗?对象动画完成后如何更改实际布局参数?
      • 感谢您对 ObjectAnimator 的关注!我正在使用 OnTouchListener 等,只是动画不起作用;我应该使用 ObjectAnimator。
      【解决方案3】:

      您只需要将视图从一个位置转换为另一个位置。所以需要使用下面的代码来完成你的任务。

      imgHeart.animate()
          .scaleXBy(-6f)
          .scaleYBy(-6f)
          .alpha(.1f)
          .translationX((heigthAndWidth[0] / 2) - minusWidth) // trying to make my location
          .translationY(-((heigthAndWidth[1] / 2) - minusHeight))
          .setDuration(1000)
          .start();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-06-15
        • 1970-01-01
        • 2014-06-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多