【问题标题】:In android How to Translate to an absolute Point in Screen在android中如何转换为屏幕中的绝对点
【发布时间】:2015-01-30 17:35:56
【问题描述】:

我有一个 Imageview 和一个 ImageButton,并希望 Imageview 移动到 imagebutton 的位置。

Imageview 及其在屏幕上的位置

myAnimation1.getLocationInWindow(imagePos);

图像按钮及其在屏幕上的位置

ImageButton.getLocationInWindow(buttonPos);

将 Imageview 翻译或移动到我尝试过的 Imagebutton

Animation move = new TranslateAnimation(Animation.ABSOLUTE,imagePos[0],Animation.ABSOLUTE,buttonPos[0],Animation.ABSOLUTE,imagePos[1],Animation.ABSOLUTE,buttonPos[1]);

但它不起作用。屏幕上看不到翻译。任何帮助将不胜感激!提前谢谢你。

【问题讨论】:

    标签: android translate-animation


    【解决方案1】:

    首先,如果您没有从 getLocationInWindow 获得正确的坐标,请尝试像这样获取坐标:

    ImageButton.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
                public void onGlobalLayout() {
    
                  ImageButton.getViewTreeObserver().removeGlobalOnLayoutListener(this);
    
                  int[] locations = new int[2];
                  ImageButton.getLocationOnScreen(locations);
                  // x is locations[0];
                  // y is locations[1];
    
                }
            });
    

    【讨论】:

    • 谢谢..我会试试这个。另外我想知道 Animation.ABSOLUTE 是否确实将图像从一个绝对位置移动到另一个正确位置?
    • 我还没试过,但我觉得可以! (stackoverflow.com/questions/16727647/…)
    【解决方案2】:

    你没有设置持续时间,甚至没有开始动画。

    move.setDuration(1000);
    imageView.startAnimation(move);
    

    然后尝试使用动画的属性来获得想要的结果

    【讨论】:

    • 我确实设置了持续时间并开始动画..我只是没有在这里显示它因为我只想粘贴相关代码。
    • 你用什么方法调用这个动画?因为在 onCreate 方法中,您的视图不会被绘制到窗口..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-22
    • 1970-01-01
    • 2012-07-06
    相关资源
    最近更新 更多