手记

也是模仿头部刷新的动画效果

// 允许从外部传递视差效果的控件进来

public void setParallaxImageView(ImageView parallaxImageView) {

this.parallaxImageView = parallaxImageView;

Drawable drawable = parallaxImageView.getDrawable();

maxHeight = drawable.getIntrinsicHeight();// 获取图片的最大高度

originH = parallaxImageView.getHeight();// 获取图片的起始高度

System.out.println("ParallaxListView.setParallaxImageView,originH="+originH);

}

获取资源高度

@Override

public boolean onTouchEvent(MotionEvent ev) {

switch (ev.getAction()) {

case MotionEvent.ACTION_UP:

// 将图片从当前高度弹回最初的高度

final int currentH = parallaxImageView.getHeight();

// 动态生成一个值的变化

ValueAnimator animator = ValueAnimator.ofFloat(1);

animator.addUpdateListener(new AnimatorUpdateListener() {

@Override

public void onAnimationUpdate(ValueAnimator value) {

System.out.println("ParallaxListView.onAnimationUpdate,time="+value.getCurrentPlayTime()+";Fraction="+value.getAnimatedFraction()+";value="+value.getAnimatedValue());

int newHeight = evaluate(value.getAnimatedFraction(), currentH, originH);

parallaxImageView.getLayoutParams().height = newHeight;

parallaxImageView.requestLayout();

}

});

animator.setDuration(200);

animator.setInterpolator(new OvershootInterpolator(4));

animator.start();

break;

default:

break;

}

return super.onTouchEvent(ev);// 一定要使用super来返回,因为列表的滚动要由ListView处理

}

处理事件,当起来的时候,不断刷新图片的高度,

animator.setInterpolator(new OvershootInterpolator(4));

这个是可以设置回弹的次数

@Override

// 当列表被滑动到两端尽头的时候被调用

// deltaY 两次滑动间的变化大小

protected boolean overScrollBy(int deltaX, int deltaY, int scrollX,

int scrollY, int scrollRangeX, int scrollRangeY,

int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {

System.out.println("deltaY="+deltaY+";scrollY="+scrollY+";scrollRangeY="+scrollRangeY+";maxOverScrollY="+maxOverScrollY+";isTouchEvent="+isTouchEvent);

if (deltaY < 0 && isTouchEvent) {

int newHeight = parallaxImageView.getHeight() + Math.abs(deltaY / 2);

if (newHeight < maxHeight) {

parallaxImageView.getLayoutParams().height = newHeight;

parallaxImageView.requestLayout();

}

}

return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX,

scrollRangeY, maxOverScrollX, maxOverScrollY, isTouchEvent);

}

当超过一定值的时候调用

原文链接:http://www.apkbus.com/blog-880881-63457.html

0人推荐
随时随地看视频
慕课网APP