Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
542 views
in Technique[技术] by (71.8m points)

android - how to add animation to textView drawable

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...