-
Notifications
You must be signed in to change notification settings - Fork 241
/
ReplyBgView.java
136 lines (117 loc) · 5.76 KB
/
ReplyBgView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package com.d.lib.ui.view.replybg;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
import com.d.lib.common.util.DimenUtils;
import com.d.lib.ui.view.R;
/**
* Message Background
* Created by D on 2017/3/3.
*/
public class ReplyBgView extends View {
private final int LEFT = 0;
private final int RIGHT = 1;
private final int TOP = 2;
private final int BOTTOM = 3;
private int mWidth;
private int mHeight;
private Rect mRect;
private RectF mRectF;
private Paint mPaint;
private Path mPathTrg; // Triangular path
private int mGravity;
private int mColorBg;
private float mRectRadius;
private float mOffset; // Triangle left offset
private float mTrgHalfWidth; // Triangle bottom length/2
private float mTrgHeight; // Triangle height
public ReplyBgView(Context context) {
this(context, null);
}
public ReplyBgView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public ReplyBgView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initAttrs(context, attrs);
init(context);
}
private void initAttrs(Context context, AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.lib_ui_view_ReplyBgView);
mGravity = typedArray.getInteger(R.styleable.lib_ui_view_ReplyBgView_lib_ui_view_replybv_gravity, TOP);
mColorBg = typedArray.getColor(R.styleable.lib_ui_view_ReplyBgView_lib_ui_view_replybv_color, ContextCompat.getColor(context, R.color.lib_pub_color_main));
mRectRadius = typedArray.getDimension(R.styleable.lib_ui_view_ReplyBgView_lib_ui_view_replybv_radius, DimenUtils.dp2px(context, 3));
mOffset = typedArray.getDimension(R.styleable.lib_ui_view_ReplyBgView_lib_ui_view_replybv_offset, DimenUtils.dp2px(context, 6.5f));
mTrgHalfWidth = typedArray.getDimension(R.styleable.lib_ui_view_ReplyBgView_lib_ui_view_replybv_trgWidth, DimenUtils.dp2px(context, 6)) / 2;
mTrgHeight = typedArray.getDimension(R.styleable.lib_ui_view_ReplyBgView_lib_ui_view_replybv_trgHeight, DimenUtils.dp2px(context, 5));
typedArray.recycle();
}
private void init(@SuppressWarnings("unused") Context context) {
mPathTrg = new Path();
mRect = new Rect();
mRectF = new RectF();
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(mColorBg);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
switch (mGravity) {
case LEFT:
mPathTrg.moveTo(mTrgHeight, mOffset > 0 ? mOffset : mHeight + mOffset - mTrgHalfWidth * 2);
mPathTrg.lineTo(0, mOffset > 0 ? mOffset + mTrgHalfWidth : mHeight + mOffset - mTrgHalfWidth);
mPathTrg.lineTo(mTrgHeight, mOffset > 0 ? mOffset + mTrgHalfWidth * 2 : mHeight + mOffset);
mPathTrg.close();
canvas.drawPath(mPathTrg, mPaint);
mRect.set((int) mTrgHeight, 0, mWidth, mHeight);
mRectF.set(mRect);
canvas.drawRoundRect(mRectF, mRectRadius, mRectRadius, mPaint);
break;
case RIGHT:
mPathTrg.moveTo(mWidth - mTrgHeight, mOffset > 0 ? mOffset : mHeight + mOffset - mTrgHalfWidth * 2);
mPathTrg.lineTo(mWidth, mOffset > 0 ? mOffset + mTrgHalfWidth : mHeight + mOffset - mTrgHalfWidth);
mPathTrg.lineTo(mWidth - mTrgHeight, mOffset > 0 ? mOffset + mTrgHalfWidth * 2 : mHeight + mOffset);
mPathTrg.close();
canvas.drawPath(mPathTrg, mPaint);
mRect.set(0, 0, (int) (mWidth - mTrgHeight), mHeight);
mRectF.set(mRect);
canvas.drawRoundRect(mRectF, mRectRadius, mRectRadius, mPaint);
break;
case TOP:
mPathTrg.moveTo(mOffset > 0 ? mOffset : mWidth + mOffset - mTrgHalfWidth * 2, mTrgHeight);
mPathTrg.lineTo(mOffset > 0 ? mOffset + mTrgHalfWidth : mWidth + mOffset - mTrgHalfWidth, 0);
mPathTrg.lineTo(mOffset > 0 ? mOffset + mTrgHalfWidth * 2 : mWidth + mOffset, mTrgHeight);
mPathTrg.close();
canvas.drawPath(mPathTrg, mPaint);
mRect.set(0, (int) mTrgHeight, mWidth, mHeight);
mRectF.set(mRect);
canvas.drawRoundRect(mRectF, mRectRadius, mRectRadius, mPaint);
break;
case BOTTOM:
mPathTrg.moveTo(mOffset > 0 ? mOffset : mWidth + mOffset - mTrgHalfWidth * 2, mHeight - mTrgHeight);
mPathTrg.lineTo(mOffset > 0 ? mOffset + mTrgHalfWidth : mWidth + mOffset - mTrgHalfWidth, mHeight);
mPathTrg.lineTo(mOffset > 0 ? mOffset + mTrgHalfWidth * 2 : mWidth + mOffset, mHeight - mTrgHeight);
mPathTrg.close();
canvas.drawPath(mPathTrg, mPaint);
mRect.set(0, 0, mWidth, (int) (mHeight - mTrgHeight));
mRectF.set(mRect);
canvas.drawRoundRect(mRectF, mRectRadius, mRectRadius, mPaint);
break;
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mWidth = MeasureSpec.getSize(widthMeasureSpec);
mHeight = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(mWidth, mHeight);
}
}