我有两个imageview,其中我使用setImageBitmap添加图像,并将setOnTouchListener应用于它们。但这里的问题是,第一次添加第一个img时,它通过触摸移动但是当我添加第二个img时,第二个img移动但是之后我无法通过触摸移动第一个img。对不起英语,感谢先进。
答案 0 :(得分:0)
你必须分别为两个图像提供onTouchListener
ImageView first_image=new ImageView(this);
first_image.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_UP)
{
}
if(event.getAction()==MotionEvent.ACTION_DOWN)
{
}
if(event.getAction()==MotionEvent.ACTION_MOVE)
{
//Code for image moving
}
return false;
}
});
ImageView second_image=new ImageView(this);
second_image.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_UP)
{
}
if(event.getAction()==MotionEvent.ACTION_DOWN)
{
}
if(event.getAction()==MotionEvent.ACTION_MOVE)
{
//code for image moving
}
return false;
}
});
答案 1 :(得分:0)
请尝试使用以下代码,它可以帮助您并使用Imageview而不是Textview。
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;
}
});