好多天沒(méi)出干貨了,今晚寫(xiě)這篇文章花了幾小時(shí)卫玖,大家喜歡的可以GitHub走一波star
AnimationHelper
這是一個(gè)動(dòng)畫(huà)幫助工具庫(kù), 提供簡(jiǎn)單的幫助實(shí)現(xiàn)以下效果的工具類.我將其上傳了Jcenter, 大家可以引用
工程見(jiàn): https://github.com/Jerey-Jobs/AnimationHelper
大家可以star一波哈!
先看一張效果圖:
import/引入方式
project's build.gradle (工程下的 build.gradle)
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
}
module's build.gradle (模塊的build.gradle)
dependencies {
compile 'com.github.Jerey-Jobs:AnimationHelper:1.0'
}
簡(jiǎn)介
Android5.0系統(tǒng)源碼中增加了新的API - ViewAnimationUtils
是其作用就是可以使控件能夠呈現(xiàn)水波一樣展開(kāi)。
源碼介紹也很簡(jiǎn)單, 我們ctrl+左擊該類可看到
public final class ViewAnimationUtils {
private ViewAnimationUtils() {}
/**
* </code></pre>
*
* @param view The View will be clipped to the animating circle.
* @param centerX The x coordinate of the center of the animating circle, relative to
* <code>view</code>.
* @param centerY The y coordinate of the center of the animating circle, relative to
* <code>view</code>.
* @param startRadius The starting radius of the animating circle.
* @param endRadius The ending radius of the animating circle.
*/
public static Animator createCircularReveal(View view,
int centerX, int centerY, float startRadius, float endRadius) {
return new RevealAnimator(view, centerX, centerY, startRadius, endRadius);
}
}
這個(gè)類就只有一個(gè)方法, createCircularReveal
參數(shù)1 view: 要實(shí)現(xiàn)波紋效果的view;
參數(shù)2 centerX: 動(dòng)畫(huà)的中心點(diǎn)的x坐標(biāo);
參數(shù)3 centerY:動(dòng)畫(huà)的中心點(diǎn)的y坐標(biāo);
參數(shù)4 startRadius: 動(dòng)畫(huà)開(kāi)始的波紋半徑;
參數(shù)5 endRadius:動(dòng)畫(huà)結(jié)束時(shí)的波紋半徑;
封裝
我們開(kāi)始封裝一個(gè)Helper類, 先實(shí)現(xiàn)View的隱藏和顯示動(dòng)畫(huà)
public class AnimationHelper {
public static final int MINI_RADIUS = 0;
public static final int DEFAULT_DURIATION = 500;
/**
* 屏蔽Android提示錯(cuò)誤, 5.0以下不做動(dòng)畫(huà)處理
*
* @param view
* @param startRadius
* @param durationMills
*/
@SuppressLint("NewApi")
public static void show(View view, float startRadius, long durationMills) {
// Android L 以下不做處理,直接顯示
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
view.setVisibility(View.VISIBLE);
return;
}
int xCenter = (view.getLeft() + view.getRight()) / 2;
int yCenter = (view.getTop() + view.getBottom()) / 2;
int w = view.getWidth();
int h = view.getHeight();
//計(jì)算最大半徑, 邊界效應(yīng)+1
int endRadius = (int) (Math.sqrt(w * w + h * h) + 1);
Animator animation = ViewAnimationUtils.createCircularReveal(view,
xCenter, yCenter, startRadius, endRadius);
view.setVisibility(View.VISIBLE);
animation.setDuration(durationMills);
animation.start();
}
@SuppressLint("NewApi")
public static void hide(final View view, float endRadius, long durationMills, final int visible) {
// Android L 以下不做處理,直接顯示
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
view.setVisibility(View.INVISIBLE);
return;
}
int xCenter = (view.getLeft() + view.getRight()) / 2;
int yCenter = (view.getTop() + view.getBottom()) / 2;
int w = view.getWidth();
int h = view.getHeight();
//計(jì)算最大半徑, 邊界效應(yīng)+1
int startRadius = (int) (Math.sqrt(w * w + h * h) + 1);
Animator animation = ViewAnimationUtils.createCircularReveal(view,
xCenter, yCenter, startRadius, endRadius);
animation.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
view.setVisibility(visible);
}
});
animation.setDuration(durationMills);
animation.start();
}
public static void show(View myView) {
show(myView, MINI_RADIUS, DEFAULT_DURIATION);
}
/**
* 默認(rèn)View隱藏狀態(tài)為 INVISIBLE
* @param myView
*/
public static void hide(View myView) {
hide(myView, MINI_RADIUS, DEFAULT_DURIATION, View.INVISIBLE);
}
/*
* @param myView 要隱藏的view
* @param endVisible 動(dòng)畫(huà)執(zhí)行結(jié)束是view的狀態(tài), 是View.INVISIBLE 還是GONE
*/
public static void hide(View myView, int endVisible) {
hide(myView, MINI_RADIUS, DEFAULT_DURIATION, endVisible);
}
}
之后我們使用就很簡(jiǎn)單了, 想要顯示一個(gè)View的展示動(dòng)畫(huà), 則
AnimationHelper.show(mImageView);
隱藏,則
AnimationHelper.hide(mImageView);
我們看一下,目前的使用的效果:
效果
拓展 - 過(guò)渡動(dòng)畫(huà)
我們可以使用該動(dòng)畫(huà),創(chuàng)造一個(gè)頁(yè)面過(guò)渡動(dòng)畫(huà), 如何弄呢?
可以通過(guò)給我們的DecordView
添加一個(gè)View, 然后overridePendingTransition
即可.
以下是代碼:
@SuppressLint("NewApi")
public static void startActivityForResult(
final Activity thisActivity, final Intent intent, final Integer requestCode,
final Bundle bundle, final View view,
int colorOrImageRes, final long durationMills) {
// SDK 低于LOLLIPOP不做處理,直接跳轉(zhuǎn)
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
if (requestCode == null) {
thisActivity.startActivity(intent);
} else if (bundle == null) {
thisActivity.startActivityForResult(intent, requestCode);
} else {
thisActivity.startActivityForResult(intent, requestCode, bundle);
}
return;
}
int[] location = new int[2];
view.getLocationInWindow(location);
final int xCenter = location[0] + view.getWidth() / 2;
final int yCenter = location[1] + view.getHeight() / 2;
final ImageView imageView = new ImageView(thisActivity);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageResource(colorOrImageRes);
final ViewGroup decorView = (ViewGroup) thisActivity.getWindow().getDecorView();
int w = decorView.getWidth();
int h = decorView.getHeight();
decorView.addView(imageView, w, h);
// 計(jì)算中心點(diǎn)至view邊界的最大距離
int maxW = Math.max(xCenter, w - xCenter);
int maxH = Math.max(yCenter, h - yCenter);
final int finalRadius = (int) Math.sqrt(maxW * maxW + maxH * maxH) + 1;
Animator anim = ViewAnimationUtils.createCircularReveal(imageView, xCenter, yCenter, 0, finalRadius);
int maxRadius = (int) Math.sqrt(w * w + h * h) + 1;
long finalDuration = durationMills;
/**
* 計(jì)算時(shí)間
*/
if (finalDuration == DEFAULT_DURIATION) {
// 算出實(shí)際邊距與最大邊距的比率
double rate = 1d * finalRadius / maxRadius;
// 水波擴(kuò)散的距離與擴(kuò)散時(shí)間成正比
finalDuration = (long) (DEFAULT_DURIATION * rate);
}
anim.setDuration(finalDuration);
anim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
if (requestCode == null) {
thisActivity.startActivity(intent);
} else if (bundle == null) {
thisActivity.startActivityForResult(intent, requestCode);
} else {
thisActivity.startActivityForResult(intent, requestCode, bundle);
}
// 默認(rèn)漸隱過(guò)渡動(dòng)畫(huà).
thisActivity.overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
// 默認(rèn)顯示返回至當(dāng)前Activity的動(dòng)畫(huà).
view.postDelayed(new Runnable() {
@Override
public void run() {
Animator anim =
ViewAnimationUtils.createCircularReveal(imageView, xCenter, yCenter, finalRadius, 0);
anim.setDuration(durationMills);
anim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
try {
decorView.removeView(imageView);
} catch (Exception e) {
e.printStackTrace();
}
}
});
anim.start();
}
}, 1000);
}
});
anim.start();
}
使用
AnimationHelper.startActivity(MainActivity.this,
new Intent(MainActivity.this, LoginActivity.class),
mStartAvtivityBtn,
R.color.colorPrimary
);
效果
大家可以star一波哈!
工程地址: https://github.com/Jerey-Jobs/AnimationHelper
哦惠呼,封面圖怕品,
本文作者:Anderson/Jerey_Jobs
博客地址 : http://jerey.cn/
簡(jiǎn)書(shū)地址 : Anderson大碼渣
github地址 : https://github.com/Jerey-Jobs