Andreas Wong
Andreas Wong

Reputation: 60506

Animating TextView's leftDrawable using ObjectAnimator

I would like to animate leftDrawable of a TextView, I tried the following:

ObjectAnimator.ofFloat(drawable, "rotation", 0f, 360f).setDuration(300).start();
button.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);

But it doesn't seem to animate, in the past I've used AnimationDrawable and it works by putting several drawables in a <animation-list>, but apk size is a concern so I'm trying to avoid having multiple assets to achieve this.

Please let me know if I've done something wrong or this is not achievable using ObjectAnimator.

Thanks.

Upvotes: 6

Views: 2539

Answers (2)

pskink
pskink

Reputation: 24720

try this:

    TextView tv = new TextView(this);
    Drawable left = getResources().getDrawable(R.drawable.rotate);
    tv.setCompoundDrawablesWithIntrinsicBounds(left, null, null, null);
    final ObjectAnimator animator = ObjectAnimator.ofInt(left, "level", 0, 10000).setDuration(1000);
    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(TAG, "onClick ");
            animator.start();
        }
    };
    tv.setOnClickListener(listener);
    tv.setGravity(Gravity.CENTER);
    tv.setTextSize(48);
    tv.setText("click me");
    setContentView(tv);

res/drawable/rotate.xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0" android:toDegrees="720">
    <bitmap android:src="@drawable/ic_launcher"/>
</rotate>

Upvotes: 10

yshahak
yshahak

Reputation: 5096

I think that the problem is you call the animation to start before the drawable is drawn. Try replace the order of the lines. Maybe you could add some delay.

Upvotes: 1

Related Questions