[android] 介面切換的簡單動畫,android切換
1. 建立個位移動畫的xml檔案
Activity中開啟動畫
使用AnimationUtils類載入動畫資源檔案
left_to_right.xml
<?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="100%" android:toXDelta="0" android:duration="3000" ></translate>
View child=new IndexView(this).getMemberView();child.startAnimation(AnimationUtils.loadAnimation(this, R.anim.left_to_right));
2. 淡入淡齣動畫
當前淡出介面和執行時間
淡出過程中,淡入介面處於等待狀態
第二個介面淡入和他的執行時間
第一個介面執行完成後,要刪除掉
package com.tsh.lottery.utils;import android.support.v4.view.ViewPager;import android.view.View;import android.view.ViewGroup;import android.view.ViewParent;import android.view.animation.AlphaAnimation;import android.view.animation.Animation;import android.view.animation.Animation.AnimationListener;public class FadeUtil { /** * 淡出介面 * @param view 介面 * @param duration 執行時間 */ public static void fadeOut(final View view,long duration){ AlphaAnimation alphaAnimation=new AlphaAnimation(1, 0); alphaAnimation.setDuration(duration); view.startAnimation(alphaAnimation); //監聽動畫結束,刪除View元素 alphaAnimation.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { ViewGroup vg=(ViewGroup) view.getParent(); vg.removeView(view); } }); } /** * 淡入介面 * @param view 介面 * @param delay 延遲時間 * @param duration 執行時間 */ public static void fadeIn(View view,long delay,long duration){ AlphaAnimation alphaAnimation=new AlphaAnimation(0, 1); //設定開始時間延遲 alphaAnimation.setStartOffset(delay); alphaAnimation.setDuration(duration); view.startAnimation(alphaAnimation); }}
FadeUtil.fadeOut(child, 2000);
FadeUtil.fadeIn(child, 2000,2000);