2016-02-07 1 views
0

J'essaie de faire ce qui est décrit here "Je veux recadrer une région circulaire de cette image bitmap.Tous les pixels en dehors du cercle doivent être transparents."Image de recadrage de cercle avec fond transparent - Android

J'ai déjà essayé ce qui est dans ce poste, mais tous ne proposent pas l'arrière-plan transparent, seulement une image de cercle, j'ai essayé ci-dessous et cela n'a pas fonctionné, des idées?

public Bitmap getCroppedCircleImage(Bitmap bmp, View view) { 
    Bitmap bitmap = getCroppedImage(bmp, view); 

    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); 
    Canvas canvas = new Canvas(output); 

    final int color = 0xff227722; 
    final Paint paint = new Paint(); 
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 

    paint.setAntiAlias(true); 
    canvas.drawARGB(0, 0, 0, 0); 
    paint.setColor(color); 

    //canvas.drawRoundRect(rectF, roundPx, roundPx, paint); 
    canvas.drawCircle(bitmap.getWidth()/2, bitmap.getHeight()/2, bitmap.getWidth()/2, paint); 
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); 
    canvas.drawBitmap(bitmap, rect, rect, paint); 

    Paint paint1 = new Paint(); 
    paint1.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); 
    Rect rectTransparent=new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
    canvas.drawRect(rectTransparent,paint1); 
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC)); 
    canvas.drawBitmap(bitmap, rect, rect, paint); 

    //Bitmap _bmp = Bitmap.createScaledBitmap(output, 60, 60, false); 
    //return _bmp; 
    OutputStream stream = null; 
    try { 
     stream = new FileOutputStream("/sdcard/test.png"); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); 
    try { 
     stream.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return output; 
} 
+0

Utilisez Picasso et une transformation de cercle https://gist.github.com/julianshen/5829333 –

Répondre