• android自定义view实现矩形下方倒三角


    效果图,内容文字可自行修改,颜色如果想自定义追加下就好。文字颜色也可以自定义属性自己在添加下就可以

    1. package com.fawersmart.engineer.View;
    2. import android.content.Context;
    3. import android.content.res.TypedArray;
    4. import android.graphics.Canvas;
    5. import android.graphics.Color;
    6. import android.graphics.Paint;
    7. import android.graphics.Path;
    8. import android.graphics.RectF;
    9. import android.util.AttributeSet;
    10. import android.view.View;
    11. import com.fawersmart.engineer.R;
    12. public class RectangleWithTriangleView extends View {
    13. private Paint paint;
    14. private Paint textPaint;
    15. private Path path;
    16. private String customText;
    17. public RectangleWithTriangleView(Context context) {
    18. super(context);
    19. init(null);
    20. }
    21. public RectangleWithTriangleView(Context context, AttributeSet attrs) {
    22. super(context, attrs);
    23. init(attrs);
    24. }
    25. public RectangleWithTriangleView(Context context, AttributeSet attrs, int defStyleAttr) {
    26. super(context, attrs, defStyleAttr);
    27. init(attrs);
    28. }
    29. private void init(AttributeSet attrs) {
    30. paint = new Paint();
    31. paint.setColor(Color.BLUE);
    32. paint.setStyle(Paint.Style.FILL);
    33. paint.setAntiAlias(true);
    34. textPaint = new Paint();
    35. textPaint.setColor(Color.WHITE);
    36. textPaint.setStyle(Paint.Style.FILL);
    37. textPaint.setAntiAlias(true);
    38. textPaint.setTextSize(28); // 文字大小,可以根据需要进行调整
    39. path = new Path();
    40. rectF = new RectF();
    41. TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.CustomView);
    42. customText = typedArray.getString(R.styleable.CustomView_customText);
    43. }
    44. private RectF rectF;
    45. @Override
    46. protected void onDraw(Canvas canvas) {
    47. super.onDraw(canvas);
    48. int width = getWidth();
    49. int height = getHeight();
    50. int triangleHeight = width / 10; // 三角形的高度为矩形宽度的四分之一
    51. int cornerRadius = 10; // 圆角半径,可以根据需要进行调整
    52. // 更新圆角矩形的位置和大小
    53. rectF.set(0, 0, width, height - triangleHeight);
    54. // 绘制圆角矩形
    55. canvas.drawRoundRect(rectF, cornerRadius, cornerRadius, paint);
    56. // 绘制倒三角
    57. // 绘制倒三角形
    58. path.reset();
    59. path.moveTo(0, 5);
    60. path.lineTo(width, 5);
    61. path.lineTo(width / 2f, height);
    62. path.close();
    63. canvas.drawPath(path, paint);
    64. // 绘制文字
    65. float textWidth = textPaint.measureText(customText);
    66. float x = rectF.centerX() - textWidth / 2;
    67. float y = rectF.centerY() - (textPaint.descent() + textPaint.ascent()) / 2;
    68. canvas.drawText(customText, x, y, textPaint);
    69. }
    70. private float dpToPx(float dp) {
    71. return dp * getResources().getDisplayMetrics().density;
    72. }
    73. }

    在res/values/attrs.xml文件中定义自定义属性:

    1. <resources>
    2. <declare-styleable name="CustomView">
    3. <attr name="customText" format="string" />
    4. <attr name="customTextColor" format="color" />
    5. </declare-styleable>
    6. </resources>

    引用

    1. <com.fawersmart.engineer.View.RectangleWithTriangleView
    2. android:layout_width="36dp"
    3. android:layout_marginLeft="2dp"
    4. app:customText="text!"
    5. android:layout_height="18dp"/>

  • 相关阅读:
    想要精通算法和SQL的成长之路 - 超过经理收入的员工(SQL)
    vite+vue3+ts解决跨域问题
    每天一道算法题:46. 全排列
    Abnova丨ACTN4 DNA 探针解决方案
    驱动开发:内核远程堆分配与销毁
    迷茫内耗的一天
    简单描述标准生成树协议STP
    有线电视模拟信号基础知识
    python实现概率密度匹配法
    家政服务接单小程序开发源码 家政保洁上门服务小程序源码 开源完整版
  • 原文地址:https://blog.csdn.net/u012372365/article/details/133036221