java - 拖放图像按钮消失在不同 View 后面

标签 java android xml drag-and-drop imagebutton

我正在编写一款Android游戏,用户可以购买在购买后动态创建的建筑物。创建它们后,用户可以将它们拖放到任何想要的地方,只要它们在地面上即可。我将天空和地面作为两种不同的框架布局,但天空占据顶部的 25%,地面占据底部的 75%。当用户拖动建筑物时,它们会按照应有的方式行事,直到用户尝试将其拖动到天空区域。它不会像我希望的那样快速回到原来的位置,而是消失在天空区域后面。

这是顶部和底部的 xml:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/mars"
    android:id="@+id/gameBackground" >

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="0.75"
        android:orientation="horizontal"
        android:id="@+id/topHalf" >

    </FrameLayout>

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="0.25"
        android:orientation="horizontal"
        android:id="@+id/bottomHalf" >

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

        <ImageButton 
            android:id="@+id/polarCapButton2"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top|right"
            android:background="@drawable/polarcap" />

    </FrameLayout>  

</LinearLayout>

以下是创建图像按钮的代码:

frame = (FrameLayout) findViewById(R.id.bottomHalf);
newColonyHut = new ImageButton(runGraphics.this);
newColonyHut.setBackgroundResource(R.drawable.mainhut);     
newColonyHut.setTag("NewColonyHut");
newColonyHut.setOnTouchListener(new ColonyHutClick());
findViewById(R.id.bottomHalf).setOnDragListener(new ColonyHutDrag());
FrameLayout.LayoutParams  param = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
frame.addView(newColonyHut, param);

这是 ColonyHutClick 类:

public class ColonyHutClick implements OnTouchListener 
{
    @Override
    public boolean onTouch(View v, MotionEvent event) 
    {
        ClipData.Item item = new ClipData.Item((CharSequence)v.getTag());

        String[] mimeTypes = { ClipDescription.MIMETYPE_TEXT_PLAIN }; 
        ClipData data = new ClipData(v.getTag().toString(), mimeTypes, item); 
        DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v); 

        v.startDrag(data, shadowBuilder, v, 0);
        v.setVisibility(View.INVISIBLE);

        return false;
    }//end onTouch function
}//end ColonyHutClick class

这是 ColonyHutDrag 类:

public class ColonyHutDrag extends Activity implements OnDragListener
{

    @Override
    public boolean onDrag(View v, DragEvent event) 
    {
        int x = 0, y = 0;
        switch(event.getAction())
        {
            case DragEvent.ACTION_DRAG_STARTED:
                //drag has started
                break;

            case DragEvent.ACTION_DRAG_ENTERED:
                //being dragged
                break;

            case DragEvent.ACTION_DRAG_EXITED:
                //stop drag
                break;

            case DragEvent.ACTION_DRAG_LOCATION:
                //find drag location
                break;

            case DragEvent.ACTION_DROP:
                if (v == v.findViewById(R.id.bottomHalf))
                {
                    //find position where dropped
                    x = (int) event.getX();
                    y = (int) event.getY();


                    View view = (View) event.getLocalState();
                    ViewGroup group = (ViewGroup) view.getParent();
                    group.removeView(view);
                    FrameLayout contain = (FrameLayout) v;
                    contain.addView(view);
                    view.setX(x - (view.getWidth()/2));
                    view.setY(y - (view.getHeight()/2));
                    view.setVisibility(View.VISIBLE);
                }//end if
                else
                {
                    View view = (View) event.getLocalState();
                    view.setVisibility(View.VISIBLE);
                }//end else
                break;

            default:
                    break;
        }//end switch
        return true;
    }//end onDrag function
}//end ColonyHutDrag class

感谢您提前提供的任何帮助。

最佳答案

您需要设置“Sky”来处理拖动事件。您只需一行即可完成此操作:

 findViewById(R.id.topHalf).setOnDragListener(new ColonyHutDrag());

当通过没有设置 OnDragListenerView 释放拖动的对象时,没有任何东西可以处理 ACTION_DROP,因此,您没有机会将拖动的 View 重置为 VISIBLE

关于java - 拖放图像按钮消失在不同 View 后面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24900769/

相关文章:

java - 在可浏览 Activity 中查找 Intent 发送者的 URL

android.view.WindowInsets ClassNotFoundException 异常

java - 该应用程序不起作用。它适用于 Android 5.1,但不适用于 Android 10.0

java - 用于简单 xml 节点字符串的 Android XML 解析器

java - 如何在方法之间进行交集

java - 对象数组的空指针异常

c# - 选择最后一个 XML 节点

java - 如何使用xsl中的参数:apply-templates which are set from java?

java - 使用 Lucene 近实时索引功能时是否需要关闭 DirectoryReader

Android NDK 访问原生蓝牙功能 bluedroid