gpt4 book ai didi

android - 如何将图像拖到另一个图像背景上

转载 作者:行者123 更新时间:2023-11-29 01:39:29 25 4
gpt4 key购买 nike

当我拖动 image1 时,它隐藏在 image2 的背景下,我需要在拖动后将 image1 设置在 image2 上。

代码如下: image1.setOnTouchListener(new View.OnTouchListener() {

        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
@Override
public boolean onTouch(View v, MotionEvent event) {
layoutParams = (LayoutParams) image1.getLayoutParams();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
int x_cord = (int) event.getRawX();
int y_cord = (int) event.getRawY();

if (x_cord > windowwidth) {
x_cord = windowwidth;
}
if (y_cord > windowheight) {
y_cord = windowheight;
}

layoutParams.leftMargin = x_cord - 25;
layoutParams.topMargin = y_cord - 75;

image1.setLayoutParams(layoutParams);
break;
default:
break;
}
return true;
}
});

这是.xml文件

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#888"
android:orientation="vertical"
>

<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:src="@drawable/image" />
</FrameLayout>

最佳答案

我得到了我的答案:

用过这个

             image1.bringToFront();

将图像设置在另一个 ImageView 的前面。

关于android - 如何将图像拖到另一个图像背景上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25638200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com