關(guān)于安卓毛玻璃實現(xiàn)(一)動態(tài)毛玻璃

動態(tài)毛玻璃實現(xiàn)

感謝作者 RealtimeBlurView

?。。≡创a鏈接在最后?。?!

問題

在不斷變化的背景中,實現(xiàn)毛玻璃

思路

(1)監(jiān)聽布局繪制,動態(tài)捕獲布局的畫像
(2)實時對布局的圖片進行高斯模糊,繪制

實現(xiàn)

(1)監(jiān)聽實現(xiàn),只需要在監(jiān)聽的布局中,設(shè)置好監(jiān)聽回調(diào)即可。
監(jiān)聽為ViewTreeObserver.OnPreDrawListener。設(shè)置好以后,布局繪制信息發(fā)生改變的時候,都會回調(diào)。

這里選擇獲取頁面的decoreview作為監(jiān)聽的布局

    protected View getActivityDecorView() {
        Context ctx = getContext();
        for (int i = 0; i < 4 && !(ctx instanceof Activity) && ctx instanceof ContextWrapper; i++) {
            ctx = ((ContextWrapper) ctx).getBaseContext();
        }
        if (ctx instanceof Activity) {
            return ((Activity) ctx).getWindow().getDecorView();
        } else {
            return null;
        }
    }

decoreview監(jiān)聽如下:

private final ViewTreeObserver.OnPreDrawListener preDrawListener = new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            ··········
            return true;
        }
    };

然后,就是核心的繪制了。這里基于decoreview,通過設(shè)置一層canvas進行獨立的繪制,核心方法如下:

    private final ViewTreeObserver.OnPreDrawListener preDrawListener = new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            if (!canBlur()) {
                return true;
            }
            if (!blurInterval()) {
                return true;
            }
            final int[] locations = new int[2];
            Bitmap oldBmp = mBlurredBitmap;
            View decor = mDecorView;
            if (decor != null && isShown() && prepare() && checkScreenLocation(decor)) {
                boolean redrawBitmap = mBlurredBitmap != oldBmp;
                oldBmp = null;
                decor.getLocationOnScreen(locations);
                int x = -locations[0];
                int y = -locations[1];
                getLocationOnScreen(locations);
                x += locations[0];
                y += locations[1];
                // just erase transparent
                mBitmapToBlur.eraseColor(mOverlayColor & 0xffffff);
                int rc = mBlurringCanvas.save();
                mIsRendering = true;
                RENDERING_COUNT++;
                try {
                    mBlurringCanvas.scale(1.f * mBitmapToBlur.getWidth() / getWidth(), 1.f * mBitmapToBlur.getHeight() / getHeight());
                    mBlurringCanvas.translate(-x, -y);
                    if (decor.getBackground() != null) {
                        decor.getBackground().draw(mBlurringCanvas);
                    }
                    decor.draw(mBlurringCanvas);
                } catch (StopException e) {
                } finally {
                    mIsRendering = false;
                    RENDERING_COUNT--;
                    mBlurringCanvas.restoreToCount(rc);
                }
                if (canBlur()) {
                    blur(mBitmapToBlur, mBlurredBitmap);
                }
                if ((redrawBitmap || mDifferentRoot) && canBlur()) {
                    Log.d(TAG, "onPreDraw identify: " + mIdentify);
                    postInvalidate();
                }
            }

            return true;
        }
    };

可以看出,這里直接通過屏幕高度方法 getLocationOnScreen();進行坐標獲取,在進行坐標計算,bitmap截取,繪制,實現(xiàn)了動態(tài)毛玻璃的效果。但是,這種相關(guān)僅僅使用于非視頻層級,視頻播放層級(surfaceview),目前還是沒有毛玻璃效果的,暫未想到解決方案。

話說回來:

實現(xiàn)毛玻璃的方法,是使用安卓原生的api:RenderScript進行實現(xiàn)。這里沒啥好說的。

注意

在recyclerview中,實現(xiàn)動態(tài)毛玻璃,需要特別適配,目前只適配了出現(xiàn)時的item顯示毛玻璃,其余適配將會放到下一個博客,敬請期待?。?/p>

that's all-------------------------------------------------

(代碼地址--庫libpicblur)[https://gitee.com/motosheep/androidutils-github]

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容