参考项目:GcsSloop/rclayout

实现1,利用Canvas.clipPath来实现,适用于任何View(无法去除锯齿效果)

package com.bu_ish.blog;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.util.AttributeSet; import androidx.appcompat.widget.AppCompatImageView; public class RoundedImageView extends AppCompatImageView {
private int cornerRadius = 12;
private Path path; public RoundedImageView(Context context, AttributeSet attrs) {
super(context, attrs);
path = new Path();
} @Override
protected void onDraw(Canvas canvas) {
ViewUtils.clipRoundedPathForCanvas(this, canvas, path, cornerRadius);
super.onDraw(canvas);
} public void setCornerRadius(int radius) {
cornerRadius = radius;
invalidate();
}
}
package com.bu_ish.blog;

import android.graphics.Canvas;
import android.graphics.Path;
import android.view.View; public class ViewUtils {
public static void clipRoundedPathForCanvas(View view, Canvas canvas, Path path, int cornerRadius) {
makePathRounded(view, path, cornerRadius);
canvas.clipPath(path);
} private static void makePathRounded(View view, Path path, int cornerRadius) {
int width = view.getWidth(), height = view.getHeight();
path.moveTo(cornerRadius, 0);
path.lineTo(width - cornerRadius, 0);
path.quadTo(width, 0, width, cornerRadius);
path.lineTo(width, height - cornerRadius);
path.quadTo(width, height, width - cornerRadius, height);
path.lineTo(cornerRadius, height);
path.quadTo(0, height, 0, height - cornerRadius);
path.lineTo(0, cornerRadius);
path.quadTo(0, 0, cornerRadius, 0);
}
}

实现2,利用Canvas.drawPath实现,可抗锯齿,适用于任何View(但是在AS中无法预览圆角效果)

package com.bu_ish.blog;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet; import androidx.appcompat.widget.AppCompatImageView; public class RoundedImageView extends AppCompatImageView {
private final Path roundedPath, pathToDraw;
private final RectF rect;
private final Paint paint;
private int cornerRadius = 50; public RoundedImageView(Context context, AttributeSet attrs) {
super(context, attrs);
roundedPath = new Path();
pathToDraw = new Path();
rect = new RectF();
paint = new Paint();
initializePaint();
} @Override
protected void onDraw(Canvas canvas) {
canvas.saveLayer(null, null, Canvas.ALL_SAVE_FLAG);
super.onDraw(canvas);
addRoundRectToRoundedPath();
preparePathToDraw();
canvas.drawPath(pathToDraw, paint);
canvas.restore();
} private void initializePaint() {
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
} private void addRoundRectToRoundedPath() {
rect.left = 0;
rect.top = 0;
rect.right = getWidth();
rect.bottom = getHeight();
     roundedPath.reset();
roundedPath.addRoundRect(rect, cornerRadius, cornerRadius, Path.Direction.CW);
} private void preparePathToDraw() {
pathToDraw.reset();
pathToDraw.addRect(0, 0, getWidth(), getHeight(), Path.Direction.CW);
pathToDraw.op(roundedPath, Path.Op.DIFFERENCE);
} public void setCornerRadius(int radius) {
cornerRadius = radius;
invalidate();
}
}

实现3,使用BitmapShader实现,仅适用于ImageView,参考Demo:https://pan.baidu.com/s/1WFyZkgmwckNSVMqdLLxymw,提取码:nvb8

一个比较好的开源项目

vinc3m1/RoundedImageView: A fast ImageView that supports rounded corners, ovals, and circles.

Android笔记之RoundedImageView的更多相关文章

  1. Android笔记——Android中数据的存储方式(二)

    我们在实际开发中,有的时候需要储存或者备份比较复杂的数据.这些数据的特点是,内容多.结构大,比如短信备份等.我们知道SharedPreferences和Files(文本文件)储存这种数据会非常的没有效 ...

  2. Android笔记:触摸事件的分析与总结----TouchEvent处理机制

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://glblong.blog.51cto.com/3058613/1559320   ...

  3. Android 笔记之 R 文件

    Android笔记之R文件 h2{ color: #4abcde; } a{ color: blue; text-decoration: none; } a:hover{ color: red; te ...

  4. Android 笔记之 Android 系统架构

    Android笔记之Android系统架构 h2{ color: #4abcde; } a{ color: blue; text-decoration: none; } a:hover{ color: ...

  5. Android笔记之使用Glide加载网络图片、下载图片

    Glide简介 不想说太多,真的很方便:P)可以节省我不少时间 GitHub地址:https://github.com/bumptech/glide 加载网络图片到ImageView Glide.wi ...

  6. Android笔记--View绘制流程源码分析(二)

    Android笔记--View绘制流程源码分析二 通过上一篇View绘制流程源码分析一可以知晓整个绘制流程之前,在activity启动过程中: Window的建立(activit.attach生成), ...

  7. Android笔记--View绘制流程源码分析(一)

    Android笔记--View绘制流程源码分析 View绘制之前框架流程分析 View绘制的分析始终是离不开Activity及其内部的Window的.在Activity的源码启动流程中,一并包含 着A ...

  8. Android笔记--自定义控件仿遥控器的圆形上下左右OK圆盘按钮

    原文:Android笔记--自定义控件仿遥控器的圆形上下左右OK圆盘按钮 上面就是几张预览图!代码在最底下 主要就两个步骤,画图.监听点击 1.整个控件基本上是一步步画出来的,重写onDraw方法开始 ...

  9. 我的Android笔记--我对安卓系统的一些了解

    敲了这么长时间代码,记录一下我对Android的一些概念,下面大部分内容来源自网络资料和官方给的文档.     1,Android操作系统的核心属于Linux的一个分支,具有典型的Linux调度和功能 ...

随机推荐

  1. layui多图上传

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. SQL生成 C# Model

    本文转自: https://www.cnblogs.com/jhli/p/11552105.html declare @TableName sysname = 'T_FakeOrderList' de ...

  3. CSS:CSS 伪元素

    ylbtech-CSS:CSS 伪元素 1.返回顶部 1. CSS 伪元素 CSS伪元素是用来添加一些选择器的特殊效果. 语法 伪元素的语法: selector:pseudo-element {pro ...

  4. 1251 client does not support

    1.mysql -uroot -p 123456        (用户root,密码123465) 2.use mysql: 3.ALTER USER 'root'@'localhost' IDENT ...

  5. LeetCode 最小栈

    题目链接:https://leetcode-cn.com/problems/min-stack/ 题目大意 略.并且题目中要求的操作都要 O(1) 实现. 分析 用 2 个栈,一个普通栈,一个单调栈. ...

  6. Excel函数——ANSI字符集与Code、Char、Asc函数

    小叙背景 Windows系统下,默认的字符集为ANSI,该字符编码方式在不同语言环境下采用不同的编码方案,在中文系统下ANSI编码是GBK.ANSI由ASCII扩展而来,ANSI下无论何种具体的编码方 ...

  7. 22-4-isarry

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. Mybatis_环境搭建

    1.配置pom文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...

  9. python 对象的删除

  10. Java—Map浅入

    写支付签名的时候遇到了Map一家,就简单的比较了一下,于是乎先打印看看结果 Map<String,String> hashMap1 = new HashMap<>();hash ...