使用Picasso实现图片圆角和图片圆形

Posted xuexidememeda

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Picasso实现图片圆角和图片圆形相关的知识,希望对你有一定的参考价值。

作者很好的文章访问量缺很少也很难搜到(我这里插个眼以后用的到)
作者:先知丨先觉
来源:CSDN
原文:https://blog.csdn.net/github_33304260/article/details/52033539

导入依赖

compile ‘com.squareup.picasso:picasso:2.5.2‘

  代码使用

Picasso.with(Activity.this)
       .load(URL)
       .transform(new CircleCornerForm())
       .error(R.drawable.head)
       .into(ImageView);

  

加载圆形图片

import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;

import com.squareup.picasso.Transformation;

/**
 * Created libin on 2016/4/25.
 * 圆形图片
 */
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";
    }
}

  

加载圆角图片

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;

import com.squareup.picasso.Transformation;

/**
 * Created by libin on 16/7/22.
 * 圆角切片
 */
public class CircleCornerForm implements Transformation {

    @Override
    public Bitmap transform(Bitmap source) {
        int widthLight = source.getWidth();
        int heightLight = source.getHeight();

        Bitmap output = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(output);
        Paint paintColor = new Paint();
        paintColor.setFlags(Paint.ANTI_ALIAS_FLAG);

        RectF rectF = new RectF(new Rect(0, 0, widthLight, heightLight));

        canvas.drawRoundRect(rectF, widthLight / 5, heightLight / 5, paintColor);//这里除的数越大角度越小

        Paint paintImage = new Paint();
        paintImage.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
        canvas.drawBitmap(source, 0, 0, paintImage);
        source.recycle();
        return output;
    }

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

  

 






以上是关于使用Picasso实现图片圆角和图片圆形的主要内容,如果未能解决你的问题,请参考以下文章

Picasso(毕加索)加载圆形图片圆角图片

Android圆角图片和圆形图片实现总结

Android自定义ImageView实现图片圆形 ,椭圆和矩形圆角显示

Android圆角图片和圆形图片实现总结

Android 完美实现图片圆角和圆形(对实现进行分析)

Android 圆角、圆形 ImageView 实现