-
Notifications
You must be signed in to change notification settings - Fork 31
/
MessageBox.java
94 lines (68 loc) · 2.47 KB
/
MessageBox.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
package com.example.customviewdemoapplication.Views;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;
import androidx.annotation.Nullable;
import com.example.customviewdemoapplication.R;
public class MessageBox extends View {
Paint paint;
Path path;
Paint textPaint;
int width;
int height;
String text;
float strokeWidth;
int textColor;
int strokeColor;
public MessageBox(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.MessageBox);
text = typedArray.getString(R.styleable.MessageBox_text);
strokeWidth = typedArray.getFloat(R.styleable.MessageBox_android_strokeWidth, 5f);
textColor = typedArray.getColor(R.styleable.MessageBox_textColor, Color.GREEN);
strokeColor = typedArray.getColor(R.styleable.MessageBox_android_strokeColor, Color.RED);
typedArray.recycle();
init(attrs);
}
private void init(AttributeSet attrs){
paint = new Paint();
paint.setColor(strokeColor);
paint.setStrokeWidth(strokeWidth);
paint.setStyle(Paint.Style.STROKE);
textPaint = new Paint();
textPaint.setColor(textColor);
textPaint.setTextSize(40);
textPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = getMeasuredWidth();
height = getMinimumHeight();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.translate(20f, 50f);
path = new Path();
int w = width-200;
int h = 100;
path.addArc(0f, 0f, 0+100, 0+100, 180, 90);
path.lineTo(w, 0);
path.addArc(w-50, 0f, w+50, 100, 270, 90);
path.lineTo(w+50, h);
path.addArc(w+50, h-50, w+50+50, 50+h, 90, 90);
path.moveTo(w+70, h+50);
path.lineTo(50, h+50);
path.addArc(0, h-50, 100, h+50, 90, 90);
path.lineTo(0, 50);
canvas.drawPath(path, paint);
canvas.drawText(text, 50, 50, textPaint);
}
}