Android 在MotionEvent.ACTION\u中拖动视图时,布局会闪烁
下面是我正在尝试做的事情的图片: 因此,我想在拖动作为ImageView的调整大小锚点(黑色四边形)时调整单个单元格的大小。为此,我将自定义onTouchListener附加到MotionEvent.ACTION\u MOVE中执行下一步操作的对象:Android 在MotionEvent.ACTION\u中拖动视图时,布局会闪烁,android,drag-and-drop,resize,layoutparams,Android,Drag And Drop,Resize,Layoutparams,下面是我正在尝试做的事情的图片: 因此,我想在拖动作为ImageView的调整大小锚点(黑色四边形)时调整单个单元格的大小。为此,我将自定义onTouchListener附加到MotionEvent.ACTION\u MOVE中执行下一步操作的对象: 计算阻力偏移量 基于此偏移设置单元格高度/宽度 通过更改锚定点的布局参数来重新定位锚定点 这样做的结果是,单元格会调整大小,但会有一些闪烁,更像是左/右或上/下抖动,偏移量很小 我的猜测是,当它捕捉到移动事件时,问题就出现了,然后我手动改变锚
- 计算阻力偏移量
- 基于此偏移设置单元格高度/宽度
- 通过更改锚定点的布局参数来重新定位锚定点
float dragY = event.getRawY() - resizePreviousPositionY;
LinearLayout.LayoutParams componentParams = (LinearLayout.LayoutParams)selectedComponent.layout.getLayoutParams();
LinearLayout parrent = (LinearLayout)selectedComponent.layout.getParent();
View topSibling = parrent.getChildAt(parrent.indexOfChild(selectedComponent.layout) - 1);
LinearLayout.LayoutParams topSiblingParams = (LinearLayout.LayoutParams)topSibling.getLayoutParams();
if(dragY > 0 && selectedComponent.layout.getHeight() > 100 ||
dragY < 0 && topSibling.getHeight() > 100)
{
componentParams.height = selectedComponent.layout.getHeight() - (int)dragY;
topSiblingParams.height = topSibling.getHeight() + (int)dragY;
//bottomSiblingParams.height = bottomSibling.getHeight();
selectedComponent.layout.setLayoutParams(componentParams);
repositionResizeAnchors(selectedComponent);
}
resizePreviousPositionY = event.getRawY();
不要使用
MotionEvent.get*()
,而是使用MotionEvent.getRaw*()
,你注意到我根本没有看到你的代码吗?是的,这就解决了问题!但是我想知道为什么它不能以另一种方式工作,你能解释一下吗?我将编辑问题,向您展示代码。现在我还有其他问题。移动时,锚点拖在后面,就像它在追逐我正在调整大小的视图一样。我移动手指的速度越快,它落在后面的时间就越多。为什么它现在能工作呢?我将把它作为你的家庭作业……;)提示:Log.d x/y和原始x/y值根据输出,我猜get*在dp中给出位置,getRax*在px中给出位置,对吗?你能把评论转换成答案让我接受吗?我想我明白了:)getRaw*返回触摸屏上的绝对位置。get*返回相对于触摸视图左上角的位置,对吗?现在,它为什么会反弹是有道理的:)但一个新的问题和拖后腿怎么办?
if(((LinearLayout)viewHolder.layout.getParent()).getOrientation() == LinearLayout.VERTICAL)
{
leftMargin = ((LinearLayout)viewHolder.layout.getParent()).getLeft() +
viewHolder.layout.getLeft() + viewHolder.layout.getWidth()/2;
}
else
{
leftMargin = viewHolder.layout.getLeft() + viewHolder.layout.getWidth()/2;
}
topMargin = viewHolder.layout.getTop();
params = (RelativeLayout.LayoutParams)resizeTop.getLayoutParams();
params.leftMargin = leftMargin - dpToPx(resizeAnchorRadius/2);
params.topMargin = topMargin - dpToPx(resizeAnchorRadius/2);
resizeTop.setLayoutParams(params);