2015-09-04 41 views
0

我有一個函數,它將一個位圖作爲參數,並返回一個位圖。Picasso自定義位圖變換方法

public Bitmap setRoundedCornes(Bitmap b, int l, int r, int t, int b) 

使用畢加索之前,我用這個方法在應用程序中使用我的最後位之前。

現在我正在使用畢加索,我不確定如何應用此方法。

有沒有人有任何想法?

編輯

我現在有:

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int topLeftX, int topLeftY, int topRightX, int topRightY, int bottomRightX, int bottomRightY, int bottomLeftX, int bottomLeftY) { 
    try { 
     final Paint paint = new Paint(); 
     final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
     Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); 
     Canvas canvas = new Canvas(output); 
     // the float array passed to this function defines the x/y values of the corners 
     // it starts top-left and works clockwise 
     // so top-left-x, top-left-y, top-right-x etc 
     RoundRectShape rrs = new RoundRectShape(new float[]{topLeftX, topLeftY, topRightX, topRightY, bottomRightX, bottomRightY, bottomLeftX, bottomLeftY}, null, null); 
     canvas.drawARGB(0, 0, 0, 0); 
     paint.setAntiAlias(true); 
     paint.setColor(0xFF000000); 
     rrs.resize(bitmap.getWidth(), bitmap.getHeight()); 
     rrs.draw(canvas, paint); 
     paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.SRC_IN)); 
     canvas.drawBitmap(bitmap, rect, rect, paint); 
     return output; 
    }catch(Exception e){ 

     return bitmap; 
    } 
} 

public class MyTransform implements Transformation { 
    @Override 
    public Bitmap transform(Bitmap source) { 
     //your logic to transform goes here 
     return getRoundedCornerBitmap(source, 20, 20, 20, 20, 0, 0, 0, 0); 
    } 

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

回答

1

用於轉化創建一個類,它實現轉化

爲例如

public class MyTransform 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"; 
    } 
} 

現在應用這種轉變可以畢加索使用這樣

Picasso.with(getContext()) 
    .load(url) 
    .transform(new MyTransform()) 
    .into(imageView, new com.squareup.picasso.Callback() { 
         @Override 
         public void onSuccess() { 
           // do something if its loaded successfully 
         } 

         @Override 
         public void onError() { 
          // do something if its not loaded successfully 
         } 
        }); 
+0

'key()'在哪裏使用? –

+0

來自文檔\t'key() 返回轉換的唯一鍵,用於緩存目的。' –

+0

https://square.github.io/picasso/javadoc/com/squareup/picasso/Transformation.html –

0

是的,你可以申請轉換像這樣的:

public class CropSquareTransformation 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 result = Bitmap.createBitmap(source, x, y, size, size); 
    if (result != source) { 
     source.recycle(); 
    } 
    return result; 
    } 

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

見畢加索文檔

+0

是'鍵()'用在哪裏? –

+0

我收到以下錯誤 - '轉換循環突變的輸入位圖,但未能回收原始文件。' –