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.

Implement a TextView with an animation in its left side的更多相关文章

  1. 用开源项目ExpandableTextView打造可以下拉扩展的TextView

    这次还是用开源项目来实现效果,我个人觉得上面的这个效果还是很赞的.于是就记录下如何实现这个效果,其实相当简单.这就是开源项目写的好的例子,整个开源项目的代码十分清晰,逻辑和代码结构都很棒,接入自己的工 ...

  2. IOS 开发教程

    http://www.raywenderlich.com/category/ios http://www.raywenderlich.com/50310/storyboards-tutorial-in ...

  3. 【转】Android 实现蘑菇街购物车动画效果

    原文出处:http://blog.csdn.net/wangjinyu501/article/details/38400479 1.思路   目前想到两种方式实现这种效果,一是使用Tween动画,直截 ...

  4. 手势识别官方教程(4)在挑划或拖动手势后view的滚动用ScrollView和 HorizontalScrollView,自定义用Scroller或OverScroller

    简单滚动用ScrollView和 HorizontalScrollView就够.自定义view时可能要自定义滚动效果,可以使用 Scroller或 OverScroller Animating a S ...

  5. android程序启动画面之Splash总结[转]

    方法一: 很多应用都会有一个启动界面.欢迎画面慢慢隐现,然后慢慢消隐.实现这种效果的方法有两种(暂时只发现两种)1.使用两个Activity,程序启动时候load第一张Activity,然后由tick ...

  6. 使用CAShapeLayer来实现圆形图片加载动画[译]

    原文链接 : How To Implement A Circular Image Loader Animation with CAShapeLayer 原文作者 : Rounak Jain 译文出自 ...

  7. Android:简单的弹幕效果达到

    首先,效果图.分类似至360检测到的骚扰电话页面: 布局非常easy,上面是一个RelativeLayout,以下一个Button. 功能: (1)弹幕生成后自己主动从右側往左側滚动(Translat ...

  8. Android 实现蘑菇街购物车动画效果

    版本号:1.0  日期:2014.8.6 版权:© 2014 kince 转载注明出处   使用过蘑菇街的用户基本上都知道有一个增加购物车的动画效果,此处不详细描写叙述想知道的能够去下载体验一下. 1 ...

  9. android---动画入门(一)

    android 动画分为两类,View Animation(视图动画)和property Animation(属性动画),View Animation(视图动画)包含了Tween Animation和 ...

随机推荐

  1. Java多线程——线程范围内共享变量

    多个线程访问共享对象和数据的方式 1.如果每个线程执行的代码相同,可以使用同一个Runnable对象,这个Runnable对象中有那个共享数据,例如,买票系统就可以这么做. package java_ ...

  2. Java数据库——JDBC 2.0操作

    可滚动的结果集 让结果集滚动起来 //================================================= // File Name : JDBC20_demo //-- ...

  3. Java——private default protected public访问控制权限

    访问控制权限 protected范例

  4. Python + OpenCV2 系列:1 - 配置

    Python+OpenCV2+Eclipse+Windos 8.1(32bits): 最初的目的是做图像处理,opencv强大的社区支持,让我想从matlab转到opencv框架下进行试验,而Pyth ...

  5. $this->display输出模板

    1.public function index(){ $this->display() } // 默认输出的是index.html模板 2.public function index(){ $t ...

  6. fixed的left:50%,漂浮

    .floor-box{width: 44px; border: 1px solid #ccc; position: %; z-index: } 漂浮距离,距中间50% .floor-box{width ...

  7. 使用Topshelf 开发windows服务

    在业务系统中,我们为了调度一些自动执行的任务或从队列中消费一些消息,所以基本上都会涉及到后台服务的开发.如果用windows service开发,非常不爽的一件事就是:调试相对麻烦,而且你还需要了解 ...

  8. php self与static的区别

    self vs static 用一个demo来直接说明self与static的区别.self示例: <?phpclass Vehicle {    protected static $name ...

  9. Apache Maven 入门篇 ( 上 )

    作者:George Ma 写这个 maven 的入门篇是因为之前在一个开发者会的动手实验中发现挺多人对于 maven 不是那么了解,所以就有了这个想法. 这个入门篇分上下两篇.本文着重动手,用 mav ...

  10. jquery-ui 的 主题 选择什么颜色? 建议使用html5 的标准进行书写, 更简洁!

    jQuery ui有多种主体, 基本上, 不能使用 no theme 的"主题包" base: 是基本的, 颜色以深灰色为主, 高亮显示为蓝色, ui lightness(明快) ...