• 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"/>

  • 相关阅读:
    PAT 乙级1090危险品装箱
    直播回放含 PPT 下载|基于 Flink & DeepRec 构建 Online Deep Learning
    【每日小bug】微信开发者工具,打开项目后捕捉不到请求也发送不出请求并提示请求失败问题
    (二)springcloud实战之config配置中心
    C++零碎记录(十)
    【计算机视觉 | 目标检测】arxiv 计算机视觉关于目标检测的学术速递(9 月 13 日论文合集)
    C 语言学习笔记(三):C 语言开发环境搭建
    java spring cloud 企业工程管理系统源码+二次开发+定制化服务
    使用adb shell 命令接收串口发送过来的16进制数据 或者 发送16进制数据
    PMI-ACP(103:57- 103)
  • 原文地址:https://blog.csdn.net/u012372365/article/details/133036221