码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-01-28 13:45:06      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:error   圆形图片   this   col   文章   lib   awr   load   tran   

作者很好的文章访问量缺很少也很难搜到(我这里插个眼以后用的到)
作者:先知丨先觉
来源: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实现图片圆角和图片圆形

标签:error   圆形图片   this   col   文章   lib   awr   load   tran   

原文地址:https://www.cnblogs.com/xuexidememeda/p/10329643.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!