2016-04-14 12 views
0

Для моего проекта я хочу использовать размытые фоны. Когда я использую следующий метод, он делает мое размытие фона, но для меня это не совсем размыто, я хочу сделать больше размытого фона. Я установил радиус как максимальное значение 25. Кто-то может мне помочь?Больше размыты с RenderScript в Android

private static final float BITMAP_SCALE = 0.9f; 
private static final float BLUR_RADIUS = 25.0f; 

public static Bitmap blur(Context context, Bitmap image) { 
    int width = Math.round(image.getWidth() * BITMAP_SCALE); 
    int height = Math.round(image.getHeight() * BITMAP_SCALE); 

    Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false); 
    Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap); 

    RenderScript rs = RenderScript.create(context); 
    ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); 
    Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap); 
    Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap); 
    theIntrinsic.setRadius(BLUR_RADIUS); 
    theIntrinsic.setInput(tmpIn); 
    theIntrinsic.forEach(tmpOut); 
    tmpOut.copyTo(outputBitmap); 

    return outputBitmap; 


} 

ответ

0

При использовании радиус размытия от 25 до сих пор не достаточно, один дешевый способ сделать размытие, чтобы изменить размер изображения фона вниз и затем изменить его.

private static final float BITMAP_SCALE = 0.9f; 
private static final float RESIZE_SCALE = 1.f/5.f; 
private static RenderScript rs; 

public static Bitmap blur(Context context, Bitmap image) { 
    int width = Math.round(image.getWidth() * BITMAP_SCALE); 
    int height = Math.round(image.getHeight() * BITMAP_SCALE); 

    Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false); 
    Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap); 

    if (rs == null) { 
     // Creating a RS context is expensive, better reuse it. 
     rs = RenderScript.create(context); 
    } 
    Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap); 
    Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap); 

    Type t = Type.createXY(mRS, tmpIn.getElement(), width*RESIZE_SCALE, height*RESIZE_SCALE); 
    Allocation tmpScratch = Allocation.createTyped(rs, t); 

    ScriptIntrinsicResize theIntrinsic = ScriptIntrinsicResize.create(rs); 
    // Resize the original img down. 
    theIntrinsic.setInput(tmpIn); 
    theIntrinsic.forEach_bicubic(tmpScratch); 
    // Resize smaller img up. 
    theIntrinsic.setInput(tmpScratch); 
    theIntrinsic.forEach_bicubic(tmpOut); 
    tmpOut.copyTo(outputBitmap); 

    return outputBitmap; 
} 
+0

Спасибо Miao, уменьшив BITMAP_SCALE, как 0.3f, мое изображение размыто так сильно. – nihasmata

+0

Да, это то же самое, что и изменение размера. Мне любопытно, что если вы сравнили производительность и качество уменьшения BITMAP_SCALE, то и размер RenderScript? –

+0

Нет Miao, я попробовал ваше решение с масштабом масштабирования 1.f/5.f, он не размывал так много. Даже ничего не изменилось :) – nihasmata