Drawable 对象上的 ObjectAnimator
ObjectAnimator on Drawable object
我想在 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
的 alpha 属性 作为 int
而不是 float
。
ObjectAnimator animator = ObjectAnimator.ofInt(myDrawable, "alpha", 255, 0);
我想在 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
的 alpha 属性 作为 int
而不是 float
。
ObjectAnimator animator = ObjectAnimator.ofInt(myDrawable, "alpha", 255, 0);