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

Posted 先知丨先觉

tags:

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

#Picasso 的使用详解

导入依赖

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";
    

扫码关注公众号“伟大程序猿的诞生“,更多干货新鲜文章等着你~

公众号回复“资料获取”,获取更多干货哦~

有问题添加本人微信号“fenghuokeji996” 或扫描博客导航栏本人二维码

以上是关于Picasso(毕加索)加载圆形图片圆角图片的主要内容,如果未能解决你的问题,请参考以下文章

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

个人资料图片上传器并使用毕加索加载

Android图片加载库:最全面的Picasso讲解

图片加载框架之Glide和Picasso

Glide的加载图片的帮助类,用来把图片圆角或者改成圆形图片

毕加索的艺术——Picasso,一个强大的Android图片下载缓存库,OkHttpUtils的使用,二次封装PicassoUtils实现微信精选