亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

自己做一個Android圓形揭示動畫

標簽:
Android

Picture

Picture

圆形揭示动画在很多情况下都可以使用,比如说转场、打开popupwidow和覆盖一层新的view。
官方也提供了一个类专门提供这个动画的封装:

ViewAnimationUtils.createCircularReveal(View view,            int centerX,  int centerY, float startRadius, float endRadius)

然而这个方法必须api 5.0以上才能使用,既然如此不如自己动手造轮子。

思路:
1.只负责显示的范围,所以继承自FrameLayout,具体显示的内容由Child负责。
2.如何控制child显示的区域?在dispatchDraw中调用canvas.clipPath(Path path)把需要显示的区域裁剪出来。
3.如何显示圆?

 mCirclePath.reset();
 mCirclePath.addCircle(float x, float y, float radius, Direction dir)

4.动画用ValueAnimator。

具体流程:

 /**
     * 开始揭示动画
     * @param x 动画开始位置的x值
     * @param y 动画开始位置的y值
     */
    public void revealLayout(int x, int y){
        mCenterX = x;
        mCenterY = y;
        computeFarthest();
        computeVisibleRegion();        if (mAnimator == null) {
            mAnimator = ValueAnimator.ofFloat(0,1);
            mAnimator.setDuration(DEFAULT_ANIMATION_DURATION);
            mAnimator.setInterpolator(new DecelerateInterpolator());
            mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    mAnimationValue = (float) animation.getAnimatedValue();
                    postInvalidate((int) (mVisibleRectF.left*mAnimationValue),(int) (mVisibleRectF.top*mAnimationValue)
                            ,(int) (mVisibleRectF.right*mAnimationValue),(int) (mVisibleRectF.bottom*mAnimationValue));

                }
            });
        }
        mAnimator.start();
    }

开始动画,传入动画开始的坐标值xy。
computeFarthest()计算动画开始点与Layout四个角的最远距离,即圆形为最大时的半径。
computeVisibleRegion()计算圆形显示区域的外界矩形,用于postInvalidate()时指定刷新的区域,减少绘制开销。

  @Override
    protected void dispatchDraw(Canvas canvas) {        if (mAnimationValue == 0){            return;
        }
        canvas.save();
        mCirclePath.reset();
        mCirclePath.addCircle(mCenterX,mCenterY,mFarthest*mAnimationValue, Path.Direction.CW);
        canvas.clipPath(mCirclePath);        super.dispatchDraw(canvas);
        canvas.restore();
    }

动画开始前不绘制Layout里的内容,动画开始后根据mAnimationValue的值改变圆形的大小。mFarthest为之前通过computeFarthest()计算出来的结果。

原文链接:http://www.apkbus.com/blog-822715-77235.html

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消