美文网首页
视差效果

视差效果

作者: 起嚸_ | 来源:发表于2019-02-21 20:23 被阅读0次
    public class MyListView extends ListView {
        private ImageView imageView;
        private int height;
        private int maxHeight;
    
        public MyListView(Context context) {
            this(context,null);
        }
        public MyListView(Context context, AttributeSet attrs) {
            this(context, attrs ,0);
        }
        public MyListView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
        /**
         * 在listView滑动到头的时候执行,可以获取到继续滑动的距离和方向
         * @param deltaY 继续滑动y方向的距离
         * @param maxOverScrollY y方向最大可以滚动的距离
         * @param isTouchEvent true表示手指拖动滑动 false表示fling靠惯性滑动
         * @return
         */
        @Override
        protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
            if(deltaY < 0 && isTouchEvent){
                int newHeight = imageView.getHeight() - deltaY /3;
                if(newHeight > maxHeight) newHeight = maxHeight;
                imageView.getLayoutParams().height = newHeight;
                imageView.requestLayout();
            }
            return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, maxOverScrollY, isTouchEvent);
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent ev) {
            if(ev.getAction() == MotionEvent.ACTION_UP){
                ValueAnimator animator = ValueAnimator.ofInt(imageView.getHeight(),height);
                animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        imageView.getLayoutParams().height = (int) animation.getAnimatedValue();
                        imageView.requestLayout();
                    }
                });
                animator.setInterpolator(new OvershootInterpolator(5));
                animator.setDuration(350);
                animator.start();
            }
            return super.onTouchEvent(ev);
        }
    
        public void setImageView(final ImageView imageView) {
            this.imageView = imageView;
            imageView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
                @Override
                public void onGlobalLayout() {
                    imageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    int intrinsicHeight = imageView.getDrawable().getIntrinsicHeight();
                    height = imageView.getHeight();
                    maxHeight = intrinsicHeight > height ? intrinsicHeight : height*2;
                    maxHeight =  height*2;
                }
            });
        }
    }
    

    相关文章

      网友评论

          本文标题:视差效果

          本文链接:https://www.haomeiwen.com/subject/fxnsjqtx.html