两个按钮动画互换位置 [英] Animated swap position of two buttons

查看:166
本文介绍了两个按钮动画互换位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想交换两个按钮的位置。 我交换code看起来是:

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

我称之为code如下:

I call the code as below:

exchangeButtons(button1, button2);

我的布局看起来如下:

My layout looks as below:

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

当我执行code是会发生什么:

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元素都是衡量呢。试试这个

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

把code在onCreate方法

Put the code in the onCreate method

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆