package
com.example.test01;
import
android.content.Context;
import
android.graphics.Bitmap;
import
android.graphics.BitmapFactory;
import
android.graphics.Canvas;
import
android.graphics.Color;
import
android.graphics.Matrix;
import
android.graphics.Paint;
import
android.graphics.Paint.Style;
import
android.graphics.Rect;
import
android.os.Handler;
import
android.view.MotionEvent;
import
android.view.View;
import
android.widget.Toast;
public
class
MyView
extends
View{
Bitmap Img1,Img2,Img3,Img4,Img5,Img6,Img7,Img8,Img9;
int
width=MainActivity.screenWidth;
int
height=MainActivity.screenHeight;
int
rectL;
Rect rect1;
Rect rect2;
Rect rect3;
Rect rect4;
Rect rect5;
Rect rect6;
Rect rect7;
Rect rect8;
Rect rect9;
Paint rectPaint ;
Context context;
int
X,Y;
int
count[][]={{
0
,
0
,
0
},{
0
,
0
,
0
},{
0
,
0
,
0
}};
int
answer[][]={{
1
,
1
,
1
},{
1
,
1
,
1
},{
1
,
1
,
1
}};
int
x1=(width/
4
);
int
x2=(width/
4
)*
2
;
int
x3=(width/
4
)*
3
;
int
y1=(height/
4
);
int
y2=(height/
4
)*
2
;
int
y3=(height/
4
)*
3
;
public
MyView(Context context) {
super
(context);
this
.context=context;
init();
handler.sendEmptyMessage(
0
);
}
public
void
init(){
Img1=BitmapFactory.decodeResource
(getResources(), R.drawable.img1);
Img2=BitmapFactory.decodeResource
(getResources(), R.drawable.img2);
Img3=BitmapFactory.decodeResource
(getResources(), R.drawable.img3);
Img4=BitmapFactory.decodeResource
(getResources(), R.drawable.img4);
Img5=BitmapFactory.decodeResource
(getResources(), R.drawable.img5);
Img6=BitmapFactory.decodeResource
(getResources(), R.drawable.img6);
Img7=BitmapFactory.decodeResource
(getResources(), R.drawable.img7);
Img8=BitmapFactory.decodeResource
(getResources(), R.drawable.img8);
Img9=BitmapFactory.decodeResource
(getResources(), R.drawable.img9);
rectPaint=
new
Paint();
rectPaint.setAlpha(
255
);
rectPaint.setColor(Color.GREEN);
rectPaint.setStyle(Style.FILL);
}
@Override
protected
void
onDraw(Canvas canvas){
canvas.drawBitmap(Img1,x1,y1,
null
);
canvas.drawBitmap(Img2,x2,y1,
null
);
canvas.drawBitmap(Img3,x3,y1,
null
);
canvas.drawBitmap(Img4,x1,y2,
null
);
canvas.drawBitmap(Img5,x2,y2,
null
);
canvas.drawBitmap(Img6,x3,y2,
null
);
canvas.drawBitmap(Img7,x1,y3,
null
);
canvas.drawBitmap(Img8,x2,y3,
null
);
canvas.drawBitmap(Img9,x3,y3,
null
);
rect1=
new
Rect(x1,y1,x1+
200
, y1+
200
);
canvas.drawRect(rect1, rectPaint);
rect2=
new
Rect(x2,y1,x2+
200
,y1+
200
);
canvas.drawRect(rect2, rectPaint);
rect3=
new
Rect(x3,y1,x3+
200
,y1+
200
);
canvas.drawRect(rect3, rectPaint);
rect4=
new
Rect(x1,y2,x1+
200
, y2+
200
);
canvas.drawRect(rect4, rectPaint);
rect5=
new
Rect(x2,y2,x2+
200
,y2+
200
);
canvas.drawRect(rect5, rectPaint);
rect6=
new
Rect(x3,y2,x3+
200
,y2+
200
);
canvas.drawRect(rect6, rectPaint);
rect7=
new
Rect(x1,y3,x1+
200
,y3+
200
);
canvas.drawRect(rect7, rectPaint);
rect8=
new
Rect(x2,y3,x2+
200
,y3+
200
);
canvas.drawRect(rect8, rectPaint);
rect9=
new
Rect(x3,y3,x3+
200
,y3+
200
);
canvas.drawRect(rect9, rectPaint);
}
@Override
public
boolean
onTouchEvent(MotionEvent event) {
if
(event.getAction() == MotionEvent.ACTION_DOWN) {
int
x = (
int
) event.getX();
int
y = (
int
) event.getY();
if
(rect1.contains(x, y) ==
true
){
Count(
0
,
0
);
Img1=imgRotate(Img1);
Toast.makeText(getContext(),
""
+count[
0
][
0
], Toast.LENGTH_SHORT).show();
Answer();
}
else
if
(rect2.contains(x, y) ==
true
){
Count(
0
,
1
);
Img2=imgRotate(Img2);
Toast.makeText(getContext(),
""
+count[
0
][
1
], Toast.LENGTH_SHORT).show();
Answer();
}
else
if
(rect3.contains(x, y) ==
true
){
Count(
0
,
2
);
Img3=imgRotate(Img3);
Toast.makeText(getContext(),
""
+count[
0
][
2
], Toast.LENGTH_SHORT).show();
Answer();
}
else
if
(rect4.contains(x, y) ==
true
){
Count(
1
,
0
);
Img4=imgRotate(Img4);
Toast.makeText(getContext(),
""
+count[
1
][
0
], Toast.LENGTH_SHORT).show();
Answer();
}
else
if
(rect5.contains(x, y) ==
true
){
Count(
1
,
1
);
Img5=imgRotate(Img5);
Toast.makeText(getContext(),
""
+count[
1
][
1
], Toast.LENGTH_SHORT).show();
Answer();
}
else
if
(rect6.contains(x, y) ==
true
){
Count(
1
,
2
);
Img6=imgRotate(Img6);
Toast.makeText(getContext(),
""
+count[
1
][
2
], Toast.LENGTH_SHORT).show();
Answer();
}
else
if
(rect7.contains(x, y) ==
true
){
Count(
2
,
0
);
Img7=imgRotate(Img7);
Toast.makeText(getContext(),
""
+count[
2
][
0
], Toast.LENGTH_SHORT).show();
Answer();
}
else
if
(rect8.contains(x, y) ==
true
){
Count(
2
,
1
);
Img8=imgRotate(Img8);
Toast.makeText(getContext(),
""
+count[
2
][
1
], Toast.LENGTH_SHORT).show();
Answer();
}
else
if
(rect9.contains(x, y) ==
true
){
Count(
2
,
2
);
Img9=imgRotate(Img9);
Toast.makeText(getContext(),
""
+count[
2
][
2
], Toast.LENGTH_SHORT).show();
Answer();
}
else
Toast.makeText(getContext(),
"Miss"
, Toast.LENGTH_SHORT).show();
}
return
false
;
}
private
Bitmap imgRotate(Bitmap bmp){
int
width = bmp.getWidth();
int
height = bmp.getHeight();
Matrix matrix =
new
Matrix();
matrix.postRotate(
90
);
Bitmap resizedBitmap = Bitmap.createBitmap(bmp,
0
,
0
, width, height, matrix,
true
);
bmp.recycle();
return
resizedBitmap;
}
private
void
Count(
int
n1,
int
n2){
if
(count[n1][n2]<
3
)
count[n1][n2]++;
else
if
(count[n1][n2]==
3
)
count[n1][n2]=
0
;
}
private
void
Answer(){
if
((count[
0
][
0
]==answer[
0
][
0
] && count[
1
][
0
]==answer[
1
][
0
] && count[
2
][
0
]==answer[
2
][
0
]
&& count[
2
][
1
]==answer[
2
][
1
] && count[
2
][
2
]==answer[
2
][
2
])==
true
)
Toast.makeText(getContext(),
"CLEAR!!"
, Toast.LENGTH_LONG).show();
}
Handler handler=
new
Handler(){
public
void
handleMessage(android.os.Message msg) {
invalidate();
handler.sendEmptyMessageDelayed(
0
,
10
);
}
};
}