【问题标题】:Animated swap position of two buttons两个按钮的动画交换位置
【发布时间】:2009-11-06 10:54:22
【问题描述】:

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

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>

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

按钮并没有交换它们的位置,它们只是消失了一段时间[可能是 500 毫秒],然后又像原来一样重新出现。

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

【问题讨论】:

    标签: android animation button swap


    【解决方案1】:

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

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

    将代码放在onCreate方法中

    【讨论】:

      【解决方案2】:

      Android 的 TranslateAnimation 不会重新定位您的组件,它只是为您想要执行的转换设置动画 - 之后,您可以更改组件的布局参数(看看这个 post)。

      附:您可以直接使用平移动画,而无需使用全新的动画集。另一件好事是在布局膨胀时创建动画并覆盖onSizeChanged(int, int, int, int) 以使用新尺寸重新创建它们。

      你使用的是什么布局?

      【讨论】:

        【解决方案3】:
         b4.setOnClickListener {
                try {
        
        
                    var arswap = arrayListOf<Button>(b1, b2, b3);
                    var randomPos: Int = 0;
        
        
                    fun swap(b1: Button, b2: Button) {
                        var tempPosition = Point(b1.x.toInt(), b1.y.toInt())
                        b1.x = b2.x;
                        b1.y = b2.y;
        
                        b2.x = tempPosition.x.toFloat();
                        b2.y = tempPosition.y.toFloat();
                    }
        
                    var totalItems: Int = 3; // total number of items
        
                    for (i in 0..totalItems/3 ) {
        
                        randomPos = ((Math.random() * arswap.size).toInt());
                        var randomItem =  ((Math.random() * ((totalItems / 2) - 1)) + ((totalItems / 2) + 1));
        
        
                        swap(arswap[randomPos], arswap[randomItem.toInt()])
                    }
                }catch (e:Exception){
        
                }
        
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-07-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-07-08
          • 2015-04-10
          • 1970-01-01
          相关资源
          最近更新 更多