Android自定义View圆形图片控件代码详解
作者:milovetingting 发布时间:2022-05-15 12:00:53
标签:Android,自定义,View
前言
在日常开发中,圆形的图片效果还是很常见的。可以通过给Paint设置Xfermode来实现,这里简单记录如下。
实现
实现圆形效果的核心是PorterDuffXfermode,对于PorterDuffXfermode,这里不展开,可以查询相关资料。
核心代码
//绘制背景
canvas.drawCircle(mSize / 2, mSize / 2, mSize / 2, mPaint);
//设置模式为:显示背景层和上层的交集,且显示上层图像
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
//绘制要显示的图像
canvas.drawBitmap(mSrcBitmap, 0, 0, mPaint);
//重置Xfermode
mPaint.setXfermode(null);
自定义属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CircleView">
<!--定义资源-->
<attr name="src" format="reference" />
<!--定义类型-->
<attr name="type" format="enum">
<!--圆形-->
<enum name="round" value="1" />
<!--矩形-->
<enum name="rect" value="2" />
</attr>
</declare-styleable>
</resources>
自定义控件
public class CircleView extends View {
private static final int DEFAULT_SIZE = 200;
private static final int DEFAULT_RADIUS = 20;
private static final int TYPE_ROUND = 1;
private static final int TYPE_RECT = 2;
private int mSize;
private int mResourceId;
private int mType;
private Paint mPaint;
private Bitmap mSrcBitmap;
public CircleView(Context context) {
this(context, null);
}
public CircleView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CircleView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CircleView);
mResourceId = ta.getResourceId(R.styleable.CircleView_src, R.mipmap.ic_launcher);
mType = ta.getInt(R.styleable.CircleView_type, TYPE_ROUND);
ta.recycle();
init();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = getMeasureSize(widthMeasureSpec);
int height = getMeasureSize(heightMeasureSpec);
mSize = Math.min(width, height);
setMeasuredDimension(mSize, mSize);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//绘制背景
if (mSrcBitmap == null) {
mSrcBitmap = getScaleBitmap();
}
if (mType == TYPE_ROUND) {
canvas.drawCircle(mSize / 2, mSize / 2, mSize / 2, mPaint);
} else if (mType == TYPE_RECT) {
canvas.drawRoundRect(0, 0, mSize, mSize, DEFAULT_RADIUS, DEFAULT_RADIUS, mPaint);
}
//设置模式为:显示背景层和上层的交集,且显示上层图像
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
//绘制要显示的图像
canvas.drawBitmap(mSrcBitmap, 0, 0, mPaint);
//重置Xfermode
mPaint.setXfermode(null);
}
private void init() {
//禁用硬件加速,否则可能无法绘制圆形
setLayerType(LAYER_TYPE_HARDWARE, null);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.FILL);
}
private int getMeasureSize(int measureSpec) {
int mode = MeasureSpec.getMode(measureSpec);
int size = MeasureSpec.getSize(measureSpec);
return mode == MeasureSpec.EXACTLY ? size : DEFAULT_SIZE;
}
/**
* 获取缩放后的Bitmap
*
* @return
*/
private Bitmap getScaleBitmap() {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), mResourceId, options);
options.inSampleSize = calcSampleSize(options, mSize, mSize);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(getResources(), mResourceId, options);
}
/**
* 计算缩放比例
*
* @param option
* @param width
* @param height
* @return
*/
private int calcSampleSize(BitmapFactory.Options option, int width, int height) {
int originWidth = option.outWidth;
int originHeight = option.outHeight;
int sampleSize = 1;
while ((originWidth = originWidth >> 1) > width && (originHeight = originHeight >> 1) > height) {
sampleSize = sampleSize << 1;
}
return sampleSize;
}
}
注意:如果没有圆形的效果,那么可能需要禁用硬件加速:setLayerType(LAYER_TYPE_HARDWARE, null)
布局
<?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:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".MainActivity">
<com.wangyz.custom.CircleView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:src="@drawable/image" />
<com.wangyz.custom.CircleView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
app:src="@drawable/image" />
<com.wangyz.custom.CircleView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
app:src="@drawable/image"
app:type="rect" />
</LinearLayout>
效果
来源:https://www.cnblogs.com/milovetingting/p/13071429.html


猜你喜欢
- 本文实例讲述了Java中方法名称和泛型相同的用法。分享给大家供大家参考,具体如下:一 点睛Java中,方法的名称可以用泛型替代。二 实战1
- 一、前言让我们先理一下springfox与swagger的关系。swagger是一个流行的API开发框架,这个框架以“开放API声明”(Op
- 定义工具类-创建对应的日志对象定义枚举类-存储定义的日志文件名称logback.xml里配置对应的日志名称和日志等级1、工具类 Logger
- C#判断数据类型的简单示例代码:int i = 5; Console
- 一、简介用法ScrollView大致相同二、方法1)HorizontalScrollView水平滚动控件使用方法1、在layout布局文件的
- 如何查看 Java 的字节码文件?在 Java 中,字节码文件.class实际上是二进制文件,并不能直接查看。要想查看,我们只能通过反编译对
- 一、读取系统配置文件application.yaml1、application.yaml配置文件中增加一下测试配置testdata: &nb
- 一、前言1.1 实现目标服务A调用服务B1和B2(B1和B2提供同种服务),当服务B1/B2在停止和重新发布阶段,或B1/B2有一个服务故障
- 概要应同学邀请,演示如何使用 PyQt5 内嵌浏览器浏览网页,并注入 Javascript 脚本实现自动化操作。下面测试的是一个廉价机票预订
- 前言短信验证码是通过发送验证码到手机的一种有效的验证码系统。主要用于验证用户手机的合法性及敏感操作的身份验证。现在市面上的短信服务平台有很多
- 今天我们来编写一个缩放效果的ImageView ,网上有很多人都讲了这些。但有许多人都直接使用了库文件,那么我们今天做的是直接上代码编写一个
- 看一段程序String t = "a||b||c||d";String[] temp = t.split("\
- Android Studio生成的APP默认图标是经典的机器人图标。可以通过Android Studio实现APP图标和名称的修改。1 修改
- 前提在日常使用SpringMVC进行开发的时候,有可能遇到前端各种类型的请求参数,这里做一次相对全面的总结。SpringMVC中处理控制器参
- maven什么是mavenMaven 是一个项目管理工具,最主要的作用就是管理jar包,他可以把jar统一放到仓库中,项目直接引用即可,而不
- Spring Boot 集成MyBatis在集成MyBatis前,我们先配置一个druid数据源。Spring Boot 集成druiddr
- Java当中的类和对象1. 类和对象的初步认知java 是一门面向对象的语言,所谓面向对象有别于面向过程,面向对象是只需对象之间的交互即可完
- 一 前言最近网上比较火的代码生成器,知识追寻者抽空试试了一下,感觉不是友好,只能说功能比较呆板吧,还需要自己玩填空题,修修补补,然后再次打开
- 1 引入 pom 包<dependency> <groupId>io.github.res
- 本文实例讲述了Spring和Hibernate的整合操作。分享给大家供大家参考,具体如下:一 web配置<?xml version=&