Android笔记之RoundedImageView
参考项目: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的更多相关文章
- Android笔记——Android中数据的存储方式(二)
我们在实际开发中,有的时候需要储存或者备份比较复杂的数据.这些数据的特点是,内容多.结构大,比如短信备份等.我们知道SharedPreferences和Files(文本文件)储存这种数据会非常的没有效 ...
- Android笔记:触摸事件的分析与总结----TouchEvent处理机制
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://glblong.blog.51cto.com/3058613/1559320 ...
- Android 笔记之 R 文件
Android笔记之R文件 h2{ color: #4abcde; } a{ color: blue; text-decoration: none; } a:hover{ color: red; te ...
- Android 笔记之 Android 系统架构
Android笔记之Android系统架构 h2{ color: #4abcde; } a{ color: blue; text-decoration: none; } a:hover{ color: ...
- Android笔记之使用Glide加载网络图片、下载图片
Glide简介 不想说太多,真的很方便:P)可以节省我不少时间 GitHub地址:https://github.com/bumptech/glide 加载网络图片到ImageView Glide.wi ...
- Android笔记--View绘制流程源码分析(二)
Android笔记--View绘制流程源码分析二 通过上一篇View绘制流程源码分析一可以知晓整个绘制流程之前,在activity启动过程中: Window的建立(activit.attach生成), ...
- Android笔记--View绘制流程源码分析(一)
Android笔记--View绘制流程源码分析 View绘制之前框架流程分析 View绘制的分析始终是离不开Activity及其内部的Window的.在Activity的源码启动流程中,一并包含 着A ...
- Android笔记--自定义控件仿遥控器的圆形上下左右OK圆盘按钮
原文:Android笔记--自定义控件仿遥控器的圆形上下左右OK圆盘按钮 上面就是几张预览图!代码在最底下 主要就两个步骤,画图.监听点击 1.整个控件基本上是一步步画出来的,重写onDraw方法开始 ...
- 我的Android笔记--我对安卓系统的一些了解
敲了这么长时间代码,记录一下我对Android的一些概念,下面大部分内容来源自网络资料和官方给的文档. 1,Android操作系统的核心属于Linux的一个分支,具有典型的Linux调度和功能 ...
随机推荐
- Linux-vim编辑器 常用命令 复制粘贴
Linux-vim编辑器 一.vim三种工作模式 1.1.命令模式 在此模式下,可以使用上.下.左.右键或者 k.j.h.l 命令进行光标移动,还可以对文件内容进行复制.粘贴.替换.删除等操作. 1. ...
- Java-Class-@I:javax.annotation.PostConstruct
ylbtech-Java-Class-@I:javax.annotation.PostConstruct 1.返回顶部 2.返回顶部 1.1. package com.ylbtech.api.pl ...
- CSS3 Media Queries模板:max-width和min-width
CSS3 Media Queries模板 CSS3 Media Queries一般都是使用“max-width”和“min-width”两个属性来检查各种设备的分辨大小与样式表所设条件是否满足,如果满 ...
- scala 集合类型
Iterable 是序列(Seq), 集(Set) 映射(Map)的特质 序列式有序的集合如数组和列表 集合可以通过== 方法确定对每个对象最多包含一个 映射包含了键值映射关系的集合 列表缓存: 使用 ...
- awesome mac
https://proxyman.app/ https://proxie.app/docs/#how-does-it-work https://github.com/kyleduo/TinyPNG4M ...
- Webpack4篇
[Webpack4篇] webpack4 打包优化策略 当前依赖包的版本 1 优化loader配置 1.1 缩小文件匹配范围(include/exclude) 通过排除node_modules下的文件 ...
- dubbo源码学习(四):暴露服务的过程
dubbo采用的nio异步的通信,通信协议默认为 netty,当然也可以选择 mina,grizzy.在服务端(provider)在启动时主要是开启netty监听,在zookeeper上注册服务节点, ...
- 转载:vs2010 问题 >LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
原文链接:http://www.cnblogs.com/newpanderking/articles/3372969.html >LINK : fatal error LNK1123: 转换到 ...
- input 实现一次性上传文件
在实际项目中可能会用到,上传多个文件请求一次接口,因此,主要代码 $('#tabList').on('click','.resetWorkStatus',function(){ var that = ...
- redis主从和集群搭建
主从搭建 redis的主从搭建非常简单,打开配置文件6379.conf,只需要将主节点的protected-mode设置为no,然后在从节点配置中加入:slaveof <masterip> ...