视差效果—顶部有没有完全显示的效果

视差效果—顶部有没有完全显示的效果_第1张图片
Paste_Image.png

图片位于listview的顶部,

上代码--重写listview

public class ParallaxListView extends ListView {
    private static final String TAG = "ParallaxListView";
    private int maxHeight;//ImageVIew的最大高度

    public ParallaxListView(Context context) {
        this(context, null);
    }

    public ParallaxListView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ParallaxListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

    }
    ImageView imageView;
    int originalHeight;//最初的高度,就是那个120dp
    public void setImageView(ImageView imageView){
        this.imageView = imageView;
        //获取图片的本身高度
        maxHeight = imageView.getDrawable().getIntrinsicHeight()*2;
        originalHeight = getResources().getDimensionPixelSize(R.dimen.img_height);

    }

    /**
     * 该方法就是当listview滑动到头的时候执行,可以获取到继续滑动的距离
     * @param deltaY    继续滑动的距离 ,负值是顶部到头,正是底部
     * @param scrollY   就是scrollTo方法产生的距离
     * @param scrollRangeY   一般没有用,就是0
     * @param maxOverScrollY    到头后,最大还可以继续滚动的距离
     * @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){
            //根据deltaY让IMageVIew的高度增高
            int newHeight = imageView.getHeight() - deltaY/3;
            //对newHeight进行限制
            newHeight = Math.min(newHeight, maxHeight);

            ViewGroup.LayoutParams params = imageView.getLayoutParams();
            params.height = newHeight;
            imageView.setLayoutParams(params);
        }
        return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, maxOverScrollY, isTouchEvent);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if(ev.getAction()==MotionEvent.ACTION_UP){
            //让ImageView从当前高度恢复到最初的高度
            ValueAnimator animator = ValueAnimator.ofInt(imageView.getHeight(), originalHeight);
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    int animatedValue = (int) animation.getAnimatedValue();
                    //更新高度
                    ViewGroup.LayoutParams params = imageView.getLayoutParams();
                    params.height = animatedValue;
                    imageView.setLayoutParams(params);
                }
            });
            animator.setInterpolator(new OvershootInterpolator());
            animator.setDuration(500).start();
        }
        return super.onTouchEvent(ev);
    }
}

布局中使用




    


mainactivity

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.listview)
    ParallaxListView listview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        //去掉listview到头的边缘阴影
        listview.setOverScrollMode(View.OVER_SCROLL_NEVER);
        View headerView = View.inflate(this,R.layout.header,null);
        ImageView imageView = (ImageView) headerView.findViewById(R.id.iv_image);
        listview.setImageView(imageView);
        listview.addHeaderView(headerView);
        listview.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1,Constant.NAMES));
    }
}

你可能感兴趣的:(视差效果—顶部有没有完全显示的效果)