zoukankan      html  css  js  c++  java
  • 使用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);
  • 相关阅读:
    中小型MIS开发之我见(二)具体实施(上)
    实现.NET应用程序的自动更新
    利用XML实现通用WEB报表打印(参考)
    几款开放源代码的软件测试工具介绍
    TextBox只输入数字
    HttpUtility 类 编码,解码字符串为Html字符串
    测试工具的选择和使用
    javascript技巧参考
    认识Web.config文件
    动态改变页面的CSS样式(收藏备用)
  • 原文地址:https://www.cnblogs.com/fajieyefu/p/5777491.html
Copyright © 2011-2022 走看看