Android5.0中对于动画可所谓是情有独钟,在设计规范中大量展现了listview的动画,其实也就是一个目的:将items动画显示出来。这个看起来很炫的效果,其实实现也蛮简单的,我下面就来用动画简单实现一下。

一、在xml文件中建立动画文件

这一步我推荐在xml中写动画,好处是你整个应用都可以调用这一种效果,保证了风格而且减少冗余。对于动画我一贯的态度是:简单的动画用xml文件,复杂的动画用ObjectAnimation。

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator"
android:shareInterpolator="true"> <translate
android:duration="1300"
android:fromXDelta="0%"
android:fromYDelta="100%"
android:toXDelta="0%"
android:toYDelta="0%" /> <alpha
android:duration="1300"
android:fromAlpha="0"
android:toAlpha="1.0" /> </set>

这里可以看见我写了两个动画,一个是从无到有渐变的,一个是从下到上的移动。为了方便演示,我把动画时间弄得比较长了。

二、在代码中进行配置

package com.example.googleplusliststyle;

import java.util.Arrays;
import java.util.List;
import java.util.Locale; import android.app.Activity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.ArrayAdapter;
import android.widget.ListView; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); final ListView listView = (ListView)findViewById(R.id.listView); List<String> data = Arrays.asList(Locale.getISOCountries());// get demo list data
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.item, R.id.myTextView,data);
listView.setAdapter(adapter); Animation animation = AnimationUtils.loadAnimation(this, R.anim.from_bottom_to_top);
final LayoutAnimationController controller = new LayoutAnimationController(animation, 0);
listView.setLayoutAnimation(controller);
listView.setDivider(null);
//listView.startAnimation(animation); } }

代码也很简单,首先加载布局文件中的listview,写好item,然后通过LayoutAnimationController来配置动画,最后让listview加载动画。

LayoutAnimationController构造函数中我们主要看第二个参数,如果设置0的话,所有item都是同时进行动画的,如果是1的话,就会让item一个接一个显示动画。

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" > <ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" > </ListView> </RelativeLayout>

item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:id="@+id/myTextView"
android:layout_margin="16dp"
android:textSize="18sp"/> <View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_below="@id/myTextView"
android:background="#888888"/> </RelativeLayout>

三、另一种办法:在布局文件中设置动画

如果想要在xml中运用动画的话,我们就需要再建立一个动画文件

anim_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:animation="@anim/from_bottom_to_top"
android:animationOrder="normal"
android:delay="0" />

这里的android:animationOrder的取值有normal:0 默认;reverse:1 倒序;random:2 随机。就是给动画进行排序,我设置了noraml。

这个文件引用了之前我们写过的一个动画,等于之前的animation被layoutAnimation包装了一下。

在listView中设置动画

    <ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layoutAnimation="@anim/anim_layout"> </ListView>

关键就是这个layoutAnimation属性,设置上我们刚刚做好的动画就行了。

效果如下:

源码下载:http://download.csdn.net/detail/shark0017/8273763

参考自:

http://blog.csdn.net/jdsjlzx/article/details/7652297

http://blog.csdn.net/jdsjlzx/article/details/7652452

http://blog.csdn.net/lixiaodaoaaa/article/details/8284246

http://droidyue.com/blog/2014/07/26/apply-google-plus-list-style-on-android/

Android5.0 ListView特效的简单实现的更多相关文章

  1. Android5.0以上的项目都会有的按钮点击特效--水波纹

    <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http: ...

  2. 一个Activity掌握Android5.0新控件 (转)

    原文地址:http://blog.csdn.net/lavor_zl/article/details/51279386 谷歌在推出Android5.0的同时推出了一些新控件,Android5.0中最常 ...

  3. android5.0中RecycleView的用法

    最近学习了android5.0中新增的一个组件RecycleView,是用来代替当前的listview开发的,是因为在RecycleView中已经有了viewholder缓存,并且不同的item之间可 ...

  4. Android5.0新控件

    谷歌在推出Android5.0的同时推出了一些新控件,Android5.0中最常用的新控件有下面5种.  1. CardView(卡片视图) CardView顾名思义是卡片视图,它继承FrameLay ...

  5. android5.0 BLE 蓝牙4.0+浅析demo搜索(一)

    作者:Bgwan链接:https://zhuanlan.zhihu.com/p/23341414来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 作者:Bgwan 莳萝花 ...

  6. android5.0联系人 sort_key改成phonebook_label

    项目中用到了联系人根据字母排序,在android4.0手机上是可以的,但是在android4.4以上的手机排序是乱的,一般字母排序都是根据sort_key这个拼音进行排序,而android5.0这个字 ...

  7. Android Material Design Ripple Effect在Android5.0(SDK=21)以下Android版本崩溃问题解决

    Android Material Design Ripple Effect在Android5.0(SDK=21)以下Android版本崩溃问题解决 附录1的Android Ripple Effect水 ...

  8. Android5.0之Activity的转场动画

    Activity的转场动画很早就有,但是太过于单调,样式也不好看,于是Google在Android5.0之后,又推出的新的转场动画,效果还是非常炫的,今天我们一起来看一下. 1.旧转场动画回顾 首先我 ...

  9. Android5.0之NavigationView的使用

    导航菜单的制作方式多种多样,网上也有各种炫酷效果的具体实现方式,那么今天我主要是想来说说Google在Android5.0之后推出的NavigationView的具体使用方式. NavigationV ...

随机推荐

  1. 慢查询日志和profiling

    MySQL调优三步: 慢查询 (分析出现出问题的sql) Explain (显示了mysql如何使用索引来处理select语句以及连接表.可以帮助选择更好的索引和写出更优化的查询语句) Profile ...

  2. [转] map/reduce

    如果你读过Google的那篇大名鼎鼎的论文“MapReduce: Simplified Data Processing on Large Clusters”,你就能大概明白map/reduce的概念. ...

  3. 【LOJ】#2109. 「JLOI2015」骗我呢

    题解 我深思熟虑许久才算是明白个大概的计数问题吧 先是转化成一个矩形,列一条直线y = x,y = x - (m + 1)我们从(0,0)走到(n + m + 1,m + 1)就是答案 因为我们起始相 ...

  4. 7-9The Morning after Halloween uva1601

    这题可以用普通bfs来做  也可以用双向bfs来做(先欠着) 有点类似专题训练的一题   不过那题是找钥匙开门   不过都用了状态压缩 题意:  n,m(<=16) 的网络上有t(<=3) ...

  5. rabbitmq学习(四) —— 发布订阅

    为了说明这种模式,我们将建立一个简单的日志系统.这个系统将由两个程序组成,第一个将发出日志消息,第二个将接收并处理日志消息.在我们的日志系统中,每一个运行的接收程序的副本都会收到日志消息. 交换器(E ...

  6. commonjs,amd,cmd

    在某些库中,经常会看到函数最前面有一个分号.其实是为了防止自动化工具拼接js时,如果前面的js文件的结尾处忘了加分号,拼接出来的代码容易挂,加分号这种行为属于防御式编程. 一个模块就是实现特定功能的文 ...

  7. 【BZOJ-3532】Lis 最小割 + 退流

    3532: [Sdoi2014]Lis Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 704  Solved: 264[Submit][Status] ...

  8. CentOS 7下的KVM网卡配置为千兆网卡

    在KVM下可以生成两种型号的网卡,RTL8139和E1000,其实应该是底层生成不同芯片的网卡,而不是附带宿主机网卡是什么型号就是什么型号的,其中默认为100兆网卡,即RTL8319的螃蟹卡,另一种是 ...

  9. 反编译APK文件的三种方法(转)

    因为学习Android编程的需要,有时我们需要对网络上发布的应用项目进行学习,可是Android项目一般是通过APK文件进行发布的,我们看不到源代码,嘿嘿,办法总会有的,而且不止一个... ps:对于 ...

  10. USBDM Coldfire V2,3,4/DSC/Kinetis Debugger and Programmer -- MC9S08JS16

    Introduction The attached files provide a port of a combined TBLCF/DSC code to a MC9S08JS16 processo ...