import
android.content.Context;
import
android.graphics.Canvas;
import
android.graphics.Paint;
import
android.graphics.Path;
import
android.graphics.Point;
import
android.graphics.RectF;
import
android.util.AttributeSet;
import
android.view.MotionEvent;
import
android.view.View;
public
class
MyView
extends
View {
Paint m_Paint;
Path m_Path;
Canvas m_Canvas;
enum
Mode {
TRIANGLE,
STAR,
HEART
}
private
Mode mMode;
private
int
mViewWidth =
0
;
private
int
mViewHeight =
0
;
public
MyView(Context context) {
super
(context);
initMyView();
}
public
MyView(Context context, AttributeSet attrs) {
super
(context, attrs);
initMyView();
}
public
MyView(Context context, AttributeSet attrs,
int
defStyleAttr) {
super
(context, attrs, defStyleAttr);
initMyView();
}
public
void
initMyView(){
m_Paint =
new
Paint();
m_Paint.setAntiAlias(
true
);
m_Paint.setDither(
true
);
m_Paint.setColor(
0xFFFF0000
);
m_Paint.setStyle(Paint.Style.STROKE);
m_Paint.setStrokeJoin(Paint.Join.ROUND);
m_Paint.setStrokeCap(Paint.Cap.ROUND);
m_Paint.setStrokeWidth(
4
);
m_Path =
new
Path();
}
@Override
protected
void
onMeasure(
int
widthMeasureSpec,
int
heightMeasureSpec) {
mViewWidth = MeasureSpec.getSize(widthMeasureSpec);
mViewHeight = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(mViewWidth, mViewHeight);
}
@Override
protected
void
onDraw(Canvas canvas) {
super
.onDraw(canvas);
m_Canvas = canvas;
m_Canvas.drawColor(
0xFFFFFFFF
);
if
(mMode == Mode.HEART) {
float
x = mViewWidth/
2
;
float
y = mViewHeight/
2
;
m_Canvas.rotate(
45
,x,y);
}
m_Canvas.drawPath(m_Path, m_Paint);
}
public
void
drawTriangle() {
float
x = mViewWidth/
2
;
float
y = mViewHeight/
2
;
drawTriangle(x, y);
}
public
void
drawTriangle(
float
x,
float
y) {
mMode = Mode.TRIANGLE;
int
side =
200
;
/** 삼각형 크기 */
int
height =
300
;
y = y - height;
m_Path.reset();
Point point1_draw =
new
Point((
int
)x, (
int
)y);
Point point2_draw =
new
Point((
int
)x-side, (
int
)y+height);
Point point3_draw =
new
Point((
int
)x+side, (
int
)y+height);
m_Path.moveTo(point1_draw.x, point1_draw.y);
m_Path.lineTo(point2_draw.x, point2_draw.y);
m_Path.lineTo(point3_draw.x, point3_draw.y);
m_Path.lineTo(point1_draw.x, point1_draw.y);
m_Path.close();
invalidate();
}
public
void
drawStar() {
float
x = mViewWidth/
2
;
float
y = mViewHeight/
2
;
drawStar(x, y);
}
public
void
drawStar(
float
x,
float
y) {
mMode = Mode.STAR;
int
radius =
200
;
/** 별 크기 */
y = y - radius;
int
numOfPt =
5
;
double
section =
2.0
* Math.PI/numOfPt;
int
innerRadius = (
int
) (radius /
2.5
);
m_Path.reset();
m_Path.moveTo(
(
float
)(x + radius * Math.cos(
0
)),
(
float
)(y + radius * Math.sin(
0
)));
m_Path.lineTo(
(
float
)(x + innerRadius * Math.cos(
0
+ section/
2.0
)),
(
float
)(y + innerRadius * Math.sin(
0
+ section/
2.0
)));
for
(
int
i=
1
; i < numOfPt; i++){
m_Path.lineTo(
(
float
)(x + radius * Math.cos(section * i)),
(
float
)(y + radius * Math.sin(section * i)));
m_Path.lineTo(
(
float
)(x + innerRadius * Math.cos(section * i + section/
2.0
)),
(
float
)(y + innerRadius * Math.sin(section * i + section/
2.0
)));
}
m_Path.close();
invalidate();
}
public
void
drawHeart() {
float
x = mViewWidth/
2
;
float
y = mViewHeight/
2
;
drawHeart(x, y);
}
public
void
drawHeart(
float
x,
float
y) {
mMode = Mode.HEART;
float
length =
200
;
m_Path.reset();
m_Path.moveTo(x,y);
m_Path.lineTo(x-length, y);
m_Path.arcTo(
new
RectF(x-length-(length/
2
),y-length,x-(length/
2
),y),
90
,
180
);
m_Path.arcTo(
new
RectF(x-length,y-length-(length/
2
),x,y-(length/
2
)),
180
,
180
);
m_Path.lineTo(x,y);
m_Path.close();
invalidate();
}
@Override
public
boolean
onTouchEvent(MotionEvent event) {
float
x = event.getX();
float
y = event.getY();
switch
(event.getAction()) {
case
MotionEvent.ACTION_DOWN:
touchDraw(x, y);
break
;
case
MotionEvent.ACTION_MOVE:
touchDraw(x, y);
break
;
case
MotionEvent.ACTION_UP:
touchDraw(x, y);
break
;
}
return
true
;
}
private
void
touchDraw(
float
x,
float
y) {
if
(mMode == Mode.TRIANGLE) {
drawTriangle(x, y);
}
else
if
(mMode == Mode.STAR) {
drawStar(x, y);
}
else
if
(mMode == Mode.HEART) {
drawHeart(x, y);
}
}
}