我有两个imageview,其中我使用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);
}
答案 0 :(得分:0)
请尝试下面的代码并使用Imageview而不是Textview。
windowwidth = getWindowManager().getDefaultDisplay().getWidth();
windowheight = getWindowManager().getDefaultDisplay().getHeight();
tv1 = (TextView)findViewById(R.id.text_view1);
tv1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
layoutParams1 = (RelativeLayout.LayoutParams) tv1.getLayoutParams();
switch(event.getActionMasked())
{
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
int x_cord = (int) event.getRawX();
int y_cord = (int) event.getRawY();
if (x_cord > windowwidth) {
x_cord = windowwidth;
}
if (y_cord > windowheight) {
y_cord = windowheight;
}
layoutParams1.leftMargin = x_cord - 25;
layoutParams1.topMargin = y_cord - 75;
tv1.setLayoutParams(layoutParams1);
break;
default:
break;
}
return true;
}
});
tv2 = (TextView)findViewById(R.id.text_view2);
tv2.setTextColor(Color.MAGENTA);
tv2.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
layoutParams2 = (RelativeLayout.LayoutParams) tv2.getLayoutParams();
switch(event.getActionMasked())
{
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
int x_cord = (int) event.getRawX();
int y_cord = (int) event.getRawY();
if (x_cord > windowwidth) {
x_cord = windowwidth;
}
if (y_cord > windowheight) {
y_cord = windowheight;
}
layoutParams2.leftMargin = x_cord - 25;
layoutParams2.topMargin = y_cord - 75;
tv2.setLayoutParams(layoutParams2);
break;
default:
break;
}
return true;
}
});