Android5.0 ListView特效的簡單實現

來源:互聯網
上載者:User

標籤:android   style   blog   http   ar   io   color   os   sp   

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特效的簡單實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.