온드로우를 쓰고 캔버스에 비트맵을 그려서 터치이벤트를 하는데.
xml은 건드릴게 없구요 자바로만 프로그래밍하는데
bringToFront(); 를 써야 이미지 우선순위대로 겹쳐져도 이미지가 상위에 있게된다는데..
어느쪽에 써야할지 감이 안잡힙니다..
이게 소스입니다...
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// MyView mv = new MyView(this)
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(new MyView(this));
}
}
class MyView extends View {
private float X; //최초 x좌표
private float X1 = 150; //최초 x좌표
private float X2 = 300;
private float Y,Y1,Y2; //최초 y좌표
private float prevX = -1; //지난터치 x좌표
private float prevY = -1; //지난터치 y좌표
private float prevX1 = 149;
private int rw,rw1,rw2 = 0; //비트맵이미지 가로
private int rh,rh1,rh2 = 0; //비트맵이미지 세로
private int width; //디스플레이 가로
private int height; //디스플레이 세로
private Rect rect, rect1, rect2;
private static boolean Flag1 = false; //static 플래그선언
private static boolean Flag2 = false;
private static boolean Flag3 = false;
private static boolean Flag4 = true;
private static boolean Flag5 = false;
private static boolean Flag6 = false;
private static boolean Flag7 = false;
private static boolean Flag8 = false;
private static boolean Flag9 = true; //테두리 넣기 플래그
Bitmap backBmp, backBmp1, backBmp2;
Paint pnt = new Paint();
public MyView(Context context) {
super(context);
setBackgroundColor(Color.WHITE);
backBmp = BitmapFactory.decodeResource(getResources(),
R.drawable.cat1); //비트맵 이미지 불러들이기
backBmp1 = BitmapFactory.decodeResource(getResources(),
R.drawable.cat2); //비트맵 이미지 불러들이기
backBmp2 = BitmapFactory.decodeResource(getResources(),
R.drawable.cat3); //비트맵 이미지 불러들이기
pnt.setStyle(Paint.Style.STROKE); //사각형테두리
pnt.setColor(Color.YELLOW);
Display display = ((WindowManager) context.getSystemService(
Context.WINDOW_SERVICE)).getDefaultDisplay();
width = display.getWidth();
height = display.getHeight();
}
@Override
protected void onDraw(Canvas canvas) {
// canvas.drawColor(Color.WHITE);
canvas.drawBitmap(backBmp, this.X, this.Y, null);
canvas.drawBitmap(backBmp1, this.X1, this.Y1, null);
canvas.drawBitmap(backBmp2, this.X2, this.Y2, null);
rw = backBmp.getWidth() ;
rh = backBmp.getHeight() ;
rw1 = backBmp1.getWidth() ;
rh1 = backBmp1.getHeight() ;
rw2 = backBmp2.getWidth() ;
rh2 = backBmp2.getHeight() ;
pnt.setStrokeWidth(3); //테두리 두께
rect = new Rect(15, 400, 140, 780); //사각형 영역
rect1 = new Rect(140, 400, 265, 780); //사각형 영역
rect2 = new Rect(265, 400, 390, 780); //사각형 영역
canvas.drawRect(rect, pnt); // 테두리 사각형 그림
canvas.drawRect(rect1, pnt); // 테두리 사각형 그림
canvas.drawRect(rect2, pnt); // 테두리 사각형 그림
super.onDraw(canvas);
}
public boolean onTouchEvent(MotionEvent event) {
float X = event.getX();
float Y = event.getY();
System.out.println("move x 좌표 : " + event.getX());
System.out.println("move Y 좌표 : " + event.getY());
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//터치 좌표가 이미지 안에 있는지 thisX는 그림의 X좌표 X좌표는 현재 찍은 좌표
if (X > this.X && X < this.X + rw && Y > this.Y
&& Y < this.Y + rh) {
Flag1 = true;
Log.d("TAG", "Flag = true");
}
if(this.X == 15 || this.Y == 400)
{
Flag9 = true;
}
if (X > this.X1 && X < this.X1 + rw && Y > this.Y1
&& Y < this.Y1 + rh) {
Log.d("TAG", "Flag2 = true");
Flag2 = true;
}
if (X > this.X2 && X < this.X2 + rw && Y > this.Y2
&& Y < this.Y2 + rh) {
Log.d("TAG", "Flag2 = true");
Flag3 = true;
}
break;
case MotionEvent.ACTION_MOVE:
Log.d("TAG", "Flag = " + Flag1);
Log.d("TAG", "Flag = " + Flag2);
Log.d("TAG", "this.x = " + this.X);
Log.d("TAG", "this.y = " + this.Y);
// 드래그 된 만큼 이미지의 좌표도 이동시킨다.
if(Flag1 == true) {
Log.d("TAG", "prev.x = " + prevX);
Log.d("TAG", "prev.y = " + prevY);
if (prevX > 0 && prevY > 0) {
this.X += (X - prevX);
this.Y += (Y - prevY);
}
prevX = X;
prevY = Y;
}
if(Flag2 == true) {
Log.d("TAG", "prev.x = " + prevX);
Log.d("TAG", "prev.y = " + prevY);
if (prevX > 0 && prevY > 0) {
this.X1 += (X - prevX);
this.Y1 += (Y - prevY);
}
prevX = X;
prevY = Y;
}
if(Flag3 == true) {
Log.d("TAG", "prev.x = " + prevX);
Log.d("TAG", "prev.y = " + prevY);
if (prevX > 0 && prevY > 0) {
this.X2 += (X - prevX);
this.Y2 += (Y - prevY);
}
prevX = X;
prevY = Y;
}
//화면 테두리 넘어감 방지
if(this.X+rw > width )
{
this.X = width-rw;
Flag1 = false;
}
if(this.X < 0)
{
this.X = 0;
Flag1 = false;
}
if(this.Y+rh > height)
{
this.Y = height-rh;
Flag1 = false;
}
if(this.Y < 0 )
{
this.Y = 0;
Flag1 = false;
}
//테두리 안에 넣기
if(this.X > 0 && this.X < 50)
if(this.Y > 350 && this.Y < 450)
{
this.X = 15;
this.Y = 400;
Flag9 = false;
/*if(Flag5 == false)
{
Toast.makeText(getContext(), "맞았다1.", Toast.LENGTH_SHORT).show();
}
Flag5 = true; */
}
//테두리 밖으로 나갔을때
if(this.X != 15 || this.Y != 400)
{
Flag5 = false;
}
// 두번째 이미지 화면 넘김방지
if(this.X1+rw > width )
{
this.X1 = width-rw;
Flag2 = false;
}
if(this.X1 < 0)
{
this.X1 = 0;
Flag2 = false;
}
if(this.Y1+rh > height)
{
this.Y1 = height-rh;
Flag2 = false;
}
if(this.Y1 < 0 )
{
this.Y1 = 0;
Flag2 = false;
}
//두번째 이미지 테두리 안에 넣기
if(this.X1 > 115 && this.X1 < 165)
if(this.Y1 > 350 && this.Y1 < 450)
{
this.X1 = 140;
this.Y1 = 400;
/*if(Flag6 == false)
{
//Toast.makeText(getContext(), "맞았다2.", Toast.LENGTH_SHORT).show();
}
Flag6 = true; */
}
if(this.X > 115 && this.X < 165)
if(this.Y > 350 && this.Y < 450)
{
this.X = 140;
this.Y = 400;
/*if(Flag6 == false)
{
//Toast.makeText(getContext(), "맞았다2.", Toast.LENGTH_SHORT).show();
}
Flag6 = true; */
}
//세번째 이미지 화면 넘김방지
if(this.X2+rw > width )
{
this.X2 = width-rw;
Flag3 = false;
}
if(this.X2 < 0)
{
this.X2 = 0;
Flag3 = false;
}
if(this.Y2+rh > height)
{
this.Y2 = height-rh;
Flag3 = false;
}
if(this.Y2 < 0 )
{
this.Y2 = 0;
Flag3 = false;
}
//세번째 이미지 테두리 안에 넣기
if(this.X2 > 240 && this.X2 < 290)
if(this.Y2 > 350 && this.Y2 < 450)
{
this.X2 = 265;
this.Y2 = 400;
/*if(Flag7 == false)
{
Toast.makeText(getContext(), "맞았다3.", Toast.LENGTH_SHORT).show();
}
Flag7 = true; */
}
if(this.X > 240 && this.X < 290)
if(this.Y > 350 && this.Y < 450)
{
this.X = 265;
this.Y = 400;
/*if(Flag7 == false)
{
Toast.makeText(getContext(), "맞았다3.", Toast.LENGTH_SHORT).show();
}
Flag7 = true; */
}
/*if(this.X == 15 && this.X1 == 140)
if(Flag8 == false)
{
{
Toast.makeText(getContext(), "두개 맞았습니다.", Toast.LENGTH_SHORT).show();
}
Flag8 = true;
}*/
break;
case MotionEvent.ACTION_UP:
prevX = -1;
prevY = -1;
Flag1 = false;
Flag2 = false;
Flag3 = false;
Flag6 = false;
Flag7 = false;
Flag8 = false;
if(this.X == 15 && this.Y == 400 && Flag5 == false)
{
Toast.makeText(getContext(), "맞았다1.", Toast.LENGTH_SHORT).show();
Flag5 = true;
}
Log.d("TAG", "Flag = false");
};
invalidate();
return Flag4;
}
}