How to implement Drag and Drop in android?

柔情痞子 提交于 2019-12-10 10:23:27

问题


I am developing a puzzle game, where user has to arrange the images in the grid. The screenshot is given below

I want to able to drag the image of one cell of the grid to another cell. I searched many sites and every where I found examples with Drag and drop API (I.e. by using OnDragListener etc.) which was introduced in Android 3.0, but my application should run in Android 2.2.

So please help me how to implement it using Touching API (I.e. OnTouchListener etc.)


回答1:


One way of doing it would be take the x & y location of the touch in relation to the grid.

IE. at 10x10 grid on a 100x100 area.

If the touch was at 25,25, it would choose square 2,2(using an array). You could then save that location to a variable (so as to move whatever piece to it that you are changing it with) and on drag update the bitmap x,y in relation to the touch.

Once you lift your finger at say, 75,75, it would set the puzzle piece at 7,7 and move that piece to 2,2.

I used something similar, less the drag, on my Lazer Maze Lite game. Mine was basically moving mirrors and bombs on touch though, but....

http://developer.android.com/guide/topics/ui/drag-drop.html



来源:https://stackoverflow.com/questions/9292602/how-to-implement-drag-and-drop-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!