Android 動畫學(xué)習(xí)——模糊Bitmap

/**
     * 模糊圖片的具體方法
     *
     * @param context 上下文對象
     * @param image   需要模糊的圖片
     * @return 模糊處理后的圖片
     */
    static Bitmap blurBitmap(Context context, Bitmap image, float blurRadius) {
        float BITMAP_SCALE = 0.4f;
        // 計算圖片縮小后的長寬
        int width = Math.round(image.getWidth() * BITMAP_SCALE);
        int height = Math.round(image.getHeight() * BITMAP_SCALE);

        // 將縮小后的圖片做為預(yù)渲染的圖片
        Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
        // 創(chuàng)建一張渲染后的輸出圖片
        Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);

        // 創(chuàng)建RenderScript內(nèi)核對象
        RenderScript rs = RenderScript.create(context);
        // 創(chuàng)建一個模糊效果的RenderScript的工具對象
        ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

        // 由于RenderScript并沒有使用VM來分配內(nèi)存,所以需要使用Allocation類來創(chuàng)建和分配內(nèi)存空間
        // 創(chuàng)建Allocation對象的時候其實內(nèi)存是空的,需要使用copyTo()將數(shù)據(jù)填充進去
        Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
        Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
        // 設(shè)置渲染的模糊程度, 25f是最大模糊度
        blurScript.setRadius(blurRadius);
        // 設(shè)置blurScript對象的輸入內(nèi)存
        blurScript.setInput(tmpIn);
        // 將輸出數(shù)據(jù)保存到輸出內(nèi)存中
        blurScript.forEach(tmpOut);
        // 將數(shù)據(jù)填充到Allocation中
        tmpOut.copyTo(outputBitmap);
        return outputBitmap;
    }
最后編輯于
?著作權(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)容