I want to move two simple bitmap on a surfaceview by finger. With one bitmap, it works fine, but I can't handle two bitmaps. I tried it with imageview onTouchListener, but then I had to use View.setX(float x) which is API level 11 amd I don't want go above API level 8. How can I implement it?
public class ItemClass extends SurfaceView implements SurfaceHolder.Callback, Runnable {
private SurfaceHolder surface;
private Paint p = new Paint();
private Bitmap[] item = new Bitmap[2];
private int[] coord = new int[2];
public DrawClass(Context context) {
super(context);
getHolder().addCallback(this);
surface = getHolder();
item[0] = BitmapFactory.decodeResource(getResources(), R.drawable.img1);
item[1] = BitmapFactory.decodeResource(getResources(), R.drawable.img2);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
@Override
public void surfaceCreated(SurfaceHolder holder) {
Thread drawThread = new Thread(this);
drawThread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {}
@Override
public void run() {
Canvas c;
while (true) {
c = null;
try {
c = surface.lockCanvas(null);
synchronized (surface) {
onDraw(c);
}
} finally {
if (c != null) {
surface.unlockCanvasAndPost(c);
}
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
coord[0]=(int)event.getX();
coord[1]=(int)event.getY();
return true;
}
@Override
public void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(item[0], coord[0], coord[1], null);
canvas.drawBitmap(item[1], 100, 100, null);
}
}