展开/折叠动画Android半视图

时间:2019-04-03 10:51:05

标签: android animation android-animation

我想扩展/折叠ImageView,但要从50%的图片开始以100%的比例扩展,然后再收缩到50%的水平。

我已经看过一些关于SO的流行问题和答案,但是我没有找到如何只管理一半的问题。我还想修改视图的高度,而不是宽度。

我尝试过的事情:

public static void expand(final View v) {
    v.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    final int targtetHeight = v.getMeasuredHeight();

    v.getLayoutParams().height = 0;
    v.setVisibility(View.VISIBLE);
    Animation a = new Animation()
    {
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            v.getLayoutParams().height = interpolatedTime == 1
                    ? ViewGroup.LayoutParams.WRAP_CONTENT
                    : (int)(targtetHeight * interpolatedTime);
            v.requestLayout();
        }

        @Override
        public boolean willChangeBounds() {
            return true;
        }
    };

    a.setDuration((int)(targtetHeight / v.getContext().getResources().getDisplayMetrics().density));
    v.startAnimation(a);
}

public static void collapse(final View v) {
    final int initialHeight = v.getMeasuredHeight();

    Animation a = new Animation() {
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            if (interpolatedTime == 1) {
                v.setVisibility(View.VISIBLE);
            } else {
                v.getLayoutParams().height = initialHeight - (int) (initialHeight * interpolatedTime);
                v.requestLayout();
            }
        }

        @Override
        public boolean willChangeBounds() {
            return true;
        }
    };

    a.setDuration((int) (initialHeight / v.getContext().getResources().getDisplayMetrics().density));
    v.startAnimation(a);
}

正如我所说的,这不是我想要的,因为它完全消失了,并且改变了宽度。

我也尝试过此片段,但是没有动画:

mImageDrawable = (ClipDrawable) pic.getDrawable();
mImageDrawable.setLevel(5000);//use set level to expand or collapse manually but no animation.

剪辑:

    <?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
    android:clipOrientation="vertical"
    android:drawable="@drawable/test_pic"
    android:gravity="top" />

1 个答案:

答案 0 :(得分:2)

使用支持包(androidx)中提供的Transition API。只需调用TransitionManager.beginDelayedTransition,然后更改视线高度即可。 TransitionManager将处理此更改,并将提供过渡,该过渡将更改带有动画的imageView。

enter image description here

scaleType中的

ImageViewcenterCrop,这就是为什么图像在折叠和展开时会缩放的原因。不幸的是,没有“填充宽度和作物底部” scaleType,因此,我认为可以通过scaleType = matrix 完成。

import androidx.appcompat.app.AppCompatActivity;
import androidx.transition.TransitionManager;

public class MainActivity extends AppCompatActivity {

  private ImageView image;
  private ViewGroup parent;
  boolean collapse = true;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    image = findViewById(R.id.image);
    parent = findViewById(R.id.parent);

    findViewById(R.id.btn).setOnClickListener(view -> {
        collapse = !collapse;
        collapse();
    });
  }

  private void collapse() {
    TransitionManager.beginDelayedTransition(parent);
    //change layout params
    int height = image.getHeight();
    LayoutParams layoutParams = image.getLayoutParams();
    layoutParams.height = !collapse ? height / 2 : height * 2;
    image.requestLayout();
  }
}

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<Button
    android:id="@+id/btn"
    android:text="start"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<ImageView
    android:id="@+id/image"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:scaleType="centerCrop"
    android:src="@drawable/qwe" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="random text"
    android:layout_margin="8dp"/>
</LinearLayout>

更新:

beginDelayedTransition(ViewGroup, Transtion)方法。 beginDelayedTransition(ViewGroup)默认使用AutoTransition作为过渡。 因此,如果您需要转换的开始/结束句柄,可以这样做:

    AutoTransition transition = new AutoTransition();
    transition.addListener(new TransitionListenerAdapter(){
        @Override
        public void onTransitionStart(@NonNull Transition transition) {
            //TODO
        }
        @Override
        public void onTransitionEnd(@NonNull Transition transition) {
            //TODO 
        }
    });
    TransitionManager.beginDelayedTransition(parent, transition);