package com.blog.demo11; import android.annotation.SuppressLint; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.widget.ImageView; import androidx.annotation.Nullable; import com.blog.BaseActivity; import com.blog.R; import static android.view.MotionEvent.INVALID_POINTER_ID; /** * 可拖拽效果类。 */ public class ViewDragActivity extends BaseActivity { private ImageView backgroundImage; private ImageView image1; private ImageView image2; @SuppressLint("MissingInflatedId") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.drag_layout); backgroundImage = findViewById(R.id.background_image); image1 = findViewById(R.id.image1); image2 = findViewById(R.id.image2); // Set touch listeners for each image image1.setOnTouchListener(new MultiTouchListener()); image2.setOnTouchListener(new MultiTouchListener()); } private class MultiTouchListener implements View.OnTouchListener { private float lastTouchX, lastTouchY; private int activePointerId = INVALID_POINTER_ID; @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: { final int pointerIndex = event.getActionIndex(); final float x = event.getX(pointerIndex); final float y = event.getY(pointerIndex); lastTouchX = x; lastTouchY = y; activePointerId = event.getPointerId(pointerIndex); break; } case MotionEvent.ACTION_MOVE: { final int pointerIndex = event.findPointerIndex(activePointerId); final float x = event.getX(pointerIndex); final float y = event.getY(pointerIndex); final float dx = x - lastTouchX; final float dy = y - lastTouchY; v.setX(v.getX() + dx); v.setY(v.getY() + dy); lastTouchX = x; lastTouchY = y; break; } case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: { activePointerId = INVALID_POINTER_ID; break; } case MotionEvent.ACTION_POINTER_UP: { final int pointerIndex = event.getActionIndex(); final int pointerId = event.getPointerId(pointerIndex); if (pointerId == activePointerId) { final int newPointerIndex = pointerIndex == 0 ? 1 : 0; lastTouchX = event.getX(newPointerIndex); lastTouchY = event.getY(newPointerIndex); activePointerId = event.getPointerId(newPointerIndex); } break; } } return true; } } }
时间: 2024-02-10 19:08:55 浏览: 122
这是一个实现了可拖拽效果的 Android Activity 类,其中包含了三个 ImageView,分别是背景图 backgroundImage 和两个可拖拽的图片 image1 和 image2。通过实现 View.OnTouchListener 接口,对每个 ImageView 设置触摸事件监听器 MultiTouchListener,实现了图片的拖拽效果。在 MultiTouchListener 中,通过 MotionEvent 对象获取触摸事件的类型和坐标,计算出拖拽的偏移量,再调用 v.setX() 和 v.setY() 方法实现图片的移动,最终实现了可拖拽效果。
相关问题
package com.example.login; import androidx.appcompat.app.AppCompatActivity; import android.annotation.SuppressLint; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.SimpleCursorAdapter; import android.app.AlertDialog; import android.content.DialogInterface; import android.widget.Spinner; import android.widget.Toast;
这是一个 Android 应用程序中的 Java 代码片段,用于实现一个登录功能。它包含了一些 Android 库的导入,如 `AppCompatActivity`,`ListView` 等等。这段代码还实现了与数据库的交互,通过 `SQLiteDatabase`、`Cursor`、`ContentValues` 等类进行数据的增删改查操作。此外,还包含了一些 UI 组件的定义,如 `EditText`、`Button`、`Spinner` 等等。最后,还有一些弹出框的实现,如 `AlertDialog`、`Toast` 等等。
package com.blog.demo11; import android.annotation.SuppressLint; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.widget.ImageView; import androidx.annotation.Nullable; import com.blog.BaseActivity; import com.blog.R; import static android.view.MotionEvent.INVALID_POINTER_ID; /** * 可拖拽效果类。 */ public class ViewDragActivity extends BaseActivity { private ImageView backgroundImage; private ImageView image1; private ImageView image2; @SuppressLint("MissingInflatedId") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); backgroundImage = findViewById(R.id.background_image); image1 = findViewById(R.id.image1); image2 = findViewById(R.id.image2); // Set touch listeners for each image image1.setOnTouchListener(new MultiTouchListener()); image2.setOnTouchListener(new MultiTouchListener()); } private class MultiTouchListener implements View.OnTouchListener { private float lastTouchX, lastTouchY; private int activePointerId = INVALID_POINTER_ID; @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: { final int pointerIndex = event.getActionIndex(); final float x = event.getX(pointerIndex); final float y = event.getY(pointerIndex); lastTouchX = x; lastTouchY = y; activePointerId = event.getPointerId(pointerIndex); break; } case MotionEvent.ACTION_MOVE: { final int pointerIndex = event.findPointerIndex(activePointerId); final float x = event.getX(pointerIndex); final float y = event.getY(pointerIndex); final float dx = x - lastTouchX; final float dy = y - lastTouchY; v.setX(v.getX() + dx); v.setY(v.getY() + dy); lastTouchX = x; lastTouchY = y; break; } case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: { activePointerId = INVALID_POINTER_ID; break; } case MotionEvent.ACTION_POINTER_UP: { final int pointerIndex = event.getActionIndex(); final int pointerId = event.getPointerId(pointerIndex); if (pointerId == activePointerId) { final int newPointerIndex = pointerIndex == 0 ? 1 : 0; lastTouchX = event.getX(newPointerIndex); lastTouchY = event.getY(newPointerIndex); activePointerId = event.getPointerId(newPointerIndex); } break; } } return true; } } }
这是一段 Android 应用程序代码,实现了一个可拖拽效果类 ViewDragActivity。在该类中,通过监听 onTouch 事件来实现多指触摸拖拽图片的效果,其中 MultiTouchListener 类实现了 View.OnTouchListener 接口,重写了 onTouch 方法,通过获取 MotionEvent 的不同事件来实现图片的移动。具体实现过程可以参考代码中的注释。
阅读全文