Android图片圆角转换 RoundedImageView开源项目 小记(1)

news2025/4/21 18:26:04

android:background=“#7f000000”

android:paddingLeft=“8dp”

android:paddingRight=“8dp”

android:textAppearance=“?android:attr/textAppearanceMediumInverse” />

<TextView

android:id=“@+id/textView1”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_above=“@+id/textView2”

android:layout_alignLeft=“@+id/textView2”

android:layout_marginBottom=“4dp”

android:background=“#7f000000”

android:paddingLeft=“8dp”

android:paddingRight=“8dp”

android:textAppearance=“?android:attr/textAppearanceLargeInverse” />

如下图:

用代码创建 :

RoundedImageView iv = new RoundedImageView(context);

iv.setScaleType(ScaleType.CENTER_CROP);

iv.setCornerRadius(10);

iv.setBorderWidth(2);

iv.setBorderColor(Color.DKGRAY);

iv.setRoundedBackground(true);

iv.setImageDrawable(drawable);

iv.setBackground(backgroundDrawable);

iv.setOval(true);

贴上部分源码:

package com.makeramen;

import android.content.Context;

import android.content.res.ColorStateList;

import android.content.res.Resources;

import android.content.res.TypedArray;

import android.graphics.Bitmap;

import android.graphics.drawable.Drawable;

import android.graphics.drawable.LayerDrawable;

import android.net.Uri;

import android.util.AttributeSet;

import android.util.Log;

import android.widget.ImageView;

@SuppressWarnings(“UnusedDeclaration”)

public class RoundedImageView extends ImageView {

public static final String TAG = “RoundedImageView”;

public static final float DEFAULT_RADIUS = 0f;

public static final float DEFAULT_BORDER_WIDTH = 0f;

private static final ScaleType[] SCALE_TYPES = {

ScaleType.MATRIX,

ScaleType.FIT_XY,

ScaleType.FIT_START,

ScaleType.FIT_CENTER,

ScaleType.FIT_END,

ScaleType.CENTER,

ScaleType.CENTER_CROP,

ScaleType.CENTER_INSIDE

};

private float cornerRadius = DEFAULT_RADIUS;

private float borderWidth = DEFAULT_BORDER_WIDTH;

private ColorStateList borderColor =

ColorStateList.valueOf(RoundedDrawable.DEFAULT_BORDER_COLOR);

private boolean isOval = false;

private boolean mutateBackground = false;

private int mResource;

private Drawable mDrawable;

private Drawable mBackgroundDrawable;

private ScaleType mScaleType;

public RoundedImageView(Context context) {

super(context);

}

public RoundedImageView(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

public RoundedImageView(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundedImageView, defStyle, 0);

int index = a.getInt(R.styleable.RoundedImageView_android_scaleType, -1);

if (index >= 0) {

setScaleType(SCALE_TYPES[index]);

} else {

// default scaletype to FIT_CENTER

setScaleType(ScaleType.FIT_CENTER);

}

cornerRadius = a.getDimensionPixelSize(R.styleable.RoundedImageView_corner_radius, -1);

borderWidth = a.getDimensionPixelSize(R.styleable.RoundedImageView_border_width, -1);

// don’t allow negative values for radius and border

if (cornerRadius < 0) {

cornerRadius = DEFAULT_RADIUS;

}

if (borderWidth < 0) {

borderWidth = DEFAULT_BORDER_WIDTH;

}

borderColor = a.getColorStateList(R.styleable.RoundedImageView_border_color);

if (borderColor == null) {

borderColo
r = ColorStateList.valueOf(RoundedDrawable.DEFAULT_BORDER_COLOR);

}

mutateBackground = a.getBoolean(R.styleable.RoundedImageView_mutate_background, false);

isOval = a.getBoolean(R.styleable.RoundedImageView_oval, false);

updateDrawableAttrs();

updateBackgroundDrawableAttrs(true);

a.recycle();

}

@Override

protected void drawableStateChanged() {

super.drawableStateChanged();

invalidate();

}

/**

  • Return the current scale type in use by this ImageView.

  • @attr ref android.R.styleable#ImageView_scaleType

  • @see android.widget.ImageView.ScaleType

*/

@Override

public ScaleType getScaleType() {

return mScaleType;

}

/**

  • Controls how the image should be resized or moved to match the size

  • of this ImageView.

  • @param scaleType The desired scaling mode.

  • @attr ref android.R.styleable#ImageView_scaleType

*/

@Override

public void setScaleType(ScaleType scaleType) {

assert scaleType != null;

if (mScaleType != scaleType) {

mScaleType = scaleType;

switch (scaleType) {

case CENTER:

case CENTER_CROP:

case CENTER_INSIDE:

case FIT_CENTER:

case FIT_START:

case FIT_END:

case FIT_XY:

super.setScaleType(ScaleType.FIT_XY);

break;

default:

super.setScaleType(scaleType);

break;

}

updateDrawableAttrs();

updateBackgroundDrawableAttrs(false);

invalidate();

}

}

@Override

public void setImageDrawable(Drawable drawable) {

mResource = 0;

mDrawable = RoundedDrawable.fromDrawable(drawable);

updateDrawableAttrs();

super.setImageDrawable(mDrawable);

}

@Override

public void setImageBitmap(Bitmap bm) {

mResource = 0;

mDrawable = RoundedDrawable.fromBitmap(bm);

updateDrawableAttrs();

super.setImageDrawable(mDrawable);

}

@Override

public void setImageResource(int resId) {

if (mResource != resId) {

mResource = resId;

mDrawable = resolveResource();

updateDrawableAttrs();

super.setImageDrawable(mDrawable);

}

}

@Override public void setImageURI(Uri uri) {

super.setImageURI(uri);

setImageDrawable(getDrawable());

}

private Drawable resolveResource() {

Resources rsrc = getResources();

if (rsrc == null) { return null; }

Drawable d = null;

if (mResource != 0) {

try {

d = rsrc.getDrawable(mResource);

} catch (Exception e) {

Log.w(TAG, "Unable to find resource: " + mResource, e);

// Don’t try again.

mResource = 0;

}

}

return RoundedDrawable.fromDrawable(d);

}

@Override

public void setBackground(Drawable background) {

setBackgroundDrawable(background);

}

private void updateDrawableAttrs() {

updateAttrs(mDrawable);

}

private void updateBackgroundDrawableAttrs(boolean convert) {

if (mutateBackground) {

if (convert) {

mBackgroundDrawable = RoundedDrawable.fromDrawable(mBackgroundDrawable);

}

updateAttrs(mBackgroundDrawable);

}

}

private void updateAttrs(Drawable drawable) {

if (drawable == null) { return; }

if (drawable instanceof RoundedDrawable) {

((RoundedDrawable) drawable)

.setScaleType(mScaleType)

.setCornerRadius(cornerRadius)

.setBorderWidth(borderWidth)

.setBorderColor(borderColor)

.setOval(isOval);

} else if (drawable instanceof LayerDrawable) {

// loop through layers to and set drawable attrs

LayerDrawable ld = ((LayerDrawable) drawable);

for (int i = 0, layers = ld.getNumberOfLayers(); i < layers; i++) {

updateAttrs(ld.getDrawable(i));

}

}

}

@Override

@Deprecated

public void setBackgroundDrawable(Drawable background) {

mBackgroundDrawable = background;

updateBackgroundDrawableAttrs(true);

super.setBackgroundDrawable(mBackgroundDrawable);

}

public float getCornerRadius() {

return cornerRadius;

}

public void setCornerRadius(int resId) {

setCornerRadius(getResources().getDimension(resId));

}

public void setCornerRadius(float radius) {

if (cornerRadius == radius) { return; }

cornerRadius = radius;

updateDrawableAttrs();

updateBackgroundDrawableAttrs(false);

}

public float getBorderWidth() {

return borderWidth;

}

public void setBorderWidth(int resId) {

setBorderWidth(getResources().getDimension(resId));

}

public void setBorderWidth(float width) {

if (borderWidth == width) { return; }

borderWidth = width;

updateDrawableAttrs();

updateBackgroundDrawableAttrs(false);

invalidate();

}

public int getBorderColor() {

return borderColor.getDefaultColor();

}

public void setBorderColor(int color) {

setBorderColor(ColorStateList.valueOf(color));

}

public ColorStateList getBorderColors() {

return borderColor;

}

public void setBorderColor(ColorStateList colors) {

if (borderColor.equals(colors)) { return; }

borderColor =

(colors != null) ? colors : ColorStateList.valueOf(RoundedDrawable.DEFAULT_BORDER_COLOR);

updateDrawableAttrs();

updateBackgroundDrawableAttrs(false);

if (borderWidth > 0) {

invalidate();

}

}

public boolean isOval() {

return isOval;

}

public void setOval(boolean oval) {

isOval = oval;

updateDrawableAttrs();

updateBackgroundDrawableAttrs(false);

invalidate();

}

public boolean isMutateBackground() {

return mutateBackground;

}

public void setMutateBackground(boolean mutate) {

if (mutateBackground == mutate) { return; }

mutateBackground = mutate;

updateBackgroundDrawableAttrs(true);

invalidate();

}

}

最后

小编这些年深知大多数初中级Android工程师,想要提升自己,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助

因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。

[外链图片转存中…(img-Zh0QShiD-1719034103397)]

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人

都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

资料⬅专栏获取
ublic void setOval(boolean oval) {

isOval = oval;

updateDrawableAttrs();

updateBackgroundDrawableAttrs(false);

invalidate();

}

public boolean isMutateBackground() {

return mutateBackground;

}

public void setMutateBackground(boolean mutate) {

if (mutateBackground == mutate) { return; }

mutateBackground = mutate;

updateBackgroundDrawableAttrs(true);

invalidate();

}

}

最后

小编这些年深知大多数初中级Android工程师,想要提升自己,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助

因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。

[外链图片转存中…(img-Zh0QShiD-1719034103397)]

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人

都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

资料⬅专栏获取

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1850643.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

c++ 内存分析模型、引用

一、内存模型分区 内存四区的意义&#xff1a; 不同区域存放的数据&#xff0c;赋予不同的生命周期&#xff0c;给我们更大的灵活编程 &#xff08;一&#xff09;程序运行前 在程序编译后&#xff0c;生成了exe可执行程序&#xff0c;未执行程序前分为两个区域 代码区&…

Linux常用命令(14)—查看文件内容(有相关截图)

写在前面&#xff1a; 最近在学习Linux命令&#xff0c;记录一下学习Linux常用命令的过程&#xff0c;方便以后复习。仅供参考&#xff0c;若有不当的地方&#xff0c;恳请指正。如果对你有帮助&#xff0c;欢迎点赞&#xff0c;关注&#xff0c;收藏&#xff0c;评论&#xf…

基于Java技术的摄影跟拍预定管理系统

你好&#xff0c;我是热衷于计算机科学与技术研究的码农小野。如果你对摄影跟拍预定管理系统感兴趣或有相关开发需求&#xff0c;欢迎私信交流。 开发语言 Java 数据库 MySQL 技术 B/S模式&#xff0c;SpringBoot 工具 Eclipse&#xff0c;Navicat&#xff0c;Tomcat …

Nuxt3 [Vue warn]: Hydration node mismatch:【解决方案】

[Vue warn]: Hydration node mismatch: 水合节点不匹配 Server rendered element contains more child nodes than client vdom. 服务器呈现的元素包含的子节点多于客户端vdom。 这个问题解决起来也很好解决&#xff0c;看这个问题是怎么出来的&#xff0c;看代码&#xff1a;…

Unity 天空盒制作使用教程

文章目录 1.概念2.制作天空盒3.使用天空盒3.1 为场景添加3.2 为相机添加 1.概念 天空盒是包裹整个场景的环境效果。 2.制作天空盒 1、创建材质球。 2、设置材质球Shader为SkyBox/6 Sided&#xff0c;将六张贴图放到对应位置。 3.使用天空盒 3.1 为场景添加 方法一、直接…

Vue3 - 在项目中使用vue-i18n不生效的问题

检查和配置 Vue I18n 确保你已经正确安装了Vue I18n并且配置了组合API模式。 安装 Vue I18n npm install vue-i18nnext配置 i18n.js import { createI18n } from vue-i18n; import messages from ./messages;const i18n createI18n({legacy: false, // 使用组合 API 模式l…

【Mysql】DQL操作单表、创建数据库、排序、聚合函数、分组、limit关键字

DQL操作单表 1.1 创建数据库 •创建一个新的数据库 db2 CREATE DATABASE db2 CHARACTER SET utf8;•将db1数据库中的 emp表 复制到当前 db2数据库 ** 1.2 排序** 通过 ORDER BY 子句,可以将查询出的结果进行排序 (排序只是显示效果,不会影响真实数据) 语法结构&#xff1a;…

计算机系统基础实训八—ProxyLab实验

实验目的与要求 1、让学生应用套接字接口实现网络编程&#xff1b; 2、让学生理解Web服务器开发的相关知识&#xff1b; 3、让学生应用并发编程技术进行并发服务器的开发&#xff1b; 实验原理与内容 Web代理是一种在Web浏览器和终端服务器之间充当中介角色的程序。在Web代…

可灵王炸更新,图生视频、视频续写,最长可达3分钟!Runway 不香了 ...

现在视频大模型有多卷&#xff1f; Runway 刚在6月17号 发布Gen3 &#xff0c;坐上王座没几天&#xff1b; 可灵就在6月21日中午&#xff0c;重新夺回了王座&#xff01;发布了图生视频功能&#xff0c;视频续写功能&#xff01; 一张图概括&#xff1a; 二师兄和团队老师第一…

链表中环的入口节点

链表中环的入口节点 描述 链表中环的入口节点 给一个长度为n链表&#xff0c;若其中包含环&#xff0c;请找出该链表的环的入口结点&#xff0c;否则&#xff0c;返回null。 数据范围&#xff1a; n≤10000&#xff0c; 1<结点值<10000 要求&#xff1a;空间复杂度 O(1)…

剪画音频提取:周杰伦音乐自由听,谁还付费听歌呀!

​作为周杰伦的狂热粉丝&#xff0c;你是否常常为各大音乐软件的会员限制而感到烦恼&#xff1f;每当想听一首心仪的周杰伦歌曲&#xff0c;却被“会员专享”这几个字挡住去路&#xff0c;实在是令人扫兴。但是别担心&#xff0c;今天我将为你揭示一个神奇的方法——把视频提取…

嵌入式通信协议----Wi-Fi协议详解(二)(基于STM32+有人物联网WIFI模块)

四、有人WIFI模块 1.模块介绍 Wi-Fi 模块用于实现串口到 Wi-Fi 数据包的双向透明转发&#xff0c;模块内部完成协议转换&#xff0c;通 过该模块&#xff0c;客户可以将物理设备连接到 Wi-Fi 网络上&#xff0c;从而实现物联网的控制与管理。 2.模块参数 Wi-Fi 模块的…

网格布局之跨行越列

网格布局之跨行越列 欢迎关注&#xff1a;xssy5431 小拾岁月 参考链接&#xff1a;https://mp.weixin.qq.com/s/xStfSmewncTW49N0Y_Vhow 点击查看 使用场景 在常见的页面布局中&#xff0c;我们往往会遇到那种类似合并单元格的布局。比如&#xff1a;成绩排名、产品排名等等…

个体核定征收双免个体户0税率大额核定税率全行业筹划

工商银行的会计政策和程序规定 工商银行会计政策和程序规定 工商银行的会计政策和程序规定 https://www.9733.cn/shop/ssch 一、个体工商户核定征收 核定征收是一种简化税收管理方式&#xff0c;适用于簿记不健全、难以准确核算收入和成本的个体。以下是主要特点&#xff1a…

领先GPT-4o:Anthropic 推出新一代模型 Claude 3.5 Sonnet|TodayAI

Anthropic&#xff0c;全球领先的人工智能实验室之一&#xff0c;近日发布了其最新的人工智能模型——Claude 3.5 Sonnet。该模型不仅速度更快&#xff0c;成本更低&#xff0c;而且在多个关键任务上的表现超过了其前代模型 Claude 3 Opus。 更强的视觉功能与幽默感 Claude 3…

基于Java的农机电招平台系统

你好呀&#xff0c;我是计算机学姐码农小野&#xff01;如果你对农机电招平台系统感兴趣或有相关开发需求&#xff0c;可以私信联系我。 开发语言 Java 数据库 MySQL 技术 B/S结构&#xff0c;SpringBoot框架 工具 Eclipse&#xff0c;Navicat&#xff0c;Tomcat8.0 系…

EasyRecovery数据恢复软件2024免费版下载

EasyRecovery数据恢复软件&#xff0c;是我在电脑使用过程中遇到的神器&#xff01;它不仅功能强大&#xff0c;操作简便&#xff0c;还帮我找回了丢失的重要文件。今天&#xff0c;我就来给大家分享一下我的使用体验和心得。 让我来介绍一下EasyRecovery的功能。这款软件可以恢…

CentOS编译安装OpenSSL 3.3.1

正文共&#xff1a;666 字 8 图&#xff0c;预估阅读时间&#xff1a;1 分钟 我们前面介绍了如何通过Windows Server生成证书&#xff08;Windows Server配置生成认证证书&#xff09;&#xff0c;也介绍了如何通过easy-RSA生成证书文件&#xff08;使用Easy-RSA配置生成SSL证书…

2024-6-18(沉默Spring,Springboot)

1.Spring小结 我们最后再来体会一下用 Spring 创建对象的过程&#xff1a; 通过 ApplicationContext 这个 IoC 容器的入口&#xff0c;用它的两个具体的实现子类&#xff0c;从 class path 或者 file path 中读取数据&#xff0c;用 getBean() 获取具体的 bean instance。 那…

【计算机网络仿真】b站湖科大教书匠思科Packet Tracer——实验7 虚拟局域网VLAN

一、实验目的 1.学习如何划分VLAN&#xff1b; 2.验证划分VLAN的作用&#xff1b; 3.学习如何用命令行创建VLAN&#xff0c;将端口划分到VLAN&#xff0c;设置端口类型。 二、实验要求 1.使用Cisco Packet Tracer仿真平台&#xff1b; 2.观看B站湖科大教书匠仿真实验视频&am…