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. easyUI Form表单的密码验证是否相同

    一.js文件中的代码: $(function(){ $.extend($.fn.validatebox.defaults.rules, { equals: {//定义一个比较相等与否的函数 valid ...

  2. easyUI draggable插件使用不当,导致拖动div内部文本框无法输入;设置echarts数据为空时就显示空白,不要动画和文字

    先上一个Demo <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://ww ...

  3. HTML学习笔记——post表单

    1>form1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:// ...

  4. 总结CSS面试题目的考察点及常见布局问题整理

    整理网上流传的若干份面试题目,突发奇想,总结关于CSS面试题目的考察点,发现问题大多围绕几个属性和几种题目,水平有限,仅供参考. 写这个博文内心有种莫名奇妙的自我谴责感,实在不应该把面试层叠样式“应试 ...

  5. Ubuntu terminal 不见了

    Python出问题了,卸载Python后,terminal竟然消失了.. Ctrl + Alt + T出现的是XTerm 解决办法: 在XTerm中输入命令: # sudo apt-get insta ...

  6. nginx配置图片防盗链

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)${ expires 30d; access_log off; valid_referers none blocked ...

  7. C#中事件的使用

    C#中事件的使用  http://www.cnblogs.com/wayfarer/archive/2004/04/20/6712.html 用一个例子来说明事件的使用. 创建一个简单的类,名为Fil ...

  8. jar tvf study.war jar命令查看war/jar包的内容

    jar tvf study.war 0 Thu Oct 20 14:01:18 CST 2016 META-INF/ 137 Thu Oct 20 14:01:16 CST 2016 META-INF ...

  9. centos 7.0 查看根目录下所有文件夹

    centos 7.0最小化安装 第一行是登录 [root@localhost ~]# [root@localhost ~]# cd ../ [root@localhost /]# ls bin dev ...

  10. Python之路【第十四篇】前端补充回顾

    布局和事件 1.布局 首先看下下面的图片: 上面的内容都是居中的,怎么实现这个效果呢,第一种方法是通过float的方式,第二种是通过“div居中的方式” 第一种方式不在复述了,直接看第二种方式: 1. ...