簡體   English   中英

拖放imagebutton在其他視圖后消失

[英]Drag and Drop imagebutton Dissapears behind different view

我正在編寫一個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>

這是創建imagebutton的代碼:

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

感謝您的任何幫助。

您需要設置“天空”以處理拖動事件。 您可以使用一行來完成此操作:

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

在沒有設置OnDragListenerView上釋放拖動的對象時,沒有任何東西可以處理ACTION_DROP ,因此您沒有機會將拖動的View重置為VISIBLE

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM