本文介绍了使用ObjectAnimator动画TextView的leftDrawable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为 TextView leftDrawable 制作动画,我尝试了以下操作:

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

但这似乎没有动画效果,过去我使用过 AnimationDrawable ,它可以通过将几个可绘制对象放入< animation-list> 来工作,但是apk大小是一个问题,因此我尝试避免拥有多个资产来实现这一目标。

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.

请告知我是否做错了什么,或者使用 ObjectAnimator无法实现此目的

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

谢谢。

推荐答案

请尝试以下操作:

    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看起来像这样:

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>

这篇关于使用ObjectAnimator动画TextView的leftDrawable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 21:12