可绘制对象上的 ObjectAnimator

bre*_*123 5 android objectanimator android-drawable

ObjectAnimator我想在 a 上使用 an Drawable(而不是 a View)。假设我有一个ImageViewand 以 aDrawable作为源:

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:background="@color/myColor"
    android:src="@drawable/myDrawable/>
Run Code Online (Sandbox Code Playgroud)

我想要的是只有myDrawable动画,而不是例如在ImageView.

现在,如果我ObjectAnimator在可绘制对象上应用 an ,则什么也不会发生:

ImageView imageView = (ImageView) findViewById(R.id.imageView);
Drawable myDrawable = imageView.getDrawable();
ObjectAnimator animator = ObjectAnimator.ofFloat(myDrawable, "alpha", 1f, 0f);
animator.setDuration(1000);
animator.start();
Run Code Online (Sandbox Code Playgroud)

为什么?

bre*_*123 3

感谢 @pskink 评论,我弄清楚了问题所在: Drawablealpha 属性为 anint而不是float

ObjectAnimator animator = ObjectAnimator.ofInt(myDrawable, "alpha", 255, 0);
Run Code Online (Sandbox Code Playgroud)