我想用手指在表面视图上移动两个简单的位图。使用一个位图,它工作正常,但我无法处理两个位图。我使用 imageview onTouchListener 进行了尝试,但后来我不得不使用 API 级别 11 的 View.setX(float x) 并且我不想超过 API 级别 8。我该如何实现它?
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);
}
}