我有两个图像视图,我在其中使用 setImageBitmap 添加图像,并将 setOnTouchListener 应用于它们。但这里的问题是,第一次添加第一个 img 时,它通过触摸移动,但是当我添加第二个 img 时,第二个 img 移动,但之后我无法通过触摸移动第一个 img。对不起英语和提前感谢。这是我的代码:-
working_bitmap = BitmapFactory.decodeFile(file_location+"cropped_image.jpg");
outBitmap=Bitmap.createBitmap(
working_bitmap.getWidth(),
working_bitmap.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas1 = new Canvas(outBitmap);
canvas1.drawBitmap(working_bitmap, new Matrix(), null);
if(downloaded_shirt_image != null)
{
int width = downloaded_shirt_image.getWidth();
int height = downloaded_shirt_image.getHeight();
int halfWidth = width/3;
int halfHeight = height/3;
//Half Scaled
Bitmap bmHalf = Bitmap.createScaledBitmap(downloaded_shirt_image,
halfWidth, halfHeight, false);
proimg =(ImageView)findViewById(R.id.pro_img);
proimg.setDrawingCacheEnabled(true);
proimg.setImageBitmap(bmHalf);
proimg.setVisibility(View.VISIBLE);
proimg.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event) {
ImageView view = (ImageView) v;
int rotation = 25;
// Dump touch event to log
dumpEvent(event);
// Handle touch events here...
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
Log.d(TAG, "mode=DRAG");
mode = DRAG;
proimg.invalidate();
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
// ...
matrix.set(savedMatrix); matrix.postTranslate(event.getX() - start.x,
event.getY() - start.y);
}
else if (mode == ZOOM) {
float newDist = spacing(event);
Log.d(TAG, "newDist=" + newDist);
if (newDist > 10f) {
matrix.set(savedMatrix);
float scale = newDist / oldDist;
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
proimg.invalidate();
break;
}
view.setImageMatrix(matrix);
return true; // indicate event was handled
}
});
canvas1.drawBitmap(downloaded_shirt_image, 50, 192, null);
}
if(downloaded_pant_image != null)
{
int width = downloaded_pant_image.getWidth();
int height = downloaded_pant_image.getHeight();
int halfWidth = width/3;
int halfHeight = height/3;
//Half Scaled
Bitmap bmHalf = Bitmap.createScaledBitmap(downloaded_pant_image,
halfWidth, halfHeight, false);
pantimg =(ImageView)findViewById(R.id.pro_img_down);
pantimg.setDrawingCacheEnabled(true);
pantimg.setImageBitmap(bmHalf);
pantimg.setVisibility(View.VISIBLE);
pantimg.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event) {
ImageView view = (ImageView) v;
int rotation = 25;
// Dump touch event to log
dumpEvent(event);
// Handle touch events here...
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
Log.d(TAG, "mode=DRAG");
mode = DRAG;
pantimg.invalidate();
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
// ...
matrix.set(savedMatrix); matrix.postTranslate(event.getX() - start.x,
event.getY() - start.y);
}
else if (mode == ZOOM) {
float newDist = spacing(event);
Log.d(TAG, "newDist=" + newDist);
if (newDist > 10f) {
matrix.set(savedMatrix);
float scale = newDist / oldDist;
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
pantimg.invalidate();
break;
}
view.setImageMatrix(matrix);
return true; // indicate event was handled
}
}
);
canvas1.drawBitmap(downloaded_pant_image, 110, 565, null);
}