A Surface view and a canvas to move Bitmap
Posted
by
John Apple Sim
on Stack Overflow
See other posts from Stack Overflow
or by John Apple Sim
Published on 2012-10-12T19:18:34Z
Indexed on
2012/10/13
9:37 UTC
Read the original article
Hit count: 204
I have a SurfaceView
and I want the Bitmap Logo
inside it in the canvas
to be movable
What I'm doing wrong ?
static float x, y;
Bitmap logo;
SurfaceView ss = (SurfaceView) findViewById(R.id.svSS);
logo = BitmapFactory.decodeResource(getResources(), R.drawable.logo);
x = 40;
y = 415;
ss.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent me) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
switch(me.getAction()) {
case MotionEvent.ACTION_DOWN:
x = me.getX();
y = me.getY();
break;
case MotionEvent.ACTION_UP:
x = me.getX();
y = me.getY();
break;
case MotionEvent.ACTION_MOVE:
x = me.getX();
y = me.getY();
break;
}
return true;
}
});
public class OurView extends SurfaceView implements Runnable{
Thread t = null;
SurfaceHolder holder;
boolean isItOK = false;
public OurView(Context context) {
super(context);
holder = getHolder();
}
public void run (){
while (isItOK == true){
//canvas DRAWING
if (!holder.getSurface().isValid()){
continue;
}
Canvas c = holder.lockCanvas();
c.drawARGB(255, 200, 100, 100);
c.drawBitmap(logo, x,y,null);
holder.unlockCanvasAndPost(c);
}
}
public void pause(){
isItOK = false;
while(true){
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
}
t = null;
}
public void resume(){
isItOK = true;
t = new Thread(this);
t.start();
}
}
Now the surface view is just black .. nothing happens also its not colored 200, 100, 100
© Stack Overflow or respective owner