Reputation: 7
I am trying to update the position of a bitmap with an onTouch event. The image displays in the initial spot correctly. The logs show that the onTouch event is updating the coordinate and also show that onDraw is being called.
My Activity:
package com.miker.haminvaders;
import android.app.Activity;
import android.os.Bundle;
public class HamInvadersActivity extends Activity
{
private DrawView drawView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
drawView = (DrawView) findViewById(R.id.drawView);
}
@Override
public void onPause()
{
super.onPause();
drawView.stopGame();
}
@Override
protected void onDestroy()
{
super.onDestroy();
drawView.releaseResources();
}
}
My Thread Class:
package com.miker.haminvaders;
import android.graphics.Canvas;
import android.util.Log;
import android.view.SurfaceHolder;
public class MainThread extends Thread
{
private boolean running;
SurfaceHolder surfaceHolder;
DrawView drawView;
public void setRunning(boolean r)
{
this.running = r;
}
@Override
public void run()
{
Canvas canvas = null;
while(running)
{
//Log.v("HAM", "tick!");
try
{
canvas = surfaceHolder.lockCanvas();
synchronized(surfaceHolder)
{
drawView.onDraw(canvas);
drawView.updatePositions();
}
}
finally
{
if(canvas != null)
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
public MainThread(SurfaceHolder surface, DrawView draw)
{
super();
surfaceHolder = surface;
drawView = draw;
}
}
My SurfaceView class:
package com.miker.haminvaders;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.graphics.BitmapFactory;
import android.util.Log;
public class DrawView extends SurfaceView implements SurfaceHolder.Callback
{
private MainThread myThread;
private int screenWidth;
private int screenHeight;
private Paint textPaint;
private Paint backPaint;
private float x, y;
private Bitmap bmpHam;
public DrawView(Context context, AttributeSet attrs)
{
super(context, attrs);
SurfaceHolder holder = getHolder();
holder.addCallback(this);
textPaint = new Paint();
backPaint = new Paint();
myThread = new MainThread(holder, this);
bmpHam = BitmapFactory.decodeResource(getResources(), R.drawable.lilhamstrich);
}
@Override
protected void onSizeChanged( int w, int h, int oldw, int oldh)
{
super.onSizeChanged(w, h, oldw, oldh);
screenWidth = w;
screenHeight = h;
textPaint.setTextSize(w/20);
backPaint.setColor(Color.WHITE);
newGame();
}
public void newGame()
{
x = screenWidth/2;
y = screenHeight/2;
}
public void updatePositions()
{
}
public void onDraw(Canvas canvas)
{
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(bmpHam, x, y, null);
Log.v("HAM", "Draw! " + x + " " + y);
}
public void stopGame()
{
if(myThread != null)
myThread.setRunning(false);
}
public void releaseResources()
{
//soundPool stuff
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
}
@Override
public void surfaceCreated(SurfaceHolder holder)
{
myThread.setRunning(true);
myThread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
boolean retry = true;
myThread.setRunning(false);
while(retry)
{
try
{
myThread.join();
retry = false;
}
catch(InterruptedException e)
{
}
}
}
@Override
public boolean onTouchEvent (MotionEvent event)
{
int action = event.getAction();
if( action == MotionEvent.ACTION_DOWN)
{
x = event.getX();
y = event.getY();
Log.v("HAM","Down! " + x + " " + y);
}
return true;
}
}
Is there anything else that I need to include?
Upvotes: 0
Views: 907
Reputation: 1830
Call invalidate()
in the onTouchEvent()
, it should cause a redraw.
Upvotes: 1