我有图像视图。我使用平移,捏图像视图。有时我需要删除 imageView。所以,我使用 OnLongClickListener 来删除图像。当我使用长按我的 imageView 删除。但是当我使用 OnTouchListener 进行平移时,捏住 imageView OnLongPress 激活并且我的 imageView 从视图中移除。如何解决这个问题?
代码:
imageView.setOnLongClickListener(new OnLongClickListener(){
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
imageView.setVisibility(View.GONE);
return true;
}
});
imageView.setOnTouchListener(new View.OnTouchListener() {
final Handler handler = new Handler();
Runnable mLongPressed = new Runnable() {
public void run() {
Log.i("", "Long press!");
}
};
@Override
public boolean onTouch(View v,MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == MotionEvent.ACTION_DOWN)
handler.postDelayed(mLongPressed, 1000);
if((event.getAction() == MotionEvent.ACTION_MOVE)||(event.getAction() == MotionEvent.ACTION_UP))
handler.removeCallbacks(mLongPressed);
layoutParams = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN :
{
parms = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
dx = event.getRawX() - parms.leftMargin;
dy = event.getRawY() - parms.topMargin;
}
break;
case MotionEvent.ACTION_MOVE :
{
x = event.getRawX();
y = event.getRawY();
parms.leftMargin = (int) (x-dx);
parms.topMargin = (int) (y - dy);
imageView.setLayoutParams(parms);
}
break;
case MotionEvent.ACTION_UP :
{
}
break;
}
return false;
}
});
}
});