-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathMyView.java
162 lines (113 loc) · 4.51 KB
/
MyView.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package com.example.customviewdemoapplication.Views;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.RectF;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import android.view.MotionEvent;
import android.view.View;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.ViewTreeObserver;
import com.example.customviewdemoapplication.R;
public class MyView extends View {
public static final int SQUARE_SIZE = 350;
private Rect mRectSquare;
private Paint mPaintSquare;
private int mSquareColor;
private int mSquareSize;
private Paint mPaintCircle;
private float mCircleX, mCircleY;
private float mCircleRadius = 100f;
private Bitmap mImage;
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
private void init(@Nullable AttributeSet set){
mRectSquare = new Rect();
mPaintSquare = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaintCircle = new Paint();
mPaintCircle.setAntiAlias(true);
mPaintCircle.setColor(Color.parseColor("#00ccff"));
mImage = BitmapFactory.decodeResource(getResources(), R.drawable.demo);
getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
getViewTreeObserver().removeOnGlobalLayoutListener(this);
mImage = getResizedBitmap(mImage, getWidth(), getHeight());
}
});
if(set == null){
return;
}
TypedArray typedArray = getContext().obtainStyledAttributes(set, R.styleable.MyView);
mSquareColor = typedArray.getColor(R.styleable.MyView_square_color, Color.GREEN);
mSquareSize = typedArray.getDimensionPixelSize(R.styleable.MyView_square_size, SQUARE_SIZE);
mPaintSquare.setColor(mSquareColor);
typedArray.recycle();
}
public void swapColor(){
mPaintSquare.setColor(mPaintSquare.getColor() == mSquareColor ? Color.RED : mSquareColor);
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
mRectSquare.left = 50;
mRectSquare.top = 50;
mRectSquare.bottom = mRectSquare.top + mSquareSize;
mRectSquare.right = mRectSquare.left + mSquareSize;
canvas.drawRect(mRectSquare, mPaintSquare);
if(mCircleX == 0f || mCircleY == 0f){
mCircleX = getWidth() / 2;
mCircleY = getHeight() / 2;
}
canvas.drawCircle(mCircleX, mCircleY, mCircleRadius, mPaintCircle);
//canvas.drawBitmap(mImage, 0, 0, null);
Paint paint = new Paint();
paint.setColor(Color.MAGENTA);
paint.setTextSize(100f);
canvas.drawText("Hello World", getWidth()/2, getHeight()/2, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
boolean value = super.onTouchEvent(event);
float x = event.getX();
float y = event.getY();
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
if(mRectSquare.left < x && mRectSquare.right > x){
if(mRectSquare.top < y && mRectSquare.bottom > y){
mCircleRadius += 10f;
postInvalidate();
}
}
return true;
case MotionEvent.ACTION_MOVE:
double dx = Math.pow(x - mCircleX, 2);
double dy = Math.pow(y - mCircleY, 2);
if(dy + dx < Math.pow(mCircleRadius, 2)){
mCircleX = x;
mCircleY = y;
postInvalidate();
return true;
}
return value;
}
return value;
}
private Bitmap getResizedBitmap(Bitmap bitmap, int reqWidth, int reqHeight){
Matrix matrix = new Matrix();
RectF src = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());
RectF dst = new RectF(0, 0, reqWidth, reqHeight);
matrix.setRectToRect(src, dst, Matrix.ScaleToFit.CENTER);
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}
}