Android自定义ImageView实现圆角功能
时间:2022-07-28
本文章向大家介绍Android自定义ImageView实现圆角功能,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
使用自定义ImageView,实现圆角功能,供大家参考,具体内容如下
1.自定义属性attrs.xml
<?xml version="1.0" encoding="utf-8"?
<resources
<declare-styleable name="RoundCornerImageView"
<attr name="radius" format="dimension" /
<attr name="left_top_radius" format="dimension" /
<attr name="right_top_radius" format="dimension" /
<attr name="right_bottom_radius" format="dimension" /
<attr name="left_bottom_radius" format="dimension" /
</declare-styleable
</resources
2.自定义RoundCornerImageView,继承AppCompatImageView
public class RoundCornerImageView extends AppCompatImageView {
private float width, height;
private int defaultRadius = 0;
private int radius;
private int leftTopRadius;
private int rightTopRadius;
private int rightBottomRadius;
private int leftBottomRadius;
public RoundCornerImageView(Context context) {
this(context, null);
init(context, null);
}
public RoundCornerImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
init(context, attrs);
}
public RoundCornerImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
if (Build.VERSION.SDK_INT < 18) {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
// 读取配置
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.RoundCornerImageView);
radius = array.getDimensionPixelOffset(R.styleable.RoundCornerImageView_radius, defaultRadius);
leftTopRadius = array.getDimensionPixelOffset(R.styleable.RoundCornerImageView_left_top_radius, defaultRadius);
rightTopRadius = array.getDimensionPixelOffset(R.styleable.RoundCornerImageView_right_top_radius, defaultRadius);
rightBottomRadius = array.getDimensionPixelOffset(R.styleable.RoundCornerImageView_right_bottom_radius, defaultRadius);
leftBottomRadius = array.getDimensionPixelOffset(R.styleable.RoundCornerImageView_left_bottom_radius, defaultRadius);
//如果四个角的值没有设置,那么就使用通用的radius的值。
if (defaultRadius == leftTopRadius) {
leftTopRadius = radius;
}
if (defaultRadius == rightTopRadius) {
rightTopRadius = radius;
}
if (defaultRadius == rightBottomRadius) {
rightBottomRadius = radius;
}
if (defaultRadius == leftBottomRadius) {
leftBottomRadius = radius;
}
array.recycle();
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
width = getWidth();
height = getHeight();
}
@Override
protected void onDraw(Canvas canvas) {
//这里做下判断,只有图片的宽高大于设置的圆角距离的时候才进行裁剪
int maxLeft = Math.max(leftTopRadius, leftBottomRadius);
int maxRight = Math.max(rightTopRadius, rightBottomRadius);
int minWidth = maxLeft + maxRight;
int maxTop = Math.max(leftTopRadius, rightTopRadius);
int maxBottom = Math.max(leftBottomRadius, rightBottomRadius);
int minHeight = maxTop + maxBottom;
if (width = minWidth && height minHeight) {
Path path = new Path();
//四个角:右上,右下,左下,左上
path.moveTo(leftTopRadius, 0);
path.lineTo(width - rightTopRadius, 0);
path.quadTo(width, 0, width, rightTopRadius);
path.lineTo(width, height - rightBottomRadius);
path.quadTo(width, height, width - rightBottomRadius, height);
path.lineTo(leftBottomRadius, height);
path.quadTo(0, height, 0, height - leftBottomRadius);
path.lineTo(0, leftTopRadius);
path.quadTo(0, 0, leftTopRadius, 0);
canvas.clipPath(path);
}
super.onDraw(canvas);
}
}
3.布局文件中使用
<?xml version="1.0" encoding="utf-8"?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="voicedemo.iflytek.com.roundimage.MainActivity"
<voicedemo.iflytek.com.roundimage.RoundCornerImageView
android:id="@+id/iv_avatar"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="50dp"
android:scaleType="centerCrop"
app:left_top_radius="20dp"
app:right_top_radius="20dp"
/
</LinearLayout
4.调用
public class MainActivity extends AppCompatActivity {
String avatarUrl = "19e9d4c0a8f1cd033ecac3692_th.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView ivAvatar = findViewById(R.id.iv_avatar);
Glide.with(this).load(avatarUrl).into(ivAvatar);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助。
- java教程
- Java快速入门
- Java 开发环境配置
- Java基本语法
- Java 对象和类
- Java 基本数据类型
- Java 变量类型
- Java 修饰符
- Java 运算符
- Java 循环结构
- Java 分支结构
- Java Number类
- Java Character类
- Java String类
- Java StringBuffer和StringBuilder类
- Java 数组
- Java 日期时间
- Java 正则表达式
- Java 方法
- Java 流(Stream)、文件(File)和IO
- Java 异常处理
- Java 继承
- Java 重写(Override)与重载(Overload)
- Java 多态
- Java 抽象类
- Java 封装
- Java 接口
- Java 包(package)
- Java 数据结构
- Java 集合框架
- Java 泛型
- Java 序列化
- Java 网络编程
- Java 发送邮件
- Java 多线程编程
- Java Applet基础
- Java 文档注释
- WPF开发之以管理员身份运行
- 快速学习-Sentinel: 分布式系统的流量防卫兵
- Godot游戏开发实践之一:使用High Level Multiplayer API制作多人游戏(上)
- Godot游戏开发实践之一:使用High Level Multiplayer API制作多人游戏(下)
- ansible生产环境使用场景(二)
- Go 语言实现 RPC 调用
- django FileResponse 解决中文命名文件下载后乱码问题
- systemctl控制服务配置
- C++中的万能引用和完美转发
- 当把Markdown编辑器搬进博客
- 踩坑ThinkPHP5之模型对象返回的数据集如何转为数组
- PHP基础编程之鬼斧神工的正则表达式-正则表达式基本语法+简单实例
- PHP+mysql数据库简单分页实例-sql分页
- PHP基础编程之鬼斧神工的正则表达式-正则表达式初探+常用的正则表达式函数
- php基础编程-php连接mysql数据库-mysqli的简单使用