两个按钮的动画交换位置

编程入门 行业动态 更新时间:2024-10-11 19:22:58
本文介绍了两个按钮的动画交换位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在尝试交换两个按钮的位置.我的交换代码看起来是:

I am trying to swap the position of two buttons. My swapping code looks is:

private void exchangeButtons(Button btn1, Button btn2) {
    // Create the animation set
    AnimationSet exchangeAnimation = new AnimationSet(true);
    TranslateAnimation translate = new TranslateAnimation(
        Animation.RELATIVE_TO_SELF, btn2.getLeft(),
        Animation.RELATIVE_TO_SELF, btn1.getLeft(),
        Animation.RELATIVE_TO_SELF, btn2.getRight(),
        Animation.RELATIVE_TO_SELF, btn1.getRight());
    translate.setDuration(500);
    exchangeAnimation.addAnimation(translate);
    //int fromX = btn1.getLeft();
    //int fromY = btn1.getRight();
    //int toX = btn2.getLeft();
    //int toY = btn2.getRight();
    Log.d("ArrangeMe", 
        "view1 pos:" + btn1.getLeft() + ", 
        " +btn1.getRight() + "view2 pos:" + 
        btn2.getLeft() + ", " + btn2.getRight());
    AnimationSet exchangeAnimation1 = new AnimationSet(true);
    TranslateAnimation translate1 = new TranslateAnimation( 
        Animation.RELATIVE_TO_SELF, btn1.getLeft(),
        Animation.RELATIVE_TO_SELF, btn2.getLeft(),
        Animation.RELATIVE_TO_SELF, btn1.getRight(),
        Animation.RELATIVE_TO_SELF, btn2.getRight());
    translate1.setDuration(500);
    exchangeAnimation1.addAnimation(translate1);
    // EXECUTE btn1.startAnimation(exchangeAnimation);
    btn2.startAnimation(exchangeAnimation1);
}

我调用代码如下:

exchangeButtons(button1, button2);

我的布局如下:

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal">

    <Button 
        android:text="One" 
        android:id="@+id/button1" 
        android:layout_height="70px" 
        android:layout_width="70px" 
        android:layout_weight="1">
    </Button>
    <Button 
        android:text="Two" 
        android:id="@+id/button2" 
        android:layout_height="70px" 
        android:layout_width="70px" 
        android:layout_weight="1">
    </Button>
</LinearLayout>

当我执行代码时会发生什么:

What happens when I execute the code is:

按钮不会交换位置,而是会消失一段时间[可能是 500 毫秒],然后又像原来一样重新出现.

Instead of the buttons exchanging their positions, they just disappear for sometime[may be 500 ms] and reappear as they were originally.

如何解决这个问题?它会在设备中正常工作吗?

How to resolve this problem ? Will it work properly in device ?

推荐答案

您不会在 onCreate 中获得 getRight 值,因为 UI 尚未在屏幕上绘制,因此还没有测量任何 UI 元素.试试这个

You wont get the getRight value in onCreate because the UI hasn't been drawn on the screen so none of the UI elements are measure yet. Try this

ViewTreeObserver vto = btn1.getViewTreeObserver();  
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {  
    @Override  
    public void onGlobalLayout() {  
        btn1.getViewTreeObserver().removeGlobalOnLayoutListener(this);  
        int left = btn1.getLeft(); 
    }  
});

将代码放在onCreate方法中

Put the code in the onCreate method

这篇关于两个按钮的动画交换位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-21 00:52:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/993852.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:按钮   位置   两个   动画

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!