目前一些比较火的图片加载库虽然支持圆角加载,若你是接的别人作了一半的项目,刚好别人用的图片加载库刚好不支持圆角加载,那么这颗控件你值得拥有.(支持网络图片的加载)

1.创建CustomImageView 类在你的项目中(源码如下)
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
import com.towatt.charge.towatt.R;
/**
* @author Mr.lynn
* @version 1.0<br>
* 图片圆角实现
*/
public class CustomImageView extends android.support.v7.widget.AppCompatImageView {
private Paint paint;
private Paint paintBorder;
private Bitmap mSrcBitmap;
/**
* 圆角的弧度
*/
private float mRadius;
private boolean mIsCircle;
public CustomImageView(final Context context) {
this(context, null);
}
public CustomImageView(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.customImageViewStyle);
}
public CustomImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray ta = context.obtainStyledAttributes(attrs,
R.styleable.CustomImageView, defStyle, 0);
mRadius = ta.getDimension(R.styleable.CustomImageView_radius, 0);
mIsCircle = ta.getBoolean(R.styleable.CustomImageView_circle, false);
int srcResource = attrs.getAttributeResourceValue(
"http://schemas.android.com/apk/res/android", "src", 0);
if (srcResource != 0)
mSrcBitmap = BitmapFactory.decodeResource(getResources(),
srcResource);
ta.recycle();
paint = new Paint();
paint.setAntiAlias(true);
paintBorder = new Paint();
paintBorder.setAntiAlias(true);
}
@Override
public void onDraw(Canvas canvas) {
int width = canvas.getWidth() - getPaddingLeft() - getPaddingRight();
int height = canvas.getHeight() - getPaddingTop() - getPaddingBottom();
Bitmap image = drawableToBitmap(getDrawable());
if (mIsCircle) {
Bitmap reSizeImage = reSizeImageC(image, width, height);
canvas.drawBitmap(createCircleImage(reSizeImage, width, height),
getPaddingLeft(), getPaddingTop(), null);
} else {
Bitmap reSizeImage = reSizeImage(image, width, height);
canvas.drawBitmap(createRoundImage(reSizeImage, width, height),
getPaddingLeft(), getPaddingTop(), null);
}
}
/**
* 画圆角
*
* @param source
* @param width
* @param height
* @return
*/
private Bitmap createRoundImage(Bitmap source, int width, int height) {
Paint paint = new Paint();
paint.setAntiAlias(true);
Bitmap target = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(target);
RectF rect = new RectF(0, 0, width, height);
canvas.drawRoundRect(rect, mRadius, mRadius, paint);
// 核心代码取两个图片的交集部分
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(source, 0, 0, paint);
return target;
}
/**
* 画圆
*
* @param source
* @param width
* @param height
* @return
*/
private Bitmap createCircleImage(Bitmap source, int width, int height) {
Paint paint = new Paint();
paint.setAntiAlias(true);
Bitmap target = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(target);
canvas.drawCircle(width / 2, height / 2, Math.min(width, height) / 2,
paint);
// 核心代码取两个图片的交集部分
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(source, (width - source.getWidth()) / 2,
(height - source.getHeight()) / 2, paint);
return target;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(width, height);
}
/**
* drawable转bitmap
*
* @param drawable
* @return
*/
private Bitmap drawableToBitmap(Drawable drawable) {
if (drawable == null) {
if (mSrcBitmap != null) {
return mSrcBitmap;
} else {
return null;
}
} else if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
}
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
/**
* 重设Bitmap的宽高
*
* @param bitmap
* @param newWidth
* @param newHeight
* @return
*/
private Bitmap reSizeImage(Bitmap bitmap, int newWidth, int newHeight) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
// 计算出缩放比
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 矩阵缩放bitmap
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
}
/**
* 重设Bitmap的宽高
*
* @param bitmap
* @param newWidth
* @param newHeight
* @return
*/
private Bitmap reSizeImageC(Bitmap bitmap, int newWidth, int newHeight) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int x = (newWidth - width) / 2;
int y = (newHeight - height) / 2;
if (x > 0 && y > 0) {
return Bitmap.createBitmap(bitmap, 0, 0, width, height, null, true);
}
float scale = 1;
if (width > height) {
// 按照宽度进行等比缩放
scale = ((float) newWidth) / width;
} else {
// 按照高度进行等比缩放
// 计算出缩放比
scale = ((float) newHeight) / height;
}
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);
return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
}
}
2.在values目录下创建attrs.xml(若是已存在该文件)直接复制如下代码既可
<resources> <declare-styleable name="Theme"> <attr name="customImageViewStyle" format="reference" /> </declare-styleable> <!-- 自定义圆角ImageView --> <declare-styleable name="CustomImageView"> <attr name="circle" format="boolean" /> <attr name="radius" format="dimension" /> </declare-styleable> </resources>
3.正确的使用方式(在布局文件中)
注意此路径是你控件所在包
<com.xxx.xxx.view.CustomImageView android:layout_marginTop="5dp" android:layout_width="77dp" android:layout_height="77dp" lynn:radius="@dimen/size_10dp" android:src="@drawable/position_icon" android:id="@+id/iv_build_icon" />
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# Android
# 圆角
# ImageView
# Android自定义ImageView实现圆角功能
# Android实现自定义ImageView的圆角矩形图片效果
# Android自定义控件之圆形、圆角ImageView
# Android ImageView绘制圆角效果
# Android实现圆角矩形和圆形ImageView的方式
# Android 圆角 ImageView类可设置弧度(代码简单)
# Android自定义圆角ImageView
# Android中通过反射实现圆角ImageView代码实例
# Android自定义带圆角的ImageView
# 加载
# 计算出
# 作了
# 自定义
# 不支持
# 既可
# 该文件
# 这颗
# 大家多多
# 若你
# 目录下
# float
# mRadius
# paintBorder
# mSrcBitmap
# boolean
# final
# context
# null
相关文章:
如何撰写建站申请书?关键要点有哪些?
沈阳制作网站公司排名,沈阳装饰协会官方网站?
建站IDE高效指南:快速搭建+SEO优化+自适应模板全解析
如何有效防御Web建站篡改攻击?
如何通过可视化优化提升建站效果?
建设网站制作价格,怎样建立自己的公司网站?
移动端手机网站制作软件,掌上时代,移动端网站的谷歌SEO该如何做?
合肥做个网站多少钱,合肥本地有没有比较靠谱的交友平台?
湖南网站制作公司,湖南上善若水科技有限公司做什么的?
如何制作算命网站,怎么注册算命网站?
网站制作和推广的区别,想自己建立一个网站做推广,有什么快捷方法马上做好一个网站?
外贸公司网站制作,外贸网站建设一般有哪些步骤?
太平洋网站制作公司,网络用语太平洋是什么意思?
高端云建站费用究竟需要多少预算?
定制建站平台哪家好?企业官网搭建与快速建站方案推荐
南平网站制作公司,2025年南平市事业单位报名时间?
移民网站制作流程,怎么看加拿大移民官网?
如何确保FTP站点访问权限与数据传输安全?
婚礼视频制作网站,学习*后期制作的网站有哪些?
智能起名网站制作软件有哪些,制作logo的软件?
国美网站制作流程,国美电器蒸汽鍋怎么用官方网站?
制作假网页,招聘网的薪资待遇,会有靠谱的吗?一面试又各种折扣?
如何选择建站程序?包含哪些必备功能与类型?
建站之星2.7模板快速切换与批量管理功能操作指南
如何通过WDCP绑定主域名及创建子域名站点?
公司网站制作需要多少钱,找人做公司网站需要多少钱?
如何在腾讯云免费申请建站?
大连网站设计制作招聘信息,大连投诉网站有哪些?
如何快速上传自定义模板至建站之星?
如何在Tomcat中配置并部署网站项目?
如何解决VPS建站LNMP环境配置常见问题?
上海网站制作网站建设公司,建筑电工证网上查询系统入口?
网站制作企业,网站的banner和导航栏是指什么?
深圳企业网站制作设计,在深圳如何网上全流程注册公司?
c# 在ASP.NET Core中管理和取消后台任务
Python文件管理规范_工程实践说明【指导】
建站之星如何助力网站排名飙升?揭秘高效技巧
如何选择香港主机高效搭建外贸独立站?
如何在IIS中配置站点IP、端口及主机头?
宝塔建站教程:一键部署配置流程与SEO优化实战指南
网站制作知乎推荐,想做自己的网站用什么工具比较好?
如何选择网络建站服务器?高效建站必看指南
电商网站制作公司有哪些,1688网是什么意思?
七夕网站制作视频,七夕大促活动怎么报名?
香港服务器租用每月最低只需15元?
上海制作企业网站有哪些,上海有哪些网站可以让企业免费发布招聘信息?
网站制作公司排行榜,抖音怎样做个人官方网站
如何在自有机房高效搭建专业网站?
制作营销网站公司,淘特是干什么用的?
魔方云NAT建站如何实现端口转发?
*请认真填写需求信息,我们会在24小时内与您取得联系。