Implement a TextView with an animation in its left side.

原创 2013年12月05日 11:18:55

In my case, I want to write a TextView with an animation in its left side.

ImageView + TextView could work but it’s not frugal enough.

TextView with drawableLeft would be the best option.

     … …
     android:drawableLeft=”@drawable/loadingicon” />

But above implementation just show a icon in the left of TextView, not an animation.

How to show an animation instead of a icon?

Assume you have 4 pictures which would be an animation if display them one after one. Then define animation xml in res/anim folder in your project.


<animation-list xmlns:android=””
      android:oneshot=”false” >

android:oneshot” determines whether or not play the animation just once.
android:duration” determines the duration of pictures switching.

Then in layout xml, use above animation like this:

      … …
      android:drawableLeft=”@anim/loading_animation” />

Bingo? no, no, no, the animation can not switching yet.

You need to do more.

In java/Activity code, you must get the animation drawable and start it.

Drawable[] draws = locateAreaTextView.getCompoundDrawables();
if (draws != null && draws.length > 0 && draws[0] instanceof AnimationDrawable) {
       loadingAnimation = (AnimationDrawable) draws[0];

In above, we get index 0 of the drawable array since drawableLeft is in 0 position of the array.

Now, the animation works. But there is one more trap.

If you put above codes in onCreate() of Activity, draws will be a [null, null, null, null] array.

You must put it in onResumse() of Activity.

版权声明:本文为博主原创文章,未经博主允许不得转载。 举报


A Markdown implement in browser 最近还在在写kotlin社区,在写网站的东西,社区需要一个基本的博客编辑器,今天动手做了一下。Markdown编辑器是有一些...

多表合计查询 with 写法

补充数据: 1、产品表,Goods,字段ID/Name。 A001/B2铅笔 2、入货表,DetailIn,字段ID/GoodsId/Quantity。 1/A001/100 ...

精选:深入理解 Docker 内部原理及网络配置

网络绝对是任何系统的核心,对于容器而言也是如此。Docker 作为目前最火的轻量级容器技术,有很多令人称道的功能,如 Docker 的镜像管理。然而,Docker的网络一直以来都比较薄弱,所以我们有必要深入了解Docker的网络知识,以满足更高的网络需求。

poj1483 It's not a Bug, It's a Feature!

It's not a Bug, It's a Feature! Time Limit: 5000MS   Memory Limit: 30000K Total Submissions: 1...

A way to implement a general purpose LinkedList in C

C is not C++, so we cannot use template of C++ to achieve this. There're more than one way to achie...
  • xuhx
  • xuhx
  • 2012-02-23 16:02
  • 505

A prompt box with an arrow

a prompt box with an arrow

iPhone: How to switch tabs with an animation?

问题I'm switching tabs programmatically in a tab bar driven application using UITabBarController.selec...

In SQL, what is the difference between a left join and a left outer join?

There is actually no difference between a left join and a left outer join – they both refer to the e...

Update a driver in an initrd file

Update a driver in an initrd file Recently I found myself wanting to put SUSE Linux Enterprise De...

a NavigationController in the details side within MGSplitViewController Just wondering if anybody has ma...
  • gnicky
  • gnicky
  • 2012-04-11 13:30
  • 1014

How can I connect Unity to an SQL database in order to implement an MMO?

问: Hello, first of all I'd like to thank each and everyone who has dedicated their time into pr...