zoukankan      html  css  js  c++  java
  • 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.

    1
    2
    3
    4
    
    <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:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    
    <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:

    1
    2
    3
    4
    
    <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.

    1
    2
    3
    4
    5
    
    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.

  • 相关阅读:
    IOS-UI- UIScrollView 滚动视图(1)
    git实用攻略(二)
    git实用攻略
    一些技术发展和职业规划的建议
    Spring Data JPA 事务
    配置Slf4j依赖,桥接各种多个日志组件(排除commons-logging依赖的影响)
    Apache Shiro去掉URL中的JSESSIONID
    浏览器缓存介绍之sessionStorage、localStorage、Cookie
    【转】大数据批处理框架 Spring Batch全面解析
    ssl和https协议详解
  • 原文地址:https://www.cnblogs.com/mosthink/p/5288908.html
Copyright © 2011-2022 走看看