Edittext更改宽度与动画

时间:2014-02-04 12:21:26

标签: android xml android-edittext android-animation

我想创建一个在其父级左侧对齐的edittext,当用户单击它时,edittext的width将增加到右侧。这是我使用的代码。但是当动画结束时,edittext宽度会变成第一个大小。
任何人都可以帮助我。?是否有任何解决方案可以在动画中将“fillparent”设置为宽度的最终大小?

Animation scaleAnimation = new ScaleAnimation(0, -500, 1, 1);
scaleAnimation.setDuration(750);
editText.startAnimation(scaleAnimation);

我在动画开始之前添加了scaleAnimation.setFillAfter(true);,但我得到了这个; enter image description here

在750ms之后,edittext将转到第一个宽度。

4 个答案:

答案 0 :(得分:5)

这绝对适合您。

  

这样做的动态方式:

ScaleAnimation animate = new ScaleAnimation(1, 2, 1, 1, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
animate.setDuration(700);
animate.setFillAfter(true);
target.startAnimation(animate);
  

使用XML的方式

scale.xml (将其放在res / anim文件夹中)

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true">

    <scale
        android:duration="400"
        android:fillAfter="true"        
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="0%"
        android:pivotY="0%"
        android:toXScale="2.0"
        android:toYScale="1.0" >
    </scale>

</set>

Java代码:

Animation anim = AnimationUtils.loadAnimation(Anims.this, R.anim.scale);
mEdittext.startAnimation(anim);

答案 1 :(得分:2)

这样做

Animation scaleAnimation = new ScaleAnimation(0, -500, 1, 1);
    scaleAnimation.setDuration(750);
    scaleAnimation.setFillAfter(true);
    editText.startAnimation(scaleAnimation);

答案 2 :(得分:-1)

使用

scaleAnimation.setFillAfter(true);

或使用ObjectAnimator:

ObjectAnimator animWidth = ObjectAnimator.ofInt(your_view, "width", startWidth,endWith);
animWidth.setDuration(yourDuration);
animWidth.start();

答案 3 :(得分:-1)

尝试一下这个。

动画scaleAnimation = new ScaleAnimation(0,-500,1,1);

         scaleAnimation.setDuration(750);

        scaleAnimation.setAnimationListener(new AnimationListener() {

                        @Override
                        public void onAnimationStart(Animation animation) {}

                        @Override
                        public void onAnimationRepeat(Animation animation) {}

                        @Override
                        public void onAnimationEnd(Animation animation) {
                          //Set your editext width to match parent using layout params
                        }
                    });
        editText.startAnimation(scaleAnimation);
相关问题