Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 737
Andreas Wong Avatar asked Jun 30 '15 06:06

Andreas Wong


2 Answers

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>
like image 130
pskink Avatar answered Nov 03 '22 01:11

pskink


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.

like image 38
yshahak Avatar answered Nov 03 '22 01:11

yshahak