Search code examples
androidandroid-drawableobjectanimator

ObjectAnimator on Drawable object


I want to use an ObjectAnimator on a Drawable (not a View). Let's say that I have an ImageView and with a Drawable as source:

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:background="@color/myColor"
    android:src="@drawable/myDrawable/>

What I want is that only myDrawable is animated and not for example the background set on the ImageView.

Now if I apply an ObjectAnimator on the drawable nothing happens:

ImageView imageView = (ImageView) findViewById(R.id.imageView);
Drawable myDrawable = imageView.getDrawable();
ObjectAnimator animator = ObjectAnimator.ofFloat(myDrawable, "alpha", 1f, 0f);
animator.setDuration(1000);
animator.start();

Why?


Solution

  • Thanks to @pskink comments I figured out what was the problem: Drawable has the alpha property as an int and not float.

    ObjectAnimator animator = ObjectAnimator.ofInt(myDrawable, "alpha", 255, 0);