如何在Android中移动视图? [英] How to move a view in Android?

查看:31
本文介绍了如何在Android中移动视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,其中有两个挑战:

I have a project where I have 2 challenges:

首先:

  • 将图标移动到手指触摸屏幕的任何位置:

为此,我发现的最佳方法是在视图上使用 .layout() 方法.

For this, the best approach I've found, is to use .layout() method on the view.

第二:

  • 我在 RelativeLayout 上有两个布局,都有屏幕宽度和高度(1 隐藏在另一个后面).每次我点击一个按钮时,我都想将上面的那个向右移动几下.
  • I have two layouts, on a RelativeLayout, both with screen width and height (1 is hidden behind the other). I want to move the one above a few dips to the right every time I click a button.

是否有更好的方法在 Android 上移动视图?

使用方法 .layout() 的缺点是什么?

What could be the disadvantages of using the method .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 maintains at least two more instances of LayoutParams class for each view, besides that one in the View itself.

检查WindowManagerupdateViewLayout方法,特别是这部分:

Check updateViewLayout method of WindowManager, this part in particular:

    view.setLayoutParams(wparams);

    synchronized (this) {
        int index = findViewLocked(view, true);
        ViewRoot root = mRoots[index];
        mParams[index] = wparams;
        root.setLayoutParams(wparams, false);
    }

我相信你可以通过直接调用 layout 来制造一些混乱.请改用 WindowManager.updateViewLayout.它会更慢,但安全(只是 IMO).

I believe that you can make some mess by calling layout directly. Use WindowManager.updateViewLayout instead. It will be slower, but safe (just IMO).

更新

[来自: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);

这篇关于如何在Android中移动视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆