8

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

第一的:

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

为此,我发现最好的方法是.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  

提前致谢。

4

5 回答 5

8

WindowManager 为每个视图维护至少两个LayoutParams类的实例,除了它View本身。

的检查updateViewLayout方法WindowManager,特别是这部分:

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


更新

[来自: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);
于 2012-11-16T11:56:38.507 回答
5

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

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

如果您的目标是 api < 11,您还可以更改 View LayoutParams 上的边距以移动。

我认为在 onLayout 或 onDraw 函数上移动项目会更糟,因为视图已经具有上面的这些选项,并且随着您添加更多项目和不同视图,手动绘制视图可能会变得复杂。

于 2012-11-23T16:58:21.850 回答
1

您可以使用对象动画

/**
 * The java class is used to move the widgets to the region of the touch point from its original position
 * 
 * @author gfernandes
 *
 */
public class animate {

/**
 * Variable declaration
 * 
 */
    private ObjectAnimator animation1;
    private ObjectAnimator animation2;


/**
 * Function animate: Animates the widget Eg:Button from its position to the 
 *                   position of the touch point.
 *
 * @param[in] -The coordinates of the touch point (x, y)
 *            -The coordinates of the widget position (valx, valy)
 *            -The widget object id
 *                      
 */

public void animategroup1(int x, int y, Object val, int valx, int valy ){

    System.out.println("animategroup1 entered here");
    System.out.println("x:"+x);
    System.out.println("y"+y);
    System.out.println("valx"+valx);
    System.out.println("valy"+valy);

    animation1 = ObjectAnimator.ofFloat(val, "x", valx, x);

    animation2 = ObjectAnimator.ofFloat(val, "y", valy, y);

    AnimatorSet set = new AnimatorSet();

    set.playTogether(animation1, animation2);

    set.start();

 }

}
于 2013-02-28T08:47:04.910 回答
0

您可以使用动画使用ObjectAnimatior类移动视图。像下面

ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationY or translationX" , number in Float)
animator.setDuration(miliseconds).start();

视图将随您决定

于 2019-11-03T17:09:02.347 回答