android - 如何通过 ObjectAnimator 旋转可绘制对象?

标签 android rotation drawable objectanimator

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

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

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

对不起,我的英语不好。

最佳答案

尝试使用 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 执行相同操作,则可见区域和可点击区域都会移动到所需的位置。

关于android - 如何通过 ObjectAnimator 旋转可绘制对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14039265/

相关文章:

javascript - 旋转 SVG 文本元素的内容

algorithm - 旋转矩形,使它们保持与 Canvas 的相对位置

Android WebView,错误 net::Name_Not_Resolved

android - 如何在 ConstraintLayout 中使用创建者

algorithm - 顺时针旋转矩阵 90*n 度

android - "How to pass a parameter into a drawable"或 "Create button with gradient and changeable image"

android - 在 Android 中获取 double 变量的指南百分比值

来自 URL 的 Android 可绘制图像

android - 仅在发布版本中将 screenOrientation 设置为 "portrait"

Android 浏览器对 HTML 和 CSS 的限制