因为我本人很喜欢在不同的页面之间跳转时加点好玩的动画,今天无意间看到一个动画效果感觉不错,几种效果图如下:既然好玩就写在博客中,直接说就是:该效果类似于iPhone中View的切换动画效果,今天就只介绍上面展示的效果。
废话不多说,先上效果,再看代码!!
效果一:
效果二:
效果三:
效果四:(犯错的效果):
效果五(回旋效果一):
效果六(回旋效果二):
效果看完了,就来看下上面效果实现的具体代码吧, 中间会把我自己试验的、犯的错误都以注释的形式写下来的, 大家使用的时候别出错就行了!先来看下使用的布局文件,很简单的布局:
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> 下面再来看下实现以上效果的具体代码,代码中所标的顺序与上面显示的效果图一致:</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;
-
-
-
-
-
-
-
-
- public class BetweenAnimationActivity extends Activity implements OnClickListener {
-
-
- private Button startNext = null ;
- private ListView firstPage = null ;
- private ListView secondPage = null ;
-
-
- 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();
-
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
-
-
-
-
-
-
-
- init();
- }
-
-
-
-
- private void init(){
-
-
- 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();
- }
-
-
-
- private void changePage() {
-
- final ListView visiable ;
- final ListView invisiable ;
-
- if(firstPage.getVisibility() == View.GONE){
- visiable = secondPage ;
- invisiable = firstPage ;
- }else{
- visiable = firstPage ;
- invisiable = secondPage ;
- }
-
-
-
-
-
- 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);
-
-
-
-
-
-
-
- visToInvis.setDuration(500);
- visToInvis.setInterpolator(accelerator);
- invisToVis.setDuration(500);
- invisToVis.setInterpolator(decelerator);
-
-
- visToInvis.addListener(new AnimatorListenerAdapter() {
- @Override
- public void onAnimationEnd(Animator anim) {
-
-
-
-
-
-
-
-
- visiable.setVisibility(View.GONE);
- invisToVis.start();
- invisiable.setVisibility(View.VISIBLE);
- }
- });
- visToInvis.start();
- }
-
- }
最后,再说下,文章标题中说是分页动画,其实这些动画并不仅仅局限于分页上面的,如果大家把插值器、动画用灵活一点的话, 也可以做出很个性的带有很多动画的应用的,再加上Activity之间的动画与以上这些结合的话就更完美了,Activity之间的动画大家可以参照我之前写的这篇文章(连接如下),希望对大家有所帮助。