我想在 Drawable
(不是 View
)上使用 ObjectAnimator
。
假设我有一个 ImageView
和一个 Drawable
作为来源:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:background="@color/myColor"
android:src="@drawable/myDrawable/>
我想要的是只有 myDrawable
是动画的,而不是例如 ImageView
上设置的背景。
现在,如果我在可绘制对象上应用 ObjectAnimator
,则什么也不会发生:
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Drawable myDrawable = imageView.getDrawable();
ObjectAnimator animator = ObjectAnimator.ofFloat(myDrawable, "alpha", 1f, 0f);
animator.setDuration(1000);
animator.start();
为什么?
最佳答案
感谢@pskink 的评论,我弄清楚了问题所在:
Drawable
具有作为 int
而不是 float
的 alpha 属性。
ObjectAnimator animator = ObjectAnimator.ofInt(myDrawable, "alpha", 255, 0);
关于android - Drawable 对象上的 ObjectAnimator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36624556/