直接上代码:
public class AnimUtils {
private final static int DURATION = 450;
public static void zoom(View view, boolean isZoomAnim) {
if (isZoomAnim) {
AnimatorSet set = new AnimatorSet();
set.playTogether(
ObjectAnimator.ofFloat(view, "scaleX", 1f, 1.12f),
ObjectAnimator.ofFloat(view, "scaleY", 1f, 1.12f)
);
set.setDuration(DURATION);
set.start();
}
}
public static void disZoom(View view, boolean isZoomAnim) {
if (isZoomAnim) {
AnimatorSet set = new AnimatorSet();
set.playTogether(
ObjectAnimator.ofFloat(view, "scaleX", 1.12f, 1f),
ObjectAnimator.ofFloat(view, "scaleY", 1.12f, 1f)
);
set.setDuration(DURATION);
set.start();
}
}
/**
* 箭头旋转动画
*
* @param arrow
* @param flag
*/
public static void rotateArrow(ImageView arrow, boolean flag) {
float pivotX = arrow.getWidth() / 2f;
float pivotY = arrow.getHeight() / 2f;
// flag为true则向上
float fromDegrees = flag ? 180f : 180f;
float toDegrees = flag ? 360f : 360f;
//旋转动画效果 参数值 旋转的开始角度 旋转的结束角度 pivotX x轴伸缩值
RotateAnimation animation = new RotateAnimation(fromDegrees, toDegrees,
pivotX, pivotY);
//该方法用于设置动画的持续时间,以毫秒为单位
animation.setDuration(350);
//启动动画
arrow.startAnimation(animation);
}
public static void expand(final View view) {
view.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
final int viewHeight = view.getMeasuredHeight();
view.getLayoutParams().height = 0;
view.setVisibility(View.VISIBLE);
Animation animation = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
if (interpolatedTime == 1) {
view.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
} else {
view.getLayoutParams().height = (int) (viewHeight * interpolatedTime);
}
view.requestLayout();
}
};
animation.setDuration(300);
animation.setInterpolator(new FastOutLinearInInterpolator());
view.startAnimation(animation);
}
public static void collapse(final View view) {
//view.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
final int viewHeight = view.getMeasuredHeight();
Animation animation = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
if (interpolatedTime == 1) {
view.setVisibility(View.GONE);
} else {
view.getLayoutParams().height = viewHeight - (int) (viewHeight * interpolatedTime);
view.requestLayout();
}
}
};
animation.setDuration(300);
animation.setInterpolator(new FastOutLinearInInterpolator());
view.startAnimation(animation);
}
}
代码中调用方式:
AnimUtils.collapse(rv_note_drawer_list)
AnimUtils.expand(rv_note_drawer_list)