如何在Android中移动视图?

问题描述 投票:6回答:5

我有一个项目面临两个挑战:

第一:

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

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

第二:

  • 我在RelativeLayout上有两种布局,均具有屏幕宽度和高度(1隐藏在另一个屏幕后面)。我想每次单击按钮时向右移几下即可。

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

使用方法[[.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
提前感谢。
android views android-animation
5个回答
7
投票
WindowManager为每个视图维护至少两个以上的LayoutParams类实例,除了View本身中的一个实例。

检查updateViewLayoutWindowManager方法,尤其是此部分:

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)。


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);


4
投票
最简单的用手指移动视图的方法是使用View类中的setTranslationX和setTranslationY。

http://developer.android.com/reference/android/view/View.html#setTranslationX(float

如果您将api <11作为目标,也可以更改View LayoutParams上的边距以移动。

我相信,在视图上已经具有这些选项的情况下,在onLayout或onDraw函数上移动项目会变得更糟,因为您添加了更多项目和不同的视图,因此手动绘制视图会变得很复杂。


1
投票
您可以使用object animation


0
投票
您可以使用动画来移动视图使用ObjectAnimatior类。如下所示
© www.soinside.com 2019 - 2024. All rights reserved.