更改ImageButton LayoutParams onAnimationEnd

时间:2013-06-30 15:09:42

标签: android android-animation android-view android-button

我需要一个带有三个图像按钮的EditText,我设法通过使用相对布局创建并基本上将图像按钮放在edittext上。就像是: layout

我正在尝试设置EditText聚焦时按钮IB1和IB2的交换动画,并在失去焦点时再次交换它们。我设法做到了这一点,但问题出现在我尝试更新按钮LayoutParams onAnimationEnd时(否则像素会改变屏幕上的位置,但底层视图仍处于其原始位置)。

问题:当我更新似乎再次触发EditText焦点事件的ImageButton layoutParams时 - 一旦点击edittext小部件,按钮就会开始不断地来回移动。

相关的XML:

<RelativeLayout
   android:id="@+id/commentView" 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content">
   <EditText
   android:id="@+id/commentBox"
   android:maxLines="1"
   android:scrollHorizontally="true"
   android:paddingTop="10dp"
   android:paddingBottom="10dp"
   android:inputType="text" 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:imeOptions="actionDone"
   android:paddingRight="100dp"
   android:paddingLeft="55dp"
   android:background="@drawable/comment"
   android:layout_marginLeft="2dp"
   android:layout_marginRight="2dp"
   android:hint="Your comment..."
   />
   <ImageButton
    android:id="@+id/share"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/commentBox"
    android:layout_marginRight="2dp"
    android:src="@drawable/sharebutton"
    android:background="@null"
    android:layout_centerVertical="true" />
   <ImageButton 
    android:id="@+id/like"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/commentBox"
    android:src="@drawable/likebutton"
    android:background="@null"
    android:layout_marginLeft="4dp"
    android:layout_centerVertical="true" />
   <ImageButton 
    android:id="@+id/commentimagebutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@+id/share"
    android:src="@drawable/commentbutton"
    android:background="@null"
    android:layout_marginRight="4dp"
    android:layout_centerVertical="true" />

相关代码片段:

commentBox.setOnFocusChangeListener(new OnFocusChangeListener() {

                @Override
                public void onFocusChange(View view, boolean hasFocus) {
                    RelativeLayout parent = (RelativeLayout) view.getParent();
                    ImageButton commentView = (ImageButton) parent.findViewById(R.id.commentimagebutton);
                    ImageButton likeView = (ImageButton) parent.findViewById(R.id.like);
                    if (hasFocus) {
                        swapViews(likeView, commentView);
                    }
                }
            }); 

private void swapViews(final ImageButton firstView, final ImageButton secondView)
{
    //get view positions and calculate distance
    int secondPosition[] = new int[2];
    int firstPosition[] = new int[2];
    secondView.getLocationOnScreen(secondPosition);
    firstView.getLocationOnScreen(firstPosition);
    int moveDistance = Math.abs(secondPosition[0]-firstPosition[0]);

    //get LayoutParams
    final RelativeLayout.LayoutParams firstViewLayout = (RelativeLayout.LayoutParams) firstView.getLayoutParams();
    final RelativeLayout.LayoutParams secondViewLayout = (RelativeLayout.LayoutParams) secondView.getLayoutParams();

    //set up animations
    TranslateAnimation moveLeft = new TranslateAnimation( 0, -moveDistance, 0, 0);
    moveLeft.setDuration(1000);
    moveLeft.setFillAfter(true);

    TranslateAnimation moveRight = new TranslateAnimation( 0, moveDistance, 0, 0);
    moveRight.setDuration(1000);
    moveRight.setFillAfter(true);

    if (secondPosition[0] > firstPosition[0]) {
        moveLeft.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {}

            @Override
            public void onAnimationRepeat(Animation animation) {}

            @Override
            public void onAnimationEnd(Animation animation) {
                //change positions
                secondView.clearAnimation();
                secondViewLayout.addRule(RelativeLayout.ALIGN_LEFT, R.id.commentBox);
                secondViewLayout.addRule(RelativeLayout.LEFT_OF, 0);
                secondView.setLayoutParams(secondViewLayout);
            }
        });

        moveRight.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {}

            @Override
            public void onAnimationRepeat(Animation animation) {}

            @Override
            public void onAnimationEnd(Animation animation) {
                //change positions
                firstView.clearAnimation();
                firstViewLayout.addRule(RelativeLayout.ALIGN_LEFT, 0);
                firstViewLayout.addRule(RelativeLayout.LEFT_OF, R.id.share);
                firstView.setLayoutParams(firstViewLayout);
            }
        });

        //animate
        secondView.startAnimation(moveLeft);
        firstView.startAnimation(moveRight);
    } else {
        moveLeft.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {}

            @Override
            public void onAnimationRepeat(Animation animation) {}

            @Override
            public void onAnimationEnd(Animation animation) {
                //change positions
                firstView.clearAnimation();
                firstViewLayout.addRule(RelativeLayout.ALIGN_LEFT, R.id.commentBox);
                firstViewLayout.addRule(RelativeLayout.LEFT_OF, 0);
                firstView.setLayoutParams(firstViewLayout);
            }
        });

        moveRight.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {}

            @Override
            public void onAnimationRepeat(Animation animation) {}

            @Override
            public void onAnimationEnd(Animation animation) {
                //change positions
                secondView.clearAnimation();
                secondViewLayout.addRule(RelativeLayout.ALIGN_LEFT, 0);
                secondViewLayout.addRule(RelativeLayout.LEFT_OF, R.id.share);
                secondView.setLayoutParams(secondViewLayout);
            }
        });

        //animate
        secondView.startAnimation(moveRight);
        firstView.startAnimation(moveLeft);
    }
}

注意:我使用的是API级别10.我也尝试在EditText上使用onClickListener,但是1.首先点击一个edittext聚焦它,只有第二个被检测为点击而且2.它不允许我一旦edittext失去焦点,就将按钮交换回来。

谢谢!

1 个答案:

答案 0 :(得分:0)

首先,向LayoutParams添加其他规则不会删除当前规则,因此您将离开:

<ImageButton
...
    android:layout_alignRight="@+id/commentBox"
    android:layout_alignLeft="@+id/commentBox"
...
 />

通过LayoutParams.removeRule()删除旧规则(在API 17中添加),或尝试创建新的LayoutParams并查看是否有效。

其次,您可能希望在更改view.requestLayout();后致电LayoutParams