简体   繁体   中英

Creating a Drag and Drop ImageButton

I have looked tutorials on how to make my imagebutton capable of drag and drop. With the code I have the imagebutton just disappears when I click it. and when I click anywhere else it does not come back.

Here is my code for the imagebutton:

    mainHut = (ImageButton) findViewById(R.id.mainHut);
    mainHut.setOnTouchListener(new OnTouchListener()
    {
        @Override
        public boolean onTouch(View v, MotionEvent event) 
        {
            Toast.makeText(getApplicationContext(), "in the movement", Toast.LENGTH_SHORT).show();
            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(50, 50);
                    params.setMargins((int)event.getRawX() - 25, (int) event.getRawY() - 50, 0, 0);
                    layout = (LinearLayout) findViewById(R.id.gllayout);
                    layout.removeView(mainHut);
                    layout.addView(mainHut, params);
                }//end if
            }//end else

            return false;
        }//end onTouch function

    });

here is the xml for the layout:

<?xml version="1.0" encoding="utf-8"?>
<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/bgm" >

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

Any help is appreciated. Thanks.

To answer my own question...this is how I fixed my issue. It took me a long time to figure this out but here it is. The parameters are a little messy when it comes to my own imagebutton so I will have to figure out what to use to keep my finger in the center of the button, but this moves the button like I was wanting:

button.setOnTouchListener(new View.OnTouchListener() 
{

    public boolean onTouch(View v, MotionEvent event)
    {
        if (me.getAction() == MotionEvent.ACTION_MOVE )
        {
             LayoutParams params = new LayoutParams(v.getWidth(),  v.getHeight());
             params.setMargins((int)event.getRawX() - v.getWidth()/2, (int)(event.getRawY() - v.getHeight()), (int)event.getRawX() - v.getWidth()/2, (int)(event.getRawY() - v.getHeight()));
             v.setLayoutParams(params);
        }
        return false;
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM