效果图
原本自己也有实现过这个效果,但是写的非常复杂,今天无意看到QQ群里的公开课,写的很简洁,极有功底,特作此记录
public class ZoomHeardListView extends ListView{
private int mImageViewHeight;
private ImageView mImageView;
public ZoomHeardListView(Context context) {
this(context,null);
}
public ZoomHeardListView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public ZoomHeardListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//默认高度
mImageViewHeight = context.getResources().getDimensionPixelSize(R.dimen.size_default_height);
}
//设置图片
public void setImageView(ImageView imageView){
mImageView = imageView;
}
/**
* deltaY - 往下拉过度距离
* deltaY + 往上拉过度距离
* 3.0之后,所有滑动控件都有此监听方法,监听是否拉到头或着尾。
通过设置centerCrop
通过Imageview+scaleType="centerCrop"
*/
@Override
protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
mImageView.getLayoutParams().height=mImageView.getHeight()-deltaY;
mImageView.requestLayout();
return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, maxOverScrollY, isTouchEvent);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
ViewGroup header= (ViewGroup) mImageView.getParent();
int deltaY = header.getTop();
if(mImageView.getHeight()>mImageViewHeight){
mImageView.getLayoutParams().height=mImageView.getHeight()+deltaY;
//由于滑出去了一截,所以要让header父容器重新摆放
header.layout(header.getLeft(),0,header.getRight(),header.getHeight());
mImageView.requestLayout();
}
super.onScrollChanged(l, t, oldl, oldt);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if(ev.getAction()==MotionEvent.ACTION_UP){
ResetAnimation resetAnimation=new ResetAnimation();
resetAnimation.setDuration(700);
mImageView.startAnimation(resetAnimation);
}
return super.onTouchEvent(ev);
}
public class ResetAnimation extends Animation {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
// interpolatedTime 0.0-1.0 执行的百分比
mImageView.getLayoutParams().height= (int) (mImageView.getHeight()-((mImageView.getHeight()-mImageViewHeight)*interpolatedTime));
mImageView.requestLayout();
super.applyTransformation(interpolatedTime, t);
}
}
}
Activity中
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
qqScrollview = (ZoomHeardListView) findViewById(R.id.lsitview);
qqScrollview.setAdapter(new ArrayAdapter<String>(this,R.layout.list_itme,new String[]{"哈哈哈 ","小磊子","Android","iOS","教程"}));
View inflate = LayoutInflater.from(this).inflate(R.layout.heardview, null);
ImageView heardView= (ImageView) inflate.findViewById(R.id.herdimage);
qqScrollview.addHeaderView(inflate);
qqScrollview.setImageView(heardView);
}