2017-09-01 1 views
0

Comment faire une transformation de cercle sans rogner l'image? J'ai essayé pendant 2 jours et je continue à échouer. C'est le plus proche que je suis venu jusqu'ici, mais comme vous pouvez le voir, l'image n'est pas centrée à l'intérieur du cercle.Transformation de cercle de Picasso sans recadrer l'image

@Override 
public Bitmap transform(Bitmap source) { 
    int size = Math.min(source.getWidth(), source.getHeight()); 
    Bitmap squaredBitmap = Bitmap.createScaledBitmap(source,size-50, size-50,true); 
    if (squaredBitmap != source) { 
     source.recycle(); 
    } 

    Bitmap bitmap = Bitmap.createBitmap(size, size 
      , source.getConfig()); 

    Canvas canvas = new Canvas(bitmap); 

    Paint paint = new Paint(); 
    BitmapShader shader = new BitmapShader(squaredBitmap, 
      BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP); 
    paint.setShader(shader); 
    paint.setAntiAlias(true); 
    float r = size/2f; 
    canvas.drawCircle(r, r, r, paint); 
    squaredBitmap.recycle(); 
    return bitmap; 
} 

Ceci est le résultat:

enter image description here

Répondre

0

Utilisez cette CircleImageView:

<de.hdodenhof.circleimageview.CircleImageView 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/image1" 
    android:layout_width="96dp" 
    android:layout_height="96dp" 
    android:src="@drawable/menu1" 
    app:border_width="2dp" 
    app:border_color="@color/white" 
/> 

Pour plus d'informations: https://github.com/hdodenhof/CircleImageView