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服务器中网站环境搭建好了.能看到首页,其他页面404解决
Linux开启url重写的方法:1.打开 apache 里httpd.conf(通常是在/etc/httpd/conf目录里)2.找到 #LoadModule rewrite_module modul ...
- 2019 ACM-ICPC 上海网络赛 B. Light bulbs (差分)
题目链接:Light bulbs 比赛链接:The Preliminary Contest for ICPC Asia Shanghai 2019 题意 给定 \(N\) 个灯泡 (编号从 \(0\) ...
- Vue 学习笔记之 —— 组件(踩了个坑)
最近在学习vue,学习组件时,遇到了一个问题,困扰了半个多小时.. <!DOCTYPE html> <html lang="en"> <head> ...
- ASP.NET中ajax验证用户名和邮箱是否重复
这个是前台显示的页面代码↓ <%@ Page Language="C#" AutoEventWireup="true" CodeFile="De ...
- Linux文件介绍
Linux文件介绍 Linux 文件属性 可以通过命令ll+文件名,查看文件的具体属性 例如:ll syz.gz 1736706 -rw-r--r--. 1 root root 28 Oct 27 1 ...
- Ubuntu 更新国内镜像源失败
Ubuntu 更新国内镜像源失败 首先打开系统原来的/etc/apt/sources.list 查看,原来的仓库地址是 https 还是 http 如果是http那么说明本机的 CA 证书有问题,运行 ...
- properties配置文件的基本操作
对properties的基本操作 public class PropertiesUtil {// 是否是文件public static boolean isFile = false;// 路径publ ...
- 2018-2-13-win10-edge扩展
title author date CreateTime categories win10 edge扩展 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17:23 ...
- vue tabNav 点击
<template> <div class="content"> <header class="tab_nav"> < ...
- 安装memcached报错:If it's already installed, specify its path using --with-libevent=/dir/
一.安装memcached,执行./configure --prefix=/usr/local/memcached时候报错: 问题:If it's already installed, specify ...