前面分享了一些開源動畫,又是引用庫,又是設置一些東西,但是在實際項目運用中多多少少還是有些出入的。不過沒關係,人家大神的創作的思想是沒毛病的,咱可以看看效果然後自己去寫一個。可能我個人比較懶,就喜歡工具類,點一個方法就搞定。
老規矩,做記錄,閒話不多說!先來看看效果
這是一個位移動畫,到項目中還是實用的,可以自行UI修改處理。
上代碼
一、彈出動畫(不管它從哪裏彈出,反正就是可以上下左右彈出)
private void showAnimation() {
//獲取View初始化後的高度
height = dp2px(view.getHeight());
//前2個參數是X軸(就是橫向位移)
//第一個參數是進入(彈出)第二個參數是退出
//後2個參數Y軸(就是縱向位移)參數同上
Animation animation = new TranslateAnimation(-height, 0, 0, 0);
animation.setDuration(1000);//動畫從開始到結束執行的時間
animation.setFillAfter(true);//設置爲true,動畫轉化結束後被應用
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
Log.i("動畫1","Start");
}
@Override
public void onAnimationEnd(Animation animation) {
animation.cancel();
isShowAnimation = true;
Log.i("動畫1","End");
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
view.startAnimation(animation);//開始動畫
}
二、退出動畫(從哪裏來,回那裏去)
private void hideAnimation(){
height = dp2px(recyclerView.getHeight());
//同樣的代碼唯一的改動在參數這裏
Animation animation = new TranslateAnimation(0, -height, 0, 0);
animation.setDuration(1000);
animation.setFillAfter(true);//設置爲true,動畫轉化結束後被應用
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
Log.i("動畫2","Start2");
}
@Override
public void onAnimationEnd(Animation animation) {
Log.i("動畫2","End2");
isShowAnimation = false;
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
view.startAnimation(animation);//開始動畫
}
其實上面的2個方法是可以優化的,只用一個方法就可以了,
//在這裏可以加一個Boolean判斷,然後改變一下這個參數即可,記得把對象要提出來作爲全局對象
Animation animation = new TranslateAnimation(0, -height, 0, 0);
以下是簡潔化後的完整Util代碼
public class AnimationViewUtils {
private Context mContext;
private View mView;
private OnAnimationClickListener animationClickListener;
private boolean isShowAnimation;
private int height;
private Animation animation;
public AnimationViewUtils(Context context, View view) {
this.mContext = context;
this.mView = view;
}
public void executeAnimation() {
mView.setVisibility(View.VISIBLE);
height = dp2px(mView.getHeight());//獲取初始化後的控件高度
if (!isShowAnimation) {
animation = new TranslateAnimation(-height, 0, 0, 0);
} else {
animation = new TranslateAnimation(0, -height, 0, 0);
}
animation.setDuration(1000);
animation.setFillAfter(true);//設置爲true,動畫轉化結束後被應用
//動畫監聽
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
if (!isShowAnimation) {
isShowAnimation = true;
} else {
isShowAnimation = false;
animation.setFillAfter(false);//如果不設置false則View隱藏無效
mView.setVisibility(View.GONE);
}
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
mView.startAnimation(animation);//開始動畫
}
//如果有需要自行回調
public interface OnAnimationClickListener {
//參數(父組件,當前單擊的View,單擊的View的位置,數據)
void onShowAnimationEnd();
void onHideAnimationEnd();
}
public void setOnAnimationClickListener(OnAnimationClickListener clickListener) {
this.animationClickListener = clickListener;
}
//dp轉像素
private int dp2px(int value) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, mContext.getResources().getDisplayMetrics());
}
}
當然,上面的的實例是一個左邊彈出動畫,到這裏就結束了,如果想要頂部或者底部,修改
animation = new TranslateAnimation(-height, 0, 0, 0);這裏面的參數即可
示例Demo
END