如何将动画添加到 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>

现在我想为这个 drawable 添加动画.我不知道如何访问这张图片.

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

如果您的 drawable ic_launcher 可以像 AnimationDrawable 一样进行动画处理a>,这应该开始动画.调用 d.stop() 停止动画.

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