Android 模仿iPhone列表數(shù)據(jù)View刷新動畫詳解
因?yàn)槲冶救撕芟矚g在不同的頁面之間跳轉(zhuǎn)時(shí)加點(diǎn)好玩的動畫,今天無意間看到一個(gè)動畫效果感覺不錯(cuò),幾種效果圖如下:既然好玩就寫在博客中,直接說就是:該效果類似于iPhone中View的切換動畫效果,今天就只介紹上面展示的效果。
廢話不多說,先上效果,再看代碼!!
效果一:

效果二:

效果三:

效果四:(犯錯(cuò)的效果):

效果五(回旋效果一):

效果六(回旋效果二):

效果看完了,就來看下上面效果實(shí)現(xiàn)的具體代碼吧, 中間會把我自己試驗(yàn)的、犯的錯(cuò)誤都以注釋的形式寫下來的, 大家使用的時(shí)候別出錯(cuò)就行了!先來看下使用的布局文件,很簡單的布局:
XML/HTML代碼
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ListView android:id="@+id/firstPage" android:layout_width="fill_parent" android:layout_weight="1.0" android:layout_height="0dip"/> <ListView android:id="@+id/secondPage" android:layout_width="fill_parent" android:layout_weight="1.0" android:layout_height="0dip" android:visibility="gone"/> <Button android:id="@+id/startNext" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/next" /> </LinearLayout>
XML/HTML代碼
<strong> 下面再來看下實(shí)現(xiàn)以上效果的具體代碼,代碼中所標(biāo)的順序與上面顯示的效果圖一致:</strong>
Java代碼
package com.xiaoma.www;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.CycleInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.view.animation.OvershootInterpolator;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
/**
* @Title: BetweenAnimationActivity.java
* @Package com.xiaoma.www
* @Description: 小馬學(xué)習(xí)模仿iPhone列表分頁旋轉(zhuǎn)刷新
* @author XiaoMa
*/
public class BetweenAnimationActivity extends Activity implements OnClickListener {
/**資源聲明*/
private Button startNext = null ;
private ListView firstPage = null ;
private ListView secondPage = null ;
/**列表項(xiàng)聲明*/
private static final String firstItem[] =
{"海闊人生","光輝歲月","無盡空虛","真的愛你","歲月無聲","灰色軌跡","再見理想"};
private static final String secondItem[] =
{"洗唰唰","愛啦啦","喜歡你","娃哈哈","小馬果","大壞蛋","冷雨夜"};
/**列表頁面切換動畫插值器聲明一*/
private Interpolator accelerator = new AccelerateInterpolator();
private Interpolator decelerator = new DecelerateInterpolator();
/**動畫插值器二:效果五與效果六都為以下插值器*/
private Interpolator accelerator1= new CycleInterpolator(45f);
private Interpolator decelerator1= new OvershootInterpolator();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/**
* 這個(gè)地方寫下,大家盡量不要在onCreate方法中寫太多的操作,
* 如果涉及到很多配置問題時(shí)有些屬性設(shè)置必須在onCreate()方法中
* 寫,比如:全屏、橫豎屏必須在setContentView()前面寫,
* 如果在onCreate()方法中寫太多東西的,一句話:太亂??!
* */
init();
}
/**
* 初始化實(shí)現(xiàn)
*/
private void init(){
/**資源定位,添加監(jiān)聽*/
startNext = (Button)findViewById(R.id.startNext);
startNext.setOnClickListener(this);
firstPage = (ListView)findViewById(R.id.firstPage);
secondPage = (ListView)findViewById(R.id.secondPage);
ArrayAdapter<String> firstAdapter = new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1,firstItem);
ArrayAdapter<String> secondAdapter = new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, secondItem);
firstPage.setAdapter(firstAdapter);
secondPage.setAdapter(secondAdapter);
}
@Override
public void onClick(View v) {
changePage();
}
//實(shí)現(xiàn)列表頁面切換
private void changePage() {
final ListView visiable ;
final ListView invisiable ;
if(firstPage.getVisibility() == View.GONE){
visiable = secondPage ;
invisiable = firstPage ;
}else{
visiable = firstPage ;
invisiable = secondPage ;
}
//這個(gè)地方大家可能看到了ObjectAnimator這個(gè)類,一開始我也不知道是什么東西,很簡單,查官方文檔,官方文檔中的解釋一堆英文,我//一直說的,我英文爛的要死,但不怕,只要你想,就肯定可以查出來的,大家 只看一句:該類是 ValueAnimator的子類,可以根據(jù)給定//的屬性名稱給目標(biāo)對象設(shè)置動畫參數(shù)
//效果一(此處效果順序與效果圖一一對應(yīng))
//final ObjectAnimator invisToVis = ObjectAnimator.ofFloat(invisiable, "rotationX",-90f, 0f);
ObjectAnimator visToInvis = ObjectAnimator.ofFloat(visiable, "rotationX", 0f, 90f);
//效果二
final ObjectAnimator invisToVis = ObjectAnimator.ofFloat(invisiable, "rotationY",-90f, 0f);
ObjectAnimator visToInvis = ObjectAnimator.ofFloat(visiable, "rotationY", 0f, 90f);
//效果三(這個(gè)地方的alpha屬性值大家只記一點(diǎn):值越大越不透明就可以了?。?!)
//final ObjectAnimator invisToVis = ObjectAnimator.ofFloat(invisiable, "alpha", 0.0f, 1.0f );
//ObjectAnimator visToInvis = ObjectAnimator.ofFloat(visiable, "alpha", 1.0f, 0.0f );
//效果四(此于是我犯的一個(gè)錯(cuò)誤,很天真的以為應(yīng)該也有rotationZ屬性名稱,其實(shí)是錯(cuò)的,在ofFloat參數(shù)中并無此屬性名稱,但大家還//是可以看到列表正常,其實(shí)顯示 效果很不正常了因?yàn)楹笈_已經(jīng)報(bào)錯(cuò),但應(yīng)用仍然不會停止 ,照常運(yùn)行,但效果僅僅是兩個(gè)ListView直接//替換,并無任何動畫添加到其中,這個(gè)地方大家注意下): ObjectAnimator.ofFloat(invisiable, "rotationZ",-90f, 0f);
visToInvis.setDuration(500);
visToInvis.setInterpolator(accelerator);
invisToVis.setDuration(500);
invisToVis.setInterpolator(decelerator);
//這個(gè)地方記錄下,下面這個(gè)監(jiān)聽器小馬第一次見到,查閱官方文檔解釋如下:此監(jiān)聽來監(jiān)聽動畫的生命周期如:開始、結(jié)束、正在播放、循//環(huán)播放等 ,此處切記: Animation是不可以監(jiān)聽動畫的,它只負(fù)責(zé)動畫的
visToInvis.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator anim) {
/*
* 列舉幾個(gè)動畫的監(jiān)聽:
* 一:anim.isRunning(){//TODO}
* 二:anim.isStarted(){//TODO}
* 三:anim.end(){//TODO}
*/
visiable.setVisibility(View.GONE);
invisToVis.start();
invisiable.setVisibility(View.VISIBLE);
}
});
visToInvis.start();
}
}
最后,再說下,文章標(biāo)題中說是分頁動畫,其實(shí)這些動畫并不僅僅局限于分頁上面的,如果大家把插值器、動畫用靈活一點(diǎn)的話, 也可以做出很個(gè)性的帶有很多動畫的應(yīng)用的,再加上Activity之間的動畫與以上這些結(jié)合的話就更完美了,Activity之間的動畫大家可以參照我之前寫的這篇文章(連接如下),希望對大家有所幫助。
相關(guān)文章
解決Android應(yīng)用冷啟動時(shí)出現(xiàn)的白屏問題的方法
本篇文章主要介紹了解決Android應(yīng)用冷啟動時(shí)出現(xiàn)的白屏問題的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Android之有效防止按鈕多次重復(fù)點(diǎn)擊的方法(必看篇)
下面小編就為大家?guī)硪黄狝ndroid之有效防止按鈕多次重復(fù)點(diǎn)擊的方法(必看篇)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04
Andriod arcgis保存Mapview為圖片的實(shí)例代碼
這篇文章主要介紹了Andriod arcgis保存Mapview為圖片的實(shí)例代碼 的相關(guān)資料,需要的朋友可以參考下2016-03-03
Android原生側(cè)滑控件DrawerLayout使用方法詳解
這篇文章主要為大家詳細(xì)介紹了Android原生側(cè)滑控件DrawerLayout的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android Compose實(shí)現(xiàn)聯(lián)系人列表流程
聲明式UI,更簡單的自定義,實(shí)時(shí)帶交互的預(yù)覽功能Compose并不是類似于Recyclerview的高級控件,而是直接拋棄了View,ViewGroup那套東西,從上到下魯了一套全新的框架,直白點(diǎn)說就是它的渲染機(jī)制,布局機(jī)制,觸摸算法,以及UI具體寫法全都是新的2023-03-03
自定義View系列之kotlin繪制手勢設(shè)置溫度控件的方法
這篇文章主要給大家介紹了關(guān)于自定義View系列之kotlin繪制手勢設(shè)置溫度控件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07

