我正在做一个 android 应用程序,我必须在其中移动不同的图像。我做了一些编码,但问题是:当我想移动一个图像时,其他图像随之而来。是否可以为位图上的每个图像制作不同的触摸监听器?
MyBringBackSurface ourSurfaceView, ourSurfaceView2;
float x, y, sX, sY, fX, fY, dX, dY, aniX, aniY, scaledX, scaledY;
Bitmap test;
Bitmap dress, dress2;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
ourSurfaceView = new MyBringBackSurface(this);
ourSurfaceView.setOnTouchListener(this);
x = 0;
y = 0;
sX = 0;
sY = 0;
fX = 0;
fY = 0;
dX = dY = aniX = aniY = scaledX = scaledY = 0;
dress = BitmapFactory.decodeResource(getResources(), R.drawable.dress);
dress2 = BitmapFactory
.decodeResource(getResources(), R.drawable.dress2);
test = BitmapFactory.decodeResource(getResources(),
R.drawable.cinderelladoll);
setContentView(ourSurfaceView);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSurfaceView.pause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
ourSurfaceView.resume();
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
x = event.getX();
y = event.getY();
sX = event.getX();
sY = event.getY();
return true;
}
public class MyBringBackSurface extends SurfaceView implements Runnable {
SurfaceHolder ourHolder;
Thread ourThread = null;
boolean isRunning = false;
Bitmap back;
public MyBringBackSurface(Context context) {
super(context);
ourHolder = getHolder();
back = BitmapFactory.decodeResource(getResources(),
R.drawable.wallpaper);
}
public void pause() {
isRunning = false;
while (true) {
try {
ourThread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
ourThread = null;
}
public void resume() {
isRunning = true;
ourThread = new Thread(this);
ourThread.start();
}
@Override
public void run() {
// TODO Auto-generated method stub
while (isRunning) {
if (!ourHolder.getSurface().isValid())
continue;
Canvas canvas = ourHolder.lockCanvas();
canvas.drawRGB(02, 02, 150);
canvas.drawBitmap(back, 0, 0, null);
if (x != 0 && y != 0) {
canvas.drawBitmap(test, 150, 150, null);
canvas.drawBitmap(dress2, x - (dress2.getWidth() / 2), y
- (dress2.getHeight() / 2), null);
canvas.drawBitmap(dress, x - (dress.getWidth() / 2), y
- (dress.getHeight() / 2), null);
}
ourHolder.unlockCanvasAndPost(canvas);
}
}
}
}