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.

<TextView
     … …
     android:drawablePadding=”10dip”
     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.

loading_animation.xml:

<animation-list xmlns:android=”http://schemas.android.com/apk/res/android”
      android:oneshot=”false” >
      <item
           android:drawable=”@drawable/loading_1″
           android:duration=”500″>
       </item>
       <item
            android:drawable=”@drawable/loading_2″
            android:duration=”500″>
        </item>
        <item
             android:drawable=”@drawable/loading_3″
             android:duration=”500″>
        </item>
</animation-list>

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:

<TextView
      … …
      android:drawablePadding=”10dip”
      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];
       loadingAnimation.start();
}

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

https://github.com/wuyisheng/pagedown 最近还在在写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

http://forums.macrumors.com/showthread.php?t=1091071 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...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)