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. js正则匹配浮点数或整数

    var pos='point(12.4 -45.423453)';var re = /([+]\d+[.]\d+|[-]\d+[.]\d+|\d+[.]\d+|[+]\d+|[-]\d+|\d+)/i ...

  2. Javascript权威指南——第二章词法结构,第三章类型、值和变量,第四章表达式和运算符,第五章语句

    第二章 词法结构 一.HTML并不区分大小写(尽管XHTML区分大小写),而javascript区分大小写:在HTML中,这些标签和属性名可以使用大写也可以使用小写,而在javascript中必须小写 ...

  3. Vim编辑器

    vim的学习曲线相当的大(参看各种文本编辑器的学习曲线),所以,如果你一开始看到的是一大堆VIM的命令分类,你一定会对这个编辑器失去兴趣的.下面的文章翻译自<Learn Vim Progress ...

  4. json 是个什么东西?

    JSONP原理 JSONP(JSON with Padding),就是异步请求跨域的服务器端时,不是直接返回数据,而是返回一个js方法,把数据作为参数传过来.如果只是跨域传递数据那么这种方式是比较好的 ...

  5. MySQL 视图的基础操作(五)

    1.为什么使用视图:     为了提高复杂SQL语句的复用性和表操作的安全性(例如:工资字段不想展示给所有能查看该查询结果的人),MySQL提供了视图特性.所谓视图,本质上是一种虚拟表,其内容与真实的 ...

  6. 固定IP 正常访问谷歌

    如题 地址栏直接输入 http://173.194.1.150/ 正常使用 ~标记一下~

  7. jquery取消选择select下拉框

    有三个select下拉框一个大类,两个小类隐藏,需要在选择大类的时候,小类显示同时清除另外的小类选择的项这需求有点儿.......... 下面是三个select: <select name=&q ...

  8. Main函数 & Autoreleasepool

    如同任何基于C的应用程序,程序启动的主入口点为iOS应用程序的main函数.在iOS应用程序,main函数的作用是很少的.它的主要工作是控制UIKit framework.因此,你在Xcode中创建任 ...

  9. asp.net mvc 4 高级编程学习笔记:第三章 视图(1)

    1.基础规则 视图的职责是向用户提供用户界面. 视图位于View目录下:有普通的需要控制器渲染的视图,有局部视图,有布局视图等各种视图. 2.视图渲染 控制器默认情况下渲染与控制器同名的目录内的与Ac ...

  10. PostgreSQL建立分区表示例

    pgsql 分区表: --主表 create table test(id integer, name varchar(32)); create index idx_test_id on test us ...