【问题标题】:How to rotate a drawable by ObjectAnimator?如何通过 ObjectAnimator 旋转可绘制对象?
【发布时间】:2012-12-26 10:28:24
【问题描述】:

像这样很好地绘制一个可绘制的作品:

if(mAlphaAnimation == null){
        mAlphaAnimation = ObjectAnimator.ofFloat(this, "alpha", 0.0f,1.0f).setDuration(TARGET_ANIM_ALPHA_DURATION);
        mAlphaAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
        mAlphaAnimation.setStartDelay(TARGET_ANIM_ALPHA_DELAY_BASE*power);
        mAlphaAnimation.setRepeatCount(ValueAnimator.INFINITE);
        mAlphaAnimation.setRepeatMode(ValueAnimator.REVERSE);
        mAlphaAnimation.addUpdateListener(this);
 }

但是如果我想像下面那样旋转一个drawable,它就行不通了。

private void createRotateAnim(float fromDegress,float toDegress,int duration){
    if(mRotateAnimation == null){
        mRotateAnimation = ObjectAnimator.ofFloat(this, "rotation",fromDegress,toDegress).setDuration(duration);
        mRotateAnimation.setStartDelay(100);
        mRotateAnimation.setInterpolator(new AccelerateInterpolator());
        mRotateAnimation.addUpdateListener(this);
    }
}

任何人都可以帮我解决这个问题,或者这些是创建旋转可绘制动画的任何其他方式。

对不起,我的英语不好。

【问题讨论】:

  • 我想通过触摸事件改变一个drawable的状态,drawable是一个单独的drawable,而不是一个drawable列表。例如,根据屏幕上的移动距离改变一个drawable的alpha值。并且这个功能已经完成。然后,我想根据移动角度改变一个可绘制的方向。换句话说,根据角度旋转drawable。这些drawables将在同一个视图上绘制。我想知道具有旋转属性的 ObjectAnimator 对象是否可以旋转可绘制对象。也许,它可以旋转,因为 imageview 可以旋转用于背景可绘制的可绘制对象。

标签: android rotation drawable objectanimator


【解决方案1】:

改用ObjectAnimator

ImageView imageview = (ImageView)findViewById(R.id.image);

ObjectAnimator imageViewObjectAnimator = ObjectAnimator.ofFloat(imageview ,
                    "rotation", 0f, 360f);
            imageViewObjectAnimator.setDuration(1000); // miliseconds
            imageViewObjectAnimator.start();

编辑 由于这个问题引起了一些关注,让我解释一下为什么使用ObjectAnimator 而不是其他过渡动画师

使用ObjectAnimator 的好处是它同时移动了项目的可见区域和可点击区域,如果您使用其他动画方法,例如 Transition Animation 或其他一些 Animator,假设您要移动Button从屏幕左下角移动到左上角,它只会移动可见区域而不是Button本身,可点击区域仍然在之前的位置,这种情况下可点击区域仍然是在您移动按钮的左下角而不是左上角。

如果您对ObjectAnimator 执行相同操作,则可见区域和可点击区域都会移动到所需的位置。

【讨论】:

    【解决方案2】:

    试试这个简单的旋转动画应用于图像。

     ImageView imageview = (ImageView)findViewById(R.id.myimage);
     RotateAnimation rotate = new RotateAnimation(180, 360, Animation.RELATIVE_TO_SELF,    
     0.5f,  Animation.RELATIVE_TO_SELF, 0.5f);
      rotate.setDuration(500);
     imageview.startAnimation(rotate);
    

    这个答案只是为了提问,可点击区域与View的当前位置不同是正确的。请检查此问题以使可点击区域正确。 Button is not clickable after TranslateAnimation

    【讨论】:

    • 谢谢。但我不想将drawble放到其他视图中,比如ImageView等,而是想在视图的子类(不是ViewGroup)上绘制它。
    • @marine8888 我认为你弄错了,ImageView 只是View 的子类,而不是ViewGroup(它本身是View 的子类)。您可以创建自己的 View 子类并将可绘制对象渲染到画布上,但这将与 ImageView 完成相同的工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-17
    • 2012-02-01
    • 2016-08-21
    • 2014-12-18
    相关资源
    最近更新 更多