【发布时间】:2013-08-31 09:08:49
【问题描述】:
我有这个问题:
我有一个ImageView,我将它设置为onTouchListener,我可以移动它。
问题是我想设置相同的 imageview 来监听 DoubleClick 事件。
你有什么例子可以帮助我解决这个问题吗?
我需要移动并双击同一个图像视图!
【问题讨论】:
标签: android move touch-event ontouchlistener double-click
我有这个问题:
我有一个ImageView,我将它设置为onTouchListener,我可以移动它。
问题是我想设置相同的 imageview 来监听 DoubleClick 事件。
你有什么例子可以帮助我解决这个问题吗?
我需要移动并双击同一个图像视图!
【问题讨论】:
标签: android move touch-event ontouchlistener double-click
public class Touch_Screen extends Activity implements OnTouchListener {
private ImageView mImageView;
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.touch_test);
super.onCreate(savedInstanceState);
mRrootLayout = (ViewGroup) findViewById(R.id.root);
mImageView = (ImageView) mRrootLayout.findViewById(R.id.imageView);
mImageView.setVisibility(View.INVISIBLE);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(150, 150);
mImageView.setLayoutParams(layoutParams);
mImageView.setOnTouchListener(this);
}
@Override
public boolean onTouch(View view, MotionEvent event) {
try {
Thread.sleep(50);
} catch (Exception e) {
// TODO: handle exception
}
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
_xDelta = X - lParams.leftMargin;
_yDelta = Y - lParams.topMargin;
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
.getLayoutParams();
layoutParams.leftMargin = X - _xDelta;
layoutParams.topMargin = Y - _yDelta;
layoutParams.rightMargin = -250;
layoutParams.bottomMargin = -250;
view.setLayoutParams(layoutParams);
break;
}
mRrootLayout.invalidate();
return true;
}
}
【讨论】: