首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更改ImageButton LayoutParams onAnimationEnd

更改ImageButton LayoutParams onAnimationEnd
EN

Stack Overflow用户
提问于 2013-06-30 23:09:42
回答 1查看 649关注 0票数 0

我需要一个内部有三个图像按钮的edittext,我通过使用相对布局并将图像按钮放置在EditText上成功地创建了它。类似于:

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

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

相关的XML:

代码语言:javascript
复制
<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" />

以及相关的代码片段:

代码语言:javascript
复制
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);
                    }
                }
            }); 

和:

代码语言:javascript
复制
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 level 10。我也尝试过在EditText上使用onClickListener,但是1.第一次点击edittext时,只有第二次点击会被检测为点击;2.一旦edittext失去焦点,它将不允许我切换回按钮。

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2013-11-14 21:26:31

首先,向LayoutParams添加其他规则不会删除当前规则,因此只剩下:

代码语言:javascript
复制
<ImageButton
...
    android:layout_alignRight="@+id/commentBox"
    android:layout_alignLeft="@+id/commentBox"
...
 />

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

其次,您可能希望在更改LayoutParams之后调用view.requestLayout();

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17391772

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档