2017-09-20 14 views
0

J'ai essayé RenderScript pour flouter l'image et cela fonctionne. Je voudrais savoir comment RenderScript peut être utilisé pour brouiller une partie de l'image. J'ai essayé ci-dessous le code, mais cela n'a pas fonctionné:Flou de l'image

Bitmap overlay = Bitmap.createBitmap(
      mWidth, 
      mHeight, 
      Bitmap.Config.ARGB_8888); 

    Canvas canvas = new Canvas(overlay); 

    canvas.drawBitmap(bitmap, -mletf, 
      -mTop, null); 

    RenderScript rs = RenderScript.create(mContext); 

    Allocation overlayAlloc = Allocation.createFromBitmap(
      rs, overlay); 

    ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(
      rs, overlayAlloc.getElement()); 

    blur.setInput(overlayAlloc); 

    blur.setRadius(mRadius); 

    blur.forEach(overlayAlloc); 

    overlayAlloc.copyTo(overlay); 




    rs.destroy(); 
    return overlay; 

Variables mHeight, mWidth sont la hauteur et la largeur de la pièce à être floue et son mTop, mletf sont là où le flou devrait commencer.

+0

double possible de [Blur au toucher. Application Android] (https://stackoverflow.com/questions/18188079/blur-on-touch-android-application) – Tharkius

Répondre

-1

Utilisez cette bibliothèque enter link description here

Et

defaultConfig { 
applicationId "com.javatechig" 
minSdkVersion 14 
targetSdkVersion 23 
versionCode 1 
versionName "1.0" 

// Add the following two lines 
renderscriptTargetApi 18 
renderscriptSupportModeEnabled true 

}

Les extraits de code suivant peut être utilisé créer un effet de flou bitmap dans Android en utilisant l'API renderScript.

//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; 
} 

Vous pouvez utiliser l'extrait de code ci-dessus pour flouter un ImageView comme suit.

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

Suivez ce lien aussi enter link description here

0

Utilisez l'API LaunchOptions:

LaunchOptions lo = new LaunchOptions(); 
lo.setX(mLeft, mLeft+mWidth); 
lo.setY(mTop, mTop+mHeight); 

blur.forEach(overlayAlloc, lo);