manifast.xml<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gusfree.touchevent"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".TouchEventActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
TouchEventActivity.javapackage com.gusfree.touchevent;
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.os.Bundle;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
public class TouchEventActivity extends Activity {
MyView myView;
float x,y;
int width,height;
Bitmap bitmap;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myView = new MyView(this);
LinearLayout layout = new LinearLayout(this);//배경
LayoutParams params =
new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
layout.setLayoutParams(params);
layout.setBackgroundColor(Color.WHITE);
layout.addView(myView);
setContentView(layout);
WindowManager manager =(WindowManager)getSystemService(WINDOW_SERVICE);
Display display = manager.getDefaultDisplay();
width=display.getWidth();//내 화면의 가로 사이즈
height=display.getHeight();//내 화면의 세로 사이즈
x=width/2;//초기값
y=height/2;
bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.snoopy);
}
class MyView extends View{
public MyView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(bitmap,x,y,null);
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
x=ev.getRawX();
y=ev.getRawY();
myView.postInvalidate();//다시 그려라
return super.dispatchTouchEvent(ev);
}
}