如何在android中缩小和移动视图?

时间:2015-02-09 06:55:41

标签: android android-animation

我想缩小并将相对布局设置为右侧,如下面的图像,使用translate动画将布局移动到右侧(转换动画工作正常),但不是能够缩小视图。无法实现形象。任何人都知道帮助我解决这个问题。

Image

每当在相对布局中单击按钮(三线按钮)时,请缩小视图并将其向右移动。

翻译动画代码

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

<translate
    android:fromXDelta="0"
    android:toXDelta="90%" />

</set>

Java代码

bt1 =  (Button)findViewById(R.id.button1);
bt1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            if (view_moved != 1) {
              Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.translate);
              anim_lay.startAnimation(animation);
              view_moved = 1;
        }
    }
});

1 个答案:

答案 0 :(得分:0)

视图动画不会重新取样可绘制的视图,只会对其应用缩放矩阵,因此它可能会变得模糊或有伪影。使用对象/值动画制作器来设置布局高度/宽度的动画,以便在每次更改时重绘它。

    ValueAnimator widthAnim = ValueAnimator.ofInt(viewGroup.getMeasuredWidth(), desiredWidth);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int val = (Integer) valueAnimator.getAnimatedValue();
                ViewGroup.LayoutParams layoutParams = viewGroup.getLayoutParams();
                layoutParams.width = val;
                viewGroup.setLayoutParams(layoutParams);
            }
        });

    ValueAnimator heightAnim = ValueAnimator.ofInt(viewGroup.getMeasuredHeight(), desiredHeight);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int val = (Integer) valueAnimator.getAnimatedValue();
                ViewGroup.LayoutParams layoutParams = viewGroup.getLayoutParams();
                layoutParams.height = val;
                viewGroup.setLayoutParams(layoutParams);
            }
        });
   AnimatorSet scaleAnimation = new AnimatorSet();
   scaleAnimation.playTogether(widthAnim, heighthAnim);
   scaleAnimation.setDuration(500).start();

其中viewGroup是您的RelativeLayout