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 Img;
int width,height;
int rectL;
Rect rect;
Paint rectPaint ;
Context context;
int X,Y;
public MyView(Context context) {
super(context);
this.context=context;
init();//초기화 하기
//핸들러를 호출해서 화면이 주기적으로 갱신되도록 한다.
handler.sendEmptyMessage(0);
}
public void init(){
//화면의 폭과 높이 얻어오기
width=480;
height=800;
//원본 이미지 읽어오기
Img=BitmapFactory.decodeResource
(getResources(), R.drawable.tiffany);
rectL = width;
rectPaint=new Paint();
rectPaint.setAlpha(0);
rectPaint.setStyle(Style.FILL);
}
@Override
protected void onDraw(Canvas canvas){
canvas.drawBitmap(Img,0,0,null);
rect=new Rect(0,0,0+width, 0+height);
canvas.drawRect(rect, rectPaint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
int x = (int) event.getX();
int y = (int) event.getY();
if (rect.contains(x, y) == true)
imgRotate(Img);
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(180);
Bitmap resizedBitmap = Bitmap.createBitmap(bmp, 0, 0, width, height, matrix, true);
bmp.recycle();
return resizedBitmap;
}
Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
//화면 갱신
invalidate();
handler.sendEmptyMessageDelayed(0, 10);
}
};
}
이미지를 터치할때 이미지가 회전하게 만들어주기 위해서
이미지를 그리고 투명한 사각형을 그려서 터치영역을 설정해 주었구요
사각형 영역을 터치시 이미지가 돌아가도록 함수를 넣었는데
터치시 오류가나면서 앱이 꺼집니다..
어디를 수정해야할지 부탁드립니다