Implement a TextView with an animation in its left side.

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.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值