2015-12-16 50 views

回答

1

您可以使用RenderScript在Android中模糊位圖。

RenderScript對於執行圖像處理,計算攝影或計算機視覺的應用程序特別有用。我們可以通過兩種方式訪問​​Android RenderScript框架API:

直接使用android.renderscript API類。這些類可以從Android 3.0(API級別11)或更高級別獲得。 或者,您可以使用android.support.v8.renderscript支持包類。支持庫類適用於運行Android 2.2(API級別8)及更高版本的設備。

爲了使用支持庫的renderScript的API,你必須擁有Android SDK工具版本22.2或更高版本和SDK構建的工具版本18.1.0或更高

下面的代碼片段可用於創建位圖模糊在Android中使用RenderScript API的效果。

//Set the radius of the Blur. Supported range 0 < radius <= 25 
private static final float BLUR_RADIUS = 25f; 

public Bitmap blur(Bitmap image) { 
    if (null == image) return null; 

    Bitmap outputBitmap = Bitmap.createBitmap(image); 
    final RenderScript renderScript = RenderScript.create(this); 
    Allocation tmpIn = Allocation.createFromBitmap(renderScript, image); 
    Allocation tmpOut = Allocation.createFromBitmap(renderScript, outputBitmap); 

    //Intrinsic Gausian blur filter 
    ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript)); 
    theIntrinsic.setRadius(BLUR_RADIUS); 
    theIntrinsic.setInput(tmpIn); 
    theIntrinsic.forEach(tmpOut); 
    tmpOut.copyTo(outputBitmap); 
    return outputBitmap; 
} 

您可以使用上面的代碼片段來模糊ImageView,如下所示。

ImageView imageView = (ImageView) findViewById(R.id.imageView); 
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.nature); 
Bitmap blurredBitmap = blur(bitmap); 
imageView.setImageBitmap(blurredBitmap); 

希望它能幫助你。

+0

感謝響應。我會看看這個。 :) – jagdish

0

渲染腳本不會在每一個API級別工作,所以請使用這個庫,使模糊的作用P21圖像https://android-arsenal.com/details/1/2192

+1

@Rakesh感謝您的迴應。我會看看這個。 :) – jagdish

+1

@Rakesh。我不想模糊整個圖像。圖像的特定部分。你有什麼想法嗎? – jagdish

+1

@jagdish wecome等待我會更新你 –

相關問題