我有一个项目,我有两个挑战:
第一:
>将图标移动到手指触摸屏幕的位置:
为此,我发现的最佳方法是在视图上使用.layout()方法.
第二:
>我在RelativeLayout上有两个布局,两个都有屏幕宽度和高度(1隐藏在另一个后面).每当我点击一个按钮时,我想将一个上方的一个向右移动.
使用方法.layout()可能有什么缺点?
public void layout (int l,int t,int r,int b) Since: API Level 1 Assign a size and position to a view and all of its descendants Parameters: l Left position,relative to parent t Top position,relative to parent r Right position,relative to parent b Bottom position,relative to parent
提前致谢.
解决方法
WindowManager为每个视图维护至少两个LayoutParams类实例,除了View本身之外的那个实例.
检查WindowManager的updateViewLayout方法,特别是这部分:
view.setLayoutParams(wparams); synchronized (this) { int index = findViewLocked(view,true); ViewRoot root = mRoots[index]; mParams[index] = wparams; root.setLayoutParams(wparams,false); }
我相信你可以通过直接调用布局来制造一些混乱.请改用WindowManager.updateViewLayout.它会更慢,但更安全(只是IMO).
UPDATE
[来自:https://stackoverflow.com/a/11188273/327011]
WindowManager windowsManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE) WindowManager.LayoutParams windowParams = new WindowManager.LayoutParams(); windowParams.x = <new X coord>; windowParams.y = <new Y coord> windowParams.height = myImageView.getHeight(); windowParams.width = myImageView.getWidth(); windowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS; windowParams.format = PixelFormat.TRANSLUCENT; windowParams.windowAnimations = 0; windowManager.updateViewLayout(myImageView,windowParams);