在开发过程中,我们可能会经常遇到这样的需求样式:

在这里插入图片描述

这张图是截取京东消息通知的弹出框,我们可以看到右上方有个三角形的气泡效果,这只是其中一种,三角形的方向还可以是上、下、左、右。

通过截图可以发现,气泡由正三角形和圆角长方形组成,于是可以通过组合来形成三角形气泡的效果,下面我们通过三种方式进行实现。

实现方式:
1、通过.9图进行实现;
2、通过shape方式实现;
3、通过自定义view的方式实现;

实现逻辑:

1、通过.9图进行实现

这种方式就不用说了吧,找你们UI小姐姐切一个.9图,使用即可,不过这种方式的图片需要占一定体积哦。

2、通过shape方式实现

  • 正三角形
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item>
  4. <rotate
  5. android:fromDegrees="45"
  6. android:pivotX="-40%"
  7. android:pivotY="80%">
  8. <shape android:shape="rectangle">
  9. <size
  10. android:width="15dp"
  11. android:height="15dp" />
  12. <solid android:color="#ffffff" />
  13. </shape>
  14. </rotate>
  15. </item>
  16. </layer-list>
  • 倒三角形
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item>
  4. <rotate
  5. android:fromDegrees="45"
  6. android:pivotX="135%"
  7. android:pivotY="15%">
  8. <shape android:shape="rectangle">
  9. <size
  10. android:width="15dp"
  11. android:height="15dp" />
  12. <solid android:color="#ffffff" />
  13. </shape>
  14. </rotate>
  15. </item>
  16. </layer-list>
  • 左三角形
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item>
  4. <rotate
  5. android:fromDegrees="-45"
  6. android:pivotX="85%"
  7. android:pivotY="-35%">>
  8. <shape android:shape="rectangle">
  9. <size
  10. android:width="15dp"
  11. android:height="15dp" />
  12. <solid android:color="#ffffff" />
  13. </shape>
  14. </rotate>
  15. </item>
  16. </layer-list>
  • 右三角形
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item>
  4. <rotate
  5. android:fromDegrees="-45"
  6. android:pivotX="15%"
  7. android:pivotY="135%">>
  8. <shape android:shape="rectangle">
  9. <size
  10. android:width="15dp"
  11. android:height="15dp" />
  12. <solid android:color="#ffffff" />
  13. </shape>
  14. </rotate>
  15. </item>
  16. </layer-list>

上面就是通过shape方式实现各个方向的代码,这种方式缺点比较明显,如果要变化不同的角的位置需要再写不同的布局。

3、通过自定义view的方式实现

由于是比较简单这里就不讲解每个怎么搞了,可以复制过去直接用

  • 添加自定义属性
  1. <declare-styleable name="TriangleView">
  2. <attr name="trv_color" format="color" />
  3. <attr name="trv_direction">
  4. <enum name="top" value="0" />
  5. <enum name="bottom" value="1" />
  6. <enum name="right" value="2" />
  7. <enum name="left" value="3" />
  8. </attr>
  9. </declare-styleable>
  • 自定义代码文件
  1. public class TriangleView extends View {
  2. private static final int TOP = 0;
  3. private static final int BOTTOM = 1;
  4. private static final int RIGHT = 2;
  5. private static final int LEFT = 3;
  6. private static final int DEFUALT_WIDTH = 10;
  7. private static final int DEFUALT_HEIGHT = 6;
  8. private static final int DEFUALT_COLOR = R.color.FFF;
  9. private Paint mPaint;
  10. private int mColor;
  11. private int mWidth;
  12. private int mHeight;
  13. private int mDirection;
  14. private Path mPath;
  15. public TriangleView(final Context context) {
  16. this(context, null);
  17. }
  18. public TriangleView(Context context, @Nullable AttributeSet attrs) {
  19. this(context, attrs, 0);
  20. }
  21. public TriangleView(final Context context, final AttributeSet attrs, final int defStyleAttr) {
  22. super(context, attrs, defStyleAttr);
  23. init();
  24. TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.TriangleView, 0, 0);
  25. mColor = typedArray.getColor(R.styleable.TriangleView_trv_color, ContextCompat.getColor(getContext(), DEFUALT_COLOR));
  26. mDirection = typedArray.getInt(R.styleable.TriangleView_trv_direction, mDirection);
  27. typedArray.recycle();
  28. mPaint.setColor(mColor);
  29. }
  30. private void init() {
  31. mPaint = new Paint();
  32. mPaint.setAntiAlias(true);
  33. mPaint.setStyle(Paint.Style.FILL);
  34. mPath = new Path();
  35. mDirection = TOP;
  36. }
  37. @Override
  38. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  39. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  40. mWidth = MeasureSpec.getSize(widthMeasureSpec);
  41. mHeight = MeasureSpec.getSize(heightMeasureSpec);
  42. final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
  43. final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
  44. if (mWidth == 0 || widthMode != MeasureSpec.EXACTLY) {
  45. mWidth = (int) PixelUtil.dp2px(DEFUALT_WIDTH);
  46. }
  47. if (mHeight == 0 || heightMode != MeasureSpec.EXACTLY) {
  48. mHeight = (int) PixelUtil.dp2px(DEFUALT_HEIGHT);
  49. }
  50. setMeasuredDimension(mWidth, mHeight);
  51. }
  52. @Override
  53. protected void onDraw(Canvas canvas) {
  54. super.onDraw(canvas);
  55. switch (mDirection) {
  56. case TOP:
  57. mPath.moveTo(0, mHeight);
  58. mPath.lineTo(mWidth, mHeight);
  59. mPath.lineTo(mWidth / 2, 0);
  60. break;
  61. case BOTTOM:
  62. mPath.moveTo(0, 0);
  63. mPath.lineTo(mWidth / 2, mHeight);
  64. mPath.lineTo(mWidth, 0);
  65. break;
  66. case RIGHT:
  67. mPath.moveTo(0, 0);
  68. mPath.lineTo(0, mHeight);
  69. mPath.lineTo(mWidth, mHeight / 2);
  70. break;
  71. case LEFT:
  72. mPath.moveTo(0, mHeight / 2);
  73. mPath.lineTo(mWidth, mHeight);
  74. mPath.lineTo(mWidth, 0);
  75. break;
  76. default:
  77. break;
  78. }
  79. mPath.close();
  80. canvas.drawPath(mPath, mPaint);
  81. }
  82. }
  • 布局文件添加
  1. <com.sjl.keeplive.triange.TriangleView
  2. android:layout_width="10dp"
  3. android:layout_height="6dp"
  4. app:trv_color="@color/FFF"
  5. app:trv_direction="top" />

通过自定义的方式可以搞定四个方向,而且在代码中也可以使用,动态添加,动态改变颜色,还是比较好的方式。

到这里就完成啦.

版权声明:本文为showly原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/showly/p/14507138.html