说明:
1)mImageView.getHeight() 图片填充后的高度,即ImageView在容器里的高度
2)mBitmapHeight图片原来的高度
3)animation.getAnimatedFraction() 就是动画运行过程中的变化因子(从0到1)
4)mImageView.requestLayout()表示重新设定ImageView的布局
5)animator.setInterpolator(new OvershootInterpolator(4)) 动画加速器,有快速弹出的效果
代码:
switch (ev.getAction()) {
case MotionEvent.ACTION_UP:
final int startHeight = mImageView.getHeight();
final int endHeight = mBitmapHeight;
ValueAnimator animator = ValueAnimator.ofInt(1);
animator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float fraction = animation.getAnimatedFraction();
int newHeight = (int) (evalute(fraction, startHeight, endHeight) + 0.5f);
mImageView.getLayoutParams().height = newHeight;
mImageView.requestLayout();
}
});
animator.setInterpolator(new OvershootInterpolator(4));
animator.setDuration(500);
animator.start();
}
/**
* 估值器,得到移动后的位置
*
* @param percent
* @param startValue
* @param endValue
* @return
*/
public Float evalute(float percent, Number startValue, Number endValue) {
float start = startValue.floatValue();
return start + percent * (endValue.floatValue() - start);
}