Does anyone know if there is a simple way to make images snap to grid when dragging a bitmap?
At the moment I can touch a bitmap and move it smoothly around the screen. I want to be able to make it snap to an invisible grid whilst you are dragging.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s what I do in an application I’m just finishing at the moment. When the user is dragging something on the screen, I display a visible snap grid, and the object is snapped to that grid when the dragging has finished. To show a grid, my approach is to use a separate custom
Viewthat I namedGridOverLayView. It is overlaid over the entire screen area and it very simply draws a snap grid in itsonDraw()method. It is made visible only when something is being dragged.Now, concerning the actual
Activityin which dragging and dropping is handled, one particular constant I’ve defined is:When the object is being dragged around, i.e. when processing
event.getAction()==MotionEvent.ACTION_MOVEevents within myOnTouchListener, I perform snapping of the object’s location to grid using the following:…where
draggedInitialYanddraggedInitialXstore the initial touch position recorded during the initialMotionEvent.ACTION_DOWN.A further nice touch is to allow the object being dragged to be moved around without snapping, but have it snap to the grid only in the
.ACTION_UPwhen the user lifts their finger. In practice, this feels much nicer to use.