2016-01-13 4434 views

回答

1

鏈接這是不可能的畢加索。看到答案here

2
Picasso.with(getApplicationContext()).load(url).placeholder(setCircularImage(R.drawable.profile_sample)).error(setCircularImage(R.drawable.profile_sample)).transform(new CircleTransform()).into(ivMenuProfile); 

附加setCircularImage方法有用於補充placehoder佔位符到圓視圖

private RoundedBitmapDrawable setCircularImage(int id) { 
     Resources res = getApplicationContext().getResources(); 
     Bitmap src = BitmapFactory.decodeResource(res, id); 
     RoundedBitmapDrawable roundedBitmapDrawable = 
     RoundedBitmapDrawableFactory.create(res, src); 
     roundedBitmapDrawable.setCornerRadius(Math.max(src.getWidth(), src.getHeight())/2.0f); 
     return roundedBitmapDrawable; 
    } 

添加CircleTransform()與變換的變化形狀,以圓爲負載URL圖像。

public class CircleTransform implements Transformation { 
    @Override 
    public Bitmap transform(Bitmap source) { 
     int size = Math.min(source.getWidth(), source.getHeight()); 

     int x = (source.getWidth() - size)/2; 
     int y = (source.getHeight() - size)/2; 

     Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size); 
     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; 
    } 

    @Override 
    public String key() { 
     return "circle"; 
    } 
}