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


    1.自定义RoundImageView

    1. package com.examle.widget;
    2. import android.content.Context;
    3. import android.content.res.TypedArray;
    4. import android.graphics.Bitmap;
    5. import android.graphics.Canvas;
    6. import android.graphics.Color;
    7. import android.graphics.Paint;
    8. import android.graphics.PorterDuff;
    9. import android.graphics.PorterDuffXfermode;
    10. import android.graphics.drawable.Drawable;
    11. import android.util.AttributeSet;
    12. import android.util.TypedValue;
    13. import android.widget.ImageView;
    14. import androidx.annotation.ColorInt;
    15. import androidx.annotation.Dimension;
    16. import androidx.annotation.Nullable;
    17. import com.examlpe.R;
    18. public class RoundImageView extends ImageView {
    19. private String TGA=RoundImageView.class.getSimpleName();
    20. private final PorterDuffXfermode xfermode=new PorterDuffXfermode(PorterDuff.Mode.DST_ATOP);//绘制模式
    21. private final Paint mPaint;//实体paint
    22. private final Paint mStrokePaint;//描边paint
    23. private int mRadius;//圆角值
    24. private int mStrokeWidthColor;//描边颜色
    25. private int mStrokeWidth;//描边宽度
    26. private boolean mIsCircle;//true-圆形模式,false-圆角矩形模式
    27. public RoundImageView(Context context) {
    28. this(context, null);
    29. }
    30. public RoundImageView(Context context, @Nullable AttributeSet attrs) {
    31. this(context, attrs, 0);
    32. }
    33. public RoundImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    34. this(context, attrs, defStyleAttr, 0);
    35. }
    36. public RoundImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    37. super(context, attrs, defStyleAttr, defStyleRes);
    38. TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundImageView);
    39. mStrokeWidthColor = a.getColor(R.styleable.RoundImageView_riv_stroke_color, Color.WHITE);
    40. mStrokeWidth = a.getDimensionPixelSize(R.styleable.RoundImageView_riv_stroke_width, 0);
    41. mRadius = a.getDimensionPixelSize(R.styleable.RoundImageView_riv_round_radius, 0);
    42. mIsCircle = a.getBoolean(R.styleable.RoundImageView_riv_circle, false);
    43. a.recycle();
    44. mPaint = new Paint();
    45. mPaint.setAntiAlias(true);//抗锯齿
    46. mPaint.setColor(Color.WHITE);
    47. mPaint.setStyle(Paint.Style.FILL);
    48. mStrokePaint = new Paint();
    49. mStrokePaint.setAntiAlias(true);//抗锯齿
    50. mStrokePaint.setStyle(Paint.Style.STROKE);
    51. }
    52. @Override
    53. protected void onDraw(Canvas canvas) {
    54. int layerId = canvas.saveLayer(0, 0, getWidth(), getHeight(), null);//保存图层
    55. super.onDraw(canvas);
    56. Drawable src = getDrawable();
    57. int tmpBpW = getWidth() - getPaddingLeft() - getPaddingRight();//位图宽度,必须大于0
    58. int tmpBpH = getHeight() - getPaddingTop() - getPaddingBottom();//位图高度,必须大于0
    59. if (src != null && getWidth() > 0 && getHeight() > 0 && tmpBpW>0 && tmpBpH>0) {
    60. Bitmap tmpBp = Bitmap.createBitmap(tmpBpW, tmpBpH, Bitmap.Config.ARGB_8888);
    61. Canvas tmpCv = new Canvas(tmpBp);//tmpBp画布
    62. float tmpR = Math.min(tmpBp.getWidth(), tmpBp.getHeight()) * 0.5f;//取最小宽度
    63. if (mIsCircle) {//圆形模式
    64. tmpCv.drawCircle(tmpR, tmpR, tmpR, mPaint);//绘制圆形
    65. } else {//圆角矩形模式
    66. tmpCv.drawRoundRect(0, 0, tmpBp.getWidth(), tmpBp.getHeight(), mRadius, mRadius, mPaint);//绘制圆角矩形
    67. }
    68. mPaint.setXfermode(xfermode);//绘制模式
    69. canvas.drawBitmap(tmpBp, getPaddingLeft(), getPaddingTop(), mPaint);//绘制位图
    70. if (mStrokeWidth > 0) {//描边
    71. mStrokePaint.setColor(mStrokeWidthColor);//描边颜色
    72. mStrokePaint.setStrokeWidth(mStrokeWidth);//描边宽度
    73. if (mIsCircle) {//圆形模式
    74. canvas.drawCircle(getPaddingLeft()+tmpR, getPaddingTop()+tmpR, tmpR-mStrokeWidth*0.5f, mStrokePaint);
    75. } else {//圆角矩形模式
    76. 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);//绘制圆角
    77. }
    78. }
    79. }
    80. canvas.restoreToCount(layerId);//恢复图层
    81. }
    82. /**
    83. * 圆角值 dp
    84. * @param dpValue
    85. */
    86. public void setRadius(@Dimension int dpValue) {
    87. this.mRadius =(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dpValue,getResources().getDisplayMetrics());
    88. invalidate();
    89. }
    90. /**
    91. * 描边颜色
    92. * @param strokeWidthColor
    93. */
    94. public void setStrokeWidthColor(@ColorInt int strokeWidthColor) {
    95. this.mStrokeWidthColor = strokeWidthColor;
    96. invalidate();
    97. }
    98. /**
    99. * 描边宽度 dp
    100. * @param dpValue
    101. */
    102. public void setStrokeWidth(@Dimension int dpValue) {
    103. this.mStrokeWidth =(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dpValue,getResources().getDisplayMetrics());
    104. invalidate();
    105. }
    106. /**
    107. * 圆角矩形 或 圆形控件
    108. * @param isCircle
    109. */
    110. public void setCircle(boolean isCircle) {
    111. this.mIsCircle = isCircle;
    112. invalidate();
    113. }
    114. }

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

    1. "1.0" encoding="utf-8"?>
    2. <resources>
    3. <declare-styleable name="RoundImageView">
    4. <attr name="riv_stroke_width" format="dimension" />
    5. <attr name="riv_stroke_color" format="color" />
    6. <attr name="riv_round_radius" format="dimension" />
    7. <attr name="riv_circle" format="boolean"/>
    8. declare-styleable>
    9. resources>

    3.布局使用:圆角矩形

    1. <com.examlpe.widget.RoundImageView
    2. android:id="@+id/riv"
    3. android:layout_width="180dp"
    4. android:layout_height="180dp"
    5. app:riv_circle="false"
    6. android:scaleType="fitXY"
    7. app:riv_round_radius="20dp"
    8. app:riv_stroke_width="2dp"
    9. app:riv_stroke_color="@color/white"
    10. android:src="@mipmap/ic_launcher" />

    4.布局使用:圆形控件

    1. <com.examlpe.widget.RoundImageView
    2. android:id="@+id/riv"
    3. android:layout_width="180dp"
    4. android:layout_height="180dp"
    5. app:riv_circle="true"
    6. android:scaleType="fitXY"
    7. app:riv_stroke_width="2dp"
    8. app:riv_stroke_color="@color/white"
    9. android:src="@mipmap/ic_launcher" />

  • 相关阅读:
    nlp 分词 提取关键词的基本操作
    acwing算法基础之搜索与图论--BFS
    小红书家装节期间,如何打造优质品牌内容?
    c++中的函数
    GitHub Pulse 是什么?它是否能衡量 OpenTiny 开源项目的健康程度?
    面试官:告诉我为什么static和transient关键字修饰的变量不能被序列化?
    Idea下面git的使用:变基、合并、优选、还原提交、重置、回滚、补丁
    网工知识角|12个交换机相关术语 ,你知道多少?随时随地扩容你的网工技能
    数通运营商方向常见面试问题(第三部分)
    vue后台开发第一步
  • 原文地址:https://blog.csdn.net/qq_29364417/article/details/136541095