android 自定义listview实现头部试图放大缩小

1.首先自定义类继承listview重写构造方法,和overScrollBy()方法

2.提供一个公共的方法用于获取子view的高度,添加全局监听方法

getViewTreeObserver().addOnGlobalLayoutListener()用于获取子view的高度,只获取一
getViewTreeObserver().removeOnGlobalLayoutListener(this);//删除全局监听
getDrawable().getIntrinsicHeight()//用于获取图片的高度

3.重写overScroolBy()方法

对其参数进行判断

if (deltaY < 0 && isTouchEvent)//我们将其认为是滑动状态
ViewGroup.LayoutParams params = mLv.getLayoutParams(); //获取布局参数
params.height = newHeght;  //对高重新赋值
requestLayout(); //重新绘制

4.重写onTouchEvent方法,对我们手指抬起来进行一个监听

为了使回弹有一个更好的效果,我给他设置了一个属性动画

final ValueAnimator animator = ObjectAnimator.ofInt(mLv.getHeight(), iv_height);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        //过渡至
        int animatedValue = (int) animation.getAnimatedValue();
        mLv.getLayoutParams().height = animatedValue;
        requestLayout();
    }
});
animator.setDuration(500);
animator.start();

猜你喜欢

转载自blog.csdn.net/Android_Code/article/details/81142867