图片位于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);
}
}
布局中使用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="com.kailing.parallax.MainActivity">
<com.kailing.parallax.ParallaxListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
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<String>(this,android.R.layout.simple_list_item_1,Constant.NAMES));
}
}