Android的位图对齐网格 [英] Android Bitmap snap to grid
问题描述
有谁知道,如果有一个简单的方法,使拖动位图图像时对齐网格?
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.
推荐答案
这就是我在一个应用程序,我只是在一瞬间完成做。当用户拖动一些在屏幕上,我显示可见捕捉栅格,和当拖动完成对象被捕捉到该电网。要显示一个网格,我的方法是使用一个单独的自定义查看
,我命名为 GridOverLayView
。它是覆盖在整个屏幕区域,很简单地画一个单元格中的的onDraw()
方法。它是可见,只有当事情被拖动。
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 View
that I named GridOverLayView
. It is overlaid over the entire screen area and it very simply draws a snap grid in its onDraw()
method. It is made visible only when something is being dragged.
现在,关于实际活动
中拖放的处理,我定义一个特定的常数是:
Now, concerning the actual Activity
in which dragging and dropping is handled, one particular constant I've defined is:
static final int SNAP_GRID_INTERVAL = 20;
当对象被拖动周围,即当处理 event.getAction()== MotionEvent.ACTION_MOVE
中我的 OnTouchListener $事件C $ C>,我用下面的执行对象的位置捕捉到网格:
When the object is being dragged around, i.e. when processing event.getAction()==MotionEvent.ACTION_MOVE
events within my OnTouchListener
, I perform snapping of the object's location to grid using the following:
RelativeLayout.LayoutParams par = (RelativeLayout.LayoutParams) mThingBeingDragged.getLayoutParams();
par.topMargin = Math.round((event.getRawY() - draggedInitialY) / SNAP_GRID_INTERVAL ) * SNAP_GRID_INTERVAL;
par.leftMargin = Math.round((event.getRawX() - draggedInitialX) / SNAP_GRID_INTERVAL ) * SNAP_GRID_INTERVAL;
mThingBeingDragged.setLayoutParams(par);
...其中 draggedInitialY
和 draggedInitialX
存储初始 MotionEvent.ACTION_DOWN
。
一个进一步的很好的接触,是让被拖到被抢购没有四处移动的对象,但它只是在 .ACTION_UP
当用户升降机与网格对齐他们的手指。在实践中,这种感觉好得多使用。
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_UP
when the user lifts their finger. In practice, this feels much nicer to use.
这篇关于Android的位图对齐网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!