【问题标题】:How to move a view in Android?如何在 Android 中移动视图?
【发布时间】:2012-11-05 02:27:48
【问题描述】:

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

第一:

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

为此,我发现的最佳方法是在视图上使用 .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


    【解决方案1】:

    除了View 本身中的一个之外,WindowManager 还为每个视图维护至少两个LayoutParams 类的实例。

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

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

    【讨论】:

    • 你建议我使用类似stackoverflow.com/a/11188273/327011 的东西吗?
    • 是的,看起来就像我的意思。在为 Android 2.x 实现拖放操作时,我使用了类似的技术。
    【解决方案2】:

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

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

    如果您的目标是 api

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

    【讨论】:

    • 这看起来很不错.. 但是仅适用于 API 级别 11 及以上... 11 以下呢?
    • 11 之前我认为最好使用边距进行翻译,为此您需要从视图中获取 LayoutParams,更改它们并将其设置回视图。您可能需要调用 invalidate 来更新视图。
    • 你认为这比上面安德烈给出的答案更好吗?
    • 旧版本可以使用 Nineoldandroids jar 文件。它具有 ViewHelper.setTransalationX() 和 Y() 方法,其工作方式类似于 view.setTranslationX() 和 Y() 的工作方式。
    【解决方案3】:

    你可以使用object animation

    /**
     * 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();
    
     }
    
    }
    

    【讨论】:

    • 这是一个不错的选择......但它是最近的:API 11。
    • @NeTeInStEiN,然后尝试使用九个旧的 android 库 - “Android 库,用于在所有版本的平台上使用 Honeycomb (Android 3.0) 动画 API 回到 1.0!”
    【解决方案4】:
    【解决方案5】:

    您可以使用动画移动视图 使用ObjectAnimatior 类。如下所示

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

    视图将随您决定

    【讨论】:

    • 第一次提到的类名有一个小错别字。
    猜你喜欢
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-12
    • 1970-01-01
    • 1970-01-01
    • 2016-02-16
    相关资源
    最近更新 更多