我想在ObjectAnimator
上使用Drawable
(而不是View
)。
假设我有一个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
上的背景。现在,如果我对drawable应用
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
的alpha属性是int
而不是float
。
ObjectAnimator animator = ObjectAnimator.ofInt(myDrawable, "alpha", 255, 0);