可绘制对象的ObjectAnimator

5

我想在一个 Drawable (而不是一个 View)上使用 ObjectAnimator。 假设我有一个带有 Drawable 资源的 ImageView:

<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();

为什么?

@Ricardo AnimationDrawable 不是 ObjectAnimator,它强制你必须有多个 Drawable 作为动画帧。 - brescia123
@Nanoc 这不是我想要的。我希望它也可以在ButtonTextView的复合可绘图上正常工作。 - brescia123
你可以在任何视图上执行此操作,例如按钮或文本视图。如果你只想要图片动画而不是整个控件,则这不是正确的方法。 - Nanoc
1
将“ofFloat”替换为“ofInt”,并更改“int... values”的内容。 - pskink
1
不,不是“旋转”,文档中写道:`一个可根据当前 level 值旋转另一个 Drawable 的 Drawable。可以控制旋转的起始和结束角度,以将任何圆弧映射到 level 值范围内。它可以在 XML 文件中使用 <rotate> 元素来定义,因此它会基于 level 值旋转另一个 drawable。` - pskink
显示剩余10条评论
1个回答

3
感谢 @pskink 的评论,我找到了问题所在: Drawable 的透明度属性是 int 类型而不是 float
ObjectAnimator animator = ObjectAnimator.ofInt(myDrawable, "alpha", 255, 0);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接