上升动画,缩小动画

上升动画

实现效果mBg2为上升动画,下面布局依然填充下面所有位置

布局


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="people.demo.MainActivity">

    <ImageView
        android:id="@+id/mBg2"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="@color/colorAccent" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/colorPrimary" />

LinearLayout>

代码

//dp改变成像素
float oldHeight = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 330, getActivity().getResources().getDisplayMetrics());

float newHeight = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, getActivity().getResources().getDisplayMetrics());

final ValueAnimator valueAnimator = ValueAnimator.ofFloat(oldHeight, newHeight);

valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float value = (float) valueAnimator.getAnimatedValue();
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) mBg2.getLayoutParams();
layoutParams.height = (int) value;
mBg2.setLayoutParams(layoutParams);
}
});

valueAnimator.setDuration(500);

valueAnimator.start();

你可能感兴趣的:(android,项目坑)