当前位置: 首页 > news >正文

android 快速实现 圆角矩形控件 及 圆形控件

1.自定义RoundImageView

package com.examle.widget;import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.ImageView;import androidx.annotation.ColorInt;
import androidx.annotation.Dimension;
import androidx.annotation.Nullable;import com.examlpe.R;public class RoundImageView extends ImageView {private String TGA=RoundImageView.class.getSimpleName();private final PorterDuffXfermode xfermode=new PorterDuffXfermode(PorterDuff.Mode.DST_ATOP);//绘制模式private final Paint mPaint;//实体paintprivate final Paint mStrokePaint;//描边paintprivate int mRadius;//圆角值private int mStrokeWidthColor;//描边颜色private int mStrokeWidth;//描边宽度private boolean mIsCircle;//true-圆形模式,false-圆角矩形模式public RoundImageView(Context context) {this(context, null);}public RoundImageView(Context context, @Nullable AttributeSet attrs) {this(context, attrs, 0);}public RoundImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {this(context, attrs, defStyleAttr, 0);}public RoundImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {super(context, attrs, defStyleAttr, defStyleRes);TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundImageView);mStrokeWidthColor = a.getColor(R.styleable.RoundImageView_riv_stroke_color, Color.WHITE);mStrokeWidth = a.getDimensionPixelSize(R.styleable.RoundImageView_riv_stroke_width, 0);mRadius = a.getDimensionPixelSize(R.styleable.RoundImageView_riv_round_radius, 0);mIsCircle = a.getBoolean(R.styleable.RoundImageView_riv_circle, false);a.recycle();mPaint = new Paint();mPaint.setAntiAlias(true);//抗锯齿mPaint.setColor(Color.WHITE);mPaint.setStyle(Paint.Style.FILL);mStrokePaint = new Paint();mStrokePaint.setAntiAlias(true);//抗锯齿mStrokePaint.setStyle(Paint.Style.STROKE);}@Overrideprotected void onDraw(Canvas canvas) {int layerId = canvas.saveLayer(0, 0, getWidth(), getHeight(), null);//保存图层super.onDraw(canvas);Drawable src = getDrawable();int tmpBpW = getWidth() - getPaddingLeft() - getPaddingRight();//位图宽度,必须大于0int tmpBpH = getHeight() - getPaddingTop() - getPaddingBottom();//位图高度,必须大于0if (src != null && getWidth() > 0 && getHeight() > 0 && tmpBpW>0 && tmpBpH>0) {Bitmap tmpBp = Bitmap.createBitmap(tmpBpW, tmpBpH, Bitmap.Config.ARGB_8888);Canvas tmpCv = new Canvas(tmpBp);//tmpBp画布float tmpR = Math.min(tmpBp.getWidth(), tmpBp.getHeight()) * 0.5f;//取最小宽度if (mIsCircle) {//圆形模式tmpCv.drawCircle(tmpR, tmpR, tmpR, mPaint);//绘制圆形} else {//圆角矩形模式tmpCv.drawRoundRect(0, 0, tmpBp.getWidth(), tmpBp.getHeight(), mRadius, mRadius, mPaint);//绘制圆角矩形}mPaint.setXfermode(xfermode);//绘制模式canvas.drawBitmap(tmpBp, getPaddingLeft(), getPaddingTop(), mPaint);//绘制位图if (mStrokeWidth > 0) {//描边mStrokePaint.setColor(mStrokeWidthColor);//描边颜色mStrokePaint.setStrokeWidth(mStrokeWidth);//描边宽度if (mIsCircle) {//圆形模式canvas.drawCircle(getPaddingLeft()+tmpR, getPaddingTop()+tmpR, tmpR-mStrokeWidth*0.5f, mStrokePaint);} else {//圆角矩形模式canvas.drawRoundRect(getPaddingLeft()+mStrokeWidth*0.5f, getPaddingTop()+mStrokeWidth*0.5f, getWidth() - getPaddingRight()-mStrokeWidth*0.5f, getHeight() - getPaddingBottom()-mStrokeWidth*0.5f, mRadius-mStrokeWidth*0.5f, mRadius-mStrokeWidth*0.5f, mStrokePaint);//绘制圆角}}}canvas.restoreToCount(layerId);//恢复图层}/*** 圆角值 dp* @param dpValue*/public void setRadius(@Dimension int dpValue) {this.mRadius =(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dpValue,getResources().getDisplayMetrics());invalidate();}/*** 描边颜色* @param strokeWidthColor*/public void setStrokeWidthColor(@ColorInt int strokeWidthColor) {this.mStrokeWidthColor = strokeWidthColor;invalidate();}/*** 描边宽度 dp* @param dpValue*/public void setStrokeWidth(@Dimension int dpValue) {this.mStrokeWidth =(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dpValue,getResources().getDisplayMetrics());invalidate();}/*** 圆角矩形 或 圆形控件* @param isCircle*/public void setCircle(boolean isCircle) {this.mIsCircle = isCircle;invalidate();}
}

2.新建attrs.xml 文件,路径 res/values/attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources><declare-styleable name="RoundImageView"><attr name="riv_stroke_width" format="dimension" /><attr name="riv_stroke_color" format="color" /><attr name="riv_round_radius" format="dimension" /><attr name="riv_circle" format="boolean"/></declare-styleable>
</resources>

3.布局使用:圆角矩形

        <com.examlpe.widget.RoundImageViewandroid:id="@+id/riv"android:layout_width="180dp"android:layout_height="180dp"app:riv_circle="false"android:scaleType="fitXY"app:riv_round_radius="20dp"app:riv_stroke_width="2dp"app:riv_stroke_color="@color/white"android:src="@mipmap/ic_launcher" />

4.布局使用:圆形控件

        <com.examlpe.widget.RoundImageViewandroid:id="@+id/riv"android:layout_width="180dp"android:layout_height="180dp"app:riv_circle="true"android:scaleType="fitXY"app:riv_stroke_width="2dp"app:riv_stroke_color="@color/white"android:src="@mipmap/ic_launcher" />

http://www.lryc.cn/news/313569.html

相关文章:

  • 【Python】外网远程登录访问jupyter notebook+pycharm使用ipython
  • error:0308010C:digital envelope routines::unsupported
  • Vue前端的工作需求
  • 97. 常用的HTTP服务压测工具
  • 活动预告|听云猿生数据创始人 CEO 曹伟分享云数据库行业十余年经验总结
  • 数仓实战——京东数据指标体系的构建与实践
  • Alias许可配置
  • 【读书笔记】针对ICS的ATTCK矩阵详解(一)
  • Rust多线程访问数据,推荐使用mutex还是channel?
  • 基于pytorch的手写体识别
  • Leetcode 56. 合并区间
  • C++:List的使用和模拟实现
  • 20个Python函数程序实例
  • Wireshark——获取捕获流量的前N个数据包
  • 006-浏览器输入域名到返回
  • 【kubernetes】关于k8s集群如何将pod调度到指定node节点?
  • 【框架】React和Vue的异同
  • 如何选择阅读软件技术学习书籍
  • 做抖店用平板能代替电脑操作吗?抖店运营相关注意事项,注意规避
  • 【FastChat】用于训练、服务和评估大型语言模型的开放平台
  • 从根到叶:深入理解二叉搜索树
  • 网络信息安全:11个常见漏洞类型汇总
  • 阿里云服务器使用教程_2024建站教程_10分钟网站搭建流程
  • 【排序算法】推排序算法解析:从原理到实现
  • Python爬虫实战(基础篇)—13获取《人民网》【最新】【国内】【国际】写入Word(附完整代码)
  • 常见控件应用
  • 什么是降压恒流芯片?它有什么作用?
  • 14:00面试,15:00就出来了,问的问题过于变态了。。。
  • Maven的settings.xml配置
  • 利用redis实现秒杀功能