package com.MyGame;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PorterDuff.Mode;
import android.util.AttributeSet;
import android.view.MotionEvent;
public class MyForeground extends MyGameSurfaceView {
Sprite mySprite;
public MyForeground(Context context) {
super(context);
init();
}
public MyForeground(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyForeground(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init(){
mySprite = new Sprite(
BitmapFactory.decodeResource(getResources(), R.drawable.icon_me),
100, 100);
}
@Override
protected void onDraw(Canvas canvas) {
//Clear Canvas with transparent background
canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);
mySprite.draw(canvas);
}
@Override
public void updateStates() {
// TODO Auto-generated method stub
mySprite.update();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
int action = event.getAction();
switch(action){
case MotionEvent.ACTION_DOWN:
mySprite.setX(x);
mySprite.setY(y);
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_CANCEL:
break;
case MotionEvent.ACTION_OUTSIDE:
break;
default:
}
return true;
}
}
Next:
- React device movement/orientation using accelerometer
0 Response to "Implement onTouchEvent() to handle user touch on SurfaceView"
Posting Komentar