android studio 中,三张图片,一张做为背景固定,另两张可以缩放,移动,旋转 。请给出代码
时间: 2024-03-03 19:53:05 浏览: 134
以下是一个简单的例子,演示如何在 Android Studio 中实现三张图片,其中一张作为背景固定,另外两张可以缩放、移动和旋转。请注意,这只是一个基本示例,您可以根据需要进行修改和扩展。
首先,在 XML 布局文件中添加三个 ImageView:
```xml
<RelativeLayout
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/background_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/background_image"/>
<ImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image1"/>
<ImageView
android:id="@+id/image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image2"/>
</RelativeLayout>
```
然后,在 Java 代码中为每个 ImageView 设置可拖动、可缩放和可旋转的属性。这可以通过使用一个名为“MultiTouchListener”的自定义类来实现。以下是完整的代码:
```java
public class MainActivity extends AppCompatActivity {
private ImageView backgroundImage;
private ImageView image1;
private ImageView image2;
@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: {
activePointerId = INVALID_POINTER_ID;
break;
}
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;
}
}
}
```
这里的关键是“MultiTouchListener”类,它实现了 View.OnTouchListener 接口并处理触摸事件以实现拖动、缩放和旋转功能。在这个类中,我们使用 MotionEvent 对象来检测用户的手势,并相应地移动、缩放和旋转 ImageView。
请注意,这里只演示了两个 ImageView 的触摸监听器。如果您需要将此功能扩展到更多图像,请在 Java 代码中为每个 ImageView 设置 MultiTouchListener。
阅读全文