使用Picasso將加載的圖片變成圓形


http://blog.it985.com/14794.html,感謝該作者

Picasso的GITHUB地址:https://github.com/square/picasso。

怎么實現各種各樣的圖片樣式呢?

其原理是在Picasso里提供了Transformation這個接口,實現該接口,編寫繪制圖形的代碼。

實現該接口的兩個抽象方法transform(),key()

代碼從原博客拷貝過來的。

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" ;
}
}
這樣使用它:
Picasso.with(activity).load(mayorShipImageLink).transform(new CircleTransform()).into(ImageView);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM