1. 程式人生 > >ViewAnimationUtils實現過渡動畫

ViewAnimationUtils實現過渡動畫

隨著material design設計規範的普及,material design中的動畫將為使用者提供操作反饋並在使用者與您的應用進行互動時提供視覺連續性。 material design將為按鈕與操作行為轉換提供一些預設動畫,而 Android 5.0(API 級別 21)及更高版本可讓您定製這些動畫,同時也可建立新動畫。今天我們就來了解一下迴圈揭露這一效果,以及它的一些擴充套件的使用。

先認識

我們先看下效果:

這裡寫圖片描述

以上效果就是通過ViewAnimationUtils實現的,利用簡單的幾行程式碼,實現酷炫的揭露動畫。

Api

這裡寫圖片描述

目前ViewAnimationUtils類中只有一個方法,那就是createCircularReveal。很明顯,我們使用ViewAnimationUtils.createCircularReveal()方法就能達到基本的揭露動畫效果了。那麼我們就直接開始看一下這個方法到底需要哪些引數吧:

  • view:代表的是你要操作的view
  • centerX:圓的x方向的中點
  • centerY:圓的y方向的中點
  • startRadius:這個圓開始時候的半徑
  • endRadius:結束時候的半徑

斜線展示

Animator animator1 = ViewAnimationUtils.createCircularReveal(cv_img, 0, 0, 0, (float) Math.hypot(cv_img.getWidth(), cv_img.getHeight()));
animator1.setInterpolator(new LinearInterpolator());//插補器有沒有不影響
animator1.setDuration(2000); animator1.start();

由內向外揭露

int cenX = cv_img.getWidth() / 2;
int cenY = cv_img.getHeight() / 2;
Animator an = ViewAnimationUtils.createCircularReveal(cv_img, cenX, cenY, 0, cenX);
an.setDuration(3000);
an.start();
an.addListener(new AnimatorListenerAdapter() {
    @Override
    public
void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); cv_img.setVisibility(View.VISIBLE); } });

由外向內揭露

int centerX = cv_img.getWidth() / 2;//獲取元件的寬的一半
int centerY = cv_img.getHeight() / 2;//獲取元件的高的一半
Animator animator = ViewAnimationUtils.createCircularReveal(cv_img, centerX, centerY,cv_img.getWidth(), 0);
animator.setDuration(3000);
animator.setInterpolator(new LinearOutSlowInInterpolator());//out到in
animator.start();
animator.addListener(new AnimatorListenerAdapter() {
    @Override
    public void onAnimationEnd(Animator animation) {
        super.onAnimationEnd(animation);
        cv_img.setVisibility(View.GONE);
    }
});

至於是何種形態變化,具體還是有createCircularReveal的後四個引數決定,這幾個引數都解釋過了,也很好理解,這裡就不再累贅了。

更多精彩內容,歡迎關注我的微信公眾號——Android機動車
這裡寫圖片描述