import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.SurfaceView;
import android.view.SurfaceHolder;
import android.view.Window;
import android.widget.AbsoluteLayout;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
public class SurfaceViewTest extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
getWindow().setFormat(PixelFormat.TRANSPARENT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new MySurfaceView(this));
addContentView(new SampleView(this), new ViewGroup.LayoutParams(-1, -1));
}
public class SampleView extends AbsoluteLayout {
private DisplayObject mKirby;
public SampleView(Context context) {
super(context);
mKirby = new DisplayObject(context);
this.addView(mKirby);
Bitmap bmp = BitmapFactory.decodeResource(this.getContext().getResources(), R.drawable.kirby_t);
mKirby.setBitmap(bmp);
mKirby.move((320 - mKirby.getImageWidth())/2, (240 - mKirby.getImageHeight())/2);
}
@Override
public void onDraw(Canvas canvas) {
mKirby.draw(canvas);
}
@Override
public boolean onMotionEvent(MotionEvent event) {
switch( event.getAction() ) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
int x = (int)(event.getX() - mKirby.getImageWidth()/2);
int y = (int)(event.getY() - mKirby.getImageHeight()/2);
mKirby.move(x, y);
return true;
}
return true;
}
}
public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
public MySurfaceView(Context context) {
super(context);
mHolder = getHolder();
mHolder.setCallback(this);
}
public boolean surfaceCreated(SurfaceHolder holder) {
return true;
}
public void surfaceDestroyed(SurfaceHolder holder) {
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
}
}
}
'Android' 카테고리의 다른 글
이클립스 이유없이 실행이 안될때 (0) | 2013.10.31 |
---|---|
카메라 제어 (0) | 2012.12.27 |
UI Thread 사용예제 (0) | 2012.12.27 |
WebView 사용예 (loadUrl, loadData) (0) | 2012.12.27 |