如何将动画添加到textView drawable

问题描述:

我也使用此行在我的textView中添加图像: android:drawableLeft = @ drawable / ic_launcher
在我的xml文件中。

I have used this line too add image in my textView : android:drawableLeft="@drawable/ic_launcher" in my xml file.

   <TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:drawablePadding="5dp"
    android:gravity="center_vertical"
    android:text="@string/hello_world"
    android:textSize="14sp"
    android:textStyle="bold"
    android:drawableLeft="@drawable/ic_launcher"
    >

</TextView>

现在我想向此可绘制对象添加动画。
我不知道如何访问此图像。

Now I want to add animation to this drawable. I dont know how can I access this image.

有帮助吗?
预先感谢

Any help? thanks in advance

如果您在XML中设置了drawable,则将无法访问它就像使用 ImageView getDrawable()一样。而是从XML中忽略它,并在 Activity / Fragment 中进行操作:

if you set the drawable in the XML, you won't be able to access it like you can with an ImageView's getDrawable(). Instead, omit it from your XML and do it in your Activity/Fragment:

TextView tv = (TextView) view.findViewById(R.id.textView1);
AnimationDrawable d = (AnimationDrawable) getResources().getDrawable(R.drawable.ic_launcher);
tv.setCompoundDrawables(d, null, null, null);
d.start();

提供了可绘制的 ic_launcher 动画 AnimationDrawable ,这应该开始动画。调用 d.stop()停止动画。

Provided your drawable ic_launcher can be animated like an AnimationDrawable, this should start the animation. Call d.stop() to cease animation.