java - 移动拖放 ImageButton 时 ImageButtons 被压扁

标签 java android mobile drag-and-drop imagebutton

所以我设置了 4 个普通图像按钮,然后是第 5 个图像按钮,它是一个拖放按钮。我希望 4 个非拖放图像按钮保留在特定位置,无论拖放图像按钮发生什么情况。当我移动拖放图像按钮时,其他图像按钮也会移动并且也被压扁。

知道如何在不影响其他图像按钮的情况下移动这个拖放图像按钮吗?

这是我的 xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gllayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:background="@drawable/mars" >

<TextView
    android:id="@+id/scores"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<ImageButton
    android:id="@+id/mainHut"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/mainhut" />

<ImageButton 
    android:id="@+id/polarCapButton1"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/polarcap" />

<ImageButton 
    android:id="@+id/polarCapButton2"
    android:layout_marginTop="-70dp"
    android:layout_marginLeft="575dp"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/polarcap" />

<ImageButton 
    android:id="@+id/spaceRockButton1"
    android:layout_marginTop="100dp"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/spacerock" />

<ImageButton 
    android:id="@+id/spaceRockButton2"
    android:layout_marginTop="-140dp"
    android:layout_marginLeft="520dp"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/spacerock" />

<ImageButton
    android:id="@+id/meteor"
    android:visibility="invisible"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/meteor" 
    android:layout_marginTop="-100dp"
    android:layout_marginLeft="400dp" />

</LinearLayout>

这是我拖放图像按钮的代码:

    mainHut = (ImageButton) findViewById(R.id.mainHut);
    mainHut.setOnTouchListener(new OnTouchListener()
    {
        @Override
        public boolean onTouch(View v, MotionEvent event) 
        {
            if (event.getAction() == MotionEvent.ACTION_DOWN)
            {
                mainHutSelected = true;
            }//end if

            if (event.getAction() == MotionEvent.ACTION_UP)
            {
                if (mainHutSelected == true)
                {
                    mainHutSelected = false;
                }//end if
            }//end if
            else if (event.getAction() == MotionEvent.ACTION_MOVE)
            {
                if (mainHutSelected == true)
                {
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(v.getWidth(), v.getHeight());
                    params.setMargins((int)(event.getRawX() - event.getRawY() / 2), (int) (event.getRawY() - v.getHeight() * 1.5), (int) (event.getRawX() - v.getWidth() / 2), (int) (event.getRawY() - v.getHeight() * 1.5));
                    v.setLayoutParams(params);
                }//end if
            }//end else

            return false;
        }//end onTouch function

    });//end setOnClickListener

在此先感谢您的帮助。

最佳答案

我建议您使用 FrameLayout。这将使您的 ImageView 可堆叠。

<FrameLayout>
    <ImageButton/> 
    <ImageButton/>
    <ImageButton/>
    <ImageButton/>
    <ImageButton/>
</FrameLayout>

您需要将它们全部设置为基于其父原点在屏幕上具有静态位置。第 5 个 ImageButton,您可以像以前一样使用它来设置 onTouch 监听器。

编辑:或者你可以这样做(如果你想保持线性布局):

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gllayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/mars"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/scores"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <ImageButton
            android:id="@+id/polarCapButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/polarcap"
            android:orientation="vertical" />

        <ImageButton
            android:id="@+id/polarCapButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="575dp"
            android:layout_marginTop="-70dp"
            android:background="@drawable/polarcap"
            android:orientation="vertical" />

        <ImageButton
            android:id="@+id/spaceRockButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="100dp"
            android:background="@drawable/spacerock"
            android:orientation="vertical" />

        <ImageButton
            android:id="@+id/spaceRockButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="520dp"
            android:layout_marginTop="-140dp"
            android:background="@drawable/spacerock"
            android:orientation="vertical" />

        <ImageButton
            android:id="@+id/meteor"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="400dp"
            android:layout_marginTop="-100dp"
            android:background="@drawable/meteor"
            android:orientation="vertical"
            android:visibility="invisible" />
    </LinearLayout>

    <ImageButton
        android:id="@+id/mainHut"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="put offset here"
        android:background="@drawable/mainhut" />

</FrameLayout>

只需将 mainHut 上的 marginTop 更改为您要设置的偏移量即可!

关于java - 移动拖放 ImageButton 时 ImageButtons 被压扁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24149034/

相关文章:

java - 转换为动态类在方法调用中崩溃

android - map View 开始位置

Android:如何使用 android.hardware.camera2 和 MediaRecorder 录制视频

javascript - -webkit-溢出-滚动 : touch causes jQuery dynamic changes to not show?

iphone - 移动分析系统中的跟踪

javax.xml.bind.UnmarshalException : unexpected element (uri :"", 本地 :"")。预期的元素是

java - 当正在加载的 Activity 保持不变时, Activity 滑到底部(就好像它向下滑动以显示新 Activity 一样)

java - 重构 orElseThrow block 以返回 RuntimeException

android - Dagger2 自定义范围和销毁组件

html - 如何在移动设备上显示具有长选项值的下拉列表?