美文网首页
圆角处理图片

圆角处理图片

作者: 小慕汐 | 来源:发表于2018-08-28 16:04 被阅读9次
public class DrawableUtils {
    
    /**
    * 将图片的四角圆弧化
    *
    * @param bitmap      原图
    * @param roundPixels 弧度
    * @return
    */
    public static Bitmap getRoundCornerImage(Bitmap bitmap, int roundPixels, int roundType) {
       int    width           = bitmap.getWidth();
       int    height          = bitmap.getHeight();
       Path   path            = checkRect(width, height, roundPixels, roundType);
       Bitmap roundRoundImage = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
       Canvas canvas          = new Canvas(roundRoundImage);
       Paint  paint           = new Paint();
       paint.setAntiAlias(true);// 抗锯齿
       paint.setFilterBitmap(true);// 抗锯齿
       
       paint.setColor(0xFF000000);
       canvas.drawPath(path, paint);
       paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));//设置相交模式
       
       canvas.drawBitmap(bitmap, 0, 0, paint);
       return roundRoundImage;
    }
    
    public static Path checkRect(int width, int height, int roundPixels, int roundType) {
       Path  path = new Path();
       RectF rect = new RectF(0, 0, width, height);
       path.addRoundRect(rect, roundPixels, roundPixels, Path.Direction.CCW);
       if ((roundType & ROUND_LEFT_TOP) == 0) {
          path.addRect(0, 0, roundPixels, roundPixels, Path.Direction.CCW);
       }
       if ((roundType & ROUND_RIGHT_TOP) == 0) {
          path.addRect(width - roundPixels, 0, width, roundPixels, Path.Direction.CCW);
       }
       if ((roundType & ROUND_LEFT_BOTTOM) == 0) {
          path.addRect(0, height - roundPixels, roundPixels, height, Path.Direction.CCW);
       }
       if ((roundType & ROUND_RIGHT_BOTTOM) == 0) {
          path.addRect(width - roundPixels, height - roundPixels, width, height, Path.Direction.CCW);
       }
       return path;
    }
    
    public static final int ROUND_LEFT_TOP     = 8;
    public static final int ROUND_RIGHT_TOP    = 4;
    public static final int ROUND_LEFT_BOTTOM  = 2;
    public static final int ROUND_RIGHT_BOTTOM = 1;
}

用int的低位来表示各种状态:

  1. 1000 -> 8 - > 左上圆
  2. 0100 -> 6 - > 右上圆
  3. 0010 -> 4 - > 左下圆
  4. 0001 -> 1 - > 右下圆

1.创建一个path,然后add一个圆角,
2.通过是否有圆角来决定要不要覆盖圆角的位置
3.通过canvas.drawPath圆路径

原始图
右下圆角
左下圆角
下圆角
右上圆角
右圆角
左下右上圆角
左下右圆角
左上圆角
左上右下圆角
左圆角
右下左圆角
上圆角
左上右圆角
右上左圆角
全圆角

相关文章

  • [iOS] 图像处理:一种高效裁剪图片圆角的算法

    [iOS] 图像处理:一种高效裁剪图片圆角的算法 [iOS] 图像处理:一种高效裁剪图片圆角的算法

  • 圆角处理图片

    用int的低位来表示各种状态: 1000 -> 8 - > 左上圆 0100 -> 6 - > 右上圆 0010 ...

  • 图片圆角处理

    在我们开发中经常会对一些图片进行圆角处理,这样会让应用看起来更加的美观,设置圆角有多种方法,但并不是每种都是性能很...

  • 图片圆角处理

    第一种方法 是对图层进行操作/** 要操作的图片 */ @property (nonatomic,retain) ...

  • 图片圆角处理

    最近做一个项目,在某一些控件上面要求选中的时候是按钮颜色是绿色,未选中的时候按钮背景图是白色且有灰色的边框,由于选...

  • 图片圆角处理

    众所周知,普通的图片圆角处理方式会产生一定的性能问题,如: 其实在处理一般的圆角问题时可以用这种方法,但是假设你的...

  • UIImage& UIImageView+SDWebIm

    经常需要我们处理图片,水印、圆角等等,处理圆角我们经常使用layer.cornerRadius,这样是非常耗性能点...

  • PHP 裁剪图片成圆形、不规则形状

    第一种方法: 第二种方法:使用PHP Imagick库进行图片圆角处理,Imagick是自带的图片圆角处理,在执行...

  • Image

    直接圆角图片 设置圆角图片度数 设置圆角图片带灰色圆角边框 设置圆角图片带灰色圆角边框带阴影

  • iOS设置圆角过量 渲染 卡顿问题

    UILabel处理 图片处理 参考文献iOS设置圆角的四种方法

网友评论

      本文标题:圆角处理图片

      本文链接:https://www.haomeiwen.com/subject/ixkqwftx.html