在Android中使用手势检测来拖动图片,可以通过以下步骤实现:
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image" />
ImageView imageView = findViewById(R.id.imageView);
imageView.setOnTouchListener(new View.OnTouchListener() {
private float lastX, lastY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
lastX = event.getX();
lastY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
float deltaX = event.getX() - lastX;
float deltaY = event.getY() - lastY;
float translationX = v.getTranslationX() + deltaX;
float translationY = v.getTranslationY() + deltaY;
v.setTranslationX(translationX);
v.setTranslationY(translationY);
break;
}
return true;
}
});
领取专属 10元无门槛券
手把手带您无忧上云