標籤:
以下博文講解比較詳細,可查閱:
http://www.360doc.com/content/13/0102/22/6541311_257754535.shtml
幾個關鍵屬性:
setRepeatCount(int repeatCount);//設定動畫重複執行的次數
setStartOffSet(long startOffSet);//設定動畫執行之前的等待時間。
setFillBefore(Boolean fillBefore);//為true則動畫執行完成後,回到最初的狀態。
setFillAfter(Boolean fillAfter);//為true 動畫保持完成狀態。
setDuration(long duration);//動畫期間
以下為自己封裝的動畫工具類,只是最簡單的封裝。
package com.create.utilslibrary;import android.content.Context;import android.view.View;import android.view.animation.AlphaAnimation;import android.view.animation.Animation;import android.view.animation.AnimationSet;import android.view.animation.DecelerateInterpolator;import android.view.animation.RotateAnimation;import android.view.animation.ScaleAnimation;import android.view.animation.TranslateAnimation;/** * Created by Administrator on 2016/5/19 0019. */public class AnimatorUtils { /** * 條目動畫 * * @param context * @param view * @param position */ public static void runEnterAnimation(Context context, View view, int position) { view.setTranslationY(ScreenUtil.getScreenHight(context)); view.animate() .translationY(0) .setStartDelay(100 * (position % 15)) .setInterpolator(new DecelerateInterpolator(3.f)) .setDuration(700) .start(); } /** * 漸層動畫,淡入淡出 動畫完成後保持在結束位置 * * @param fromAlpha 開始的漸層值 0f 表開始時是全透明 1f表開始時是完全顯示 值為 0--1f之間 * @param toAlpha 結束的漸層值 0f 表結束時是全透明 1f表結束時是完全顯示 值為 0--1f之間 * @param duration 動畫期間 毫秒 * @return */ public static AlphaAnimation getAlpaAnimation(float fromAlpha, float toAlpha, long duration) { AlphaAnimation alphaAnimation = new AlphaAnimation(fromAlpha, fromAlpha); alphaAnimation.setDuration(duration); return alphaAnimation; } /** * 平移動畫 動畫完成後保持在結束位置 * 參考對象是view本身:Animation.RELATIVE_TO_SELF * * @param fromXvelue x軸方向 開始的值,相對於view寬度(x軸座標) 的百分比 0f表示從view的最左側的座標點開始動畫,1f表示從最右側開始 值為 0f--1f之間 * @param toXvelue x軸方向 結束的值,相對於view寬度 的百分比 0f表示到view的最左側座標點時結束,1f表示到最右側座標點結束 值為 0f--1f之間 * @param fromYValue y軸方向 開始的值,相對於view高度(y軸座標) 的百分比 0f表示從view的最頂部的座標點開始動畫,1f表示從最底部的座標點開始 值為 0f--1f之間 * @param toYValue y軸方向 結束的值,相對於view高度 的百分比 0f表示到view的最頂部的座標點時結束,1f表示到底部的座標點結束 值為 0f--1f之間 * @param duration 動畫期間 * @return */ public static TranslateAnimation getTranslateAnimation(float fromXvelue, float toXvelue, float fromYValue, float toYValue, long duration ) { TranslateAnimation translateAnimation = new TranslateAnimation( Animation.RELATIVE_TO_SELF, fromXvelue, Animation.RELATIVE_TO_SELF, toXvelue, Animation.RELATIVE_TO_SELF, fromYValue, Animation.RELATIVE_TO_SELF, toYValue ); translateAnimation.setDuration(duration); return translateAnimation; } /** * 縮放動畫 動畫完成後保持在結束位置 * @param fromX 開始的x軸大小 相對於自身寬度的比例 0f--1f之間 * @param toX 結束的x軸的大小 相對於自身寬度的比例 0f--1f之間 * @param fromY 開始的y軸大小 相對於自身高度的比例 0f--1f之間 * @param toY 結束的y軸大小 相對於自身高度的比例 0f--1f之間 * * 以下四個值是確定縮放開始的座標點。 * @param pivotXType Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT. * @param pivotXValue 0f--1f之間的值 * @param pivotYType 同上 * @param pivotYValue 同上 * @return */ public static ScaleAnimation getScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue, long duration ){ ScaleAnimation scaleAnimation = new ScaleAnimation(fromX, toX, fromY, toY, pivotXType, pivotXValue, pivotYType, pivotYValue); scaleAnimation.setDuration(duration); scaleAnimation.setFillAfter(true); return scaleAnimation; } /** * 旋轉動畫 保持在結束位置 * @param fromDegrees 開始角度 * @param toDegrees 結束角度 * 以下四個參數使用者旋轉的圓心 * @param pivotXType 確定x軸座標參照類型 Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT. * @param pivotXValue x軸 的座標值 0f--1f * @param pivotYType 確定y軸的座標參照類型 Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT. * @param pivotYValue y軸 的座標值 0f--1f * @return */ public static RotateAnimation getRotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue, long duration ){ RotateAnimation rotateAnimation=new RotateAnimation(fromDegrees,toDegrees, pivotXType,pivotXValue,pivotYType,pivotYValue); rotateAnimation.setDuration(duration); rotateAnimation.setFillAfter(true); return rotateAnimation; } /** * 建立動畫集合對象 * @param bool true 所有集合中的動畫使用 集合的插值器, false表各個動畫使用自己的插值器。 * @return */ public static AnimationSet getAnimationSet(Boolean bool) { AnimationSet animationSet = new AnimationSet(false); return animationSet; }}
android 動畫總結