我想在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);

07-24 09:49
查看更多