/**
* Created by Administrator on 2017-08-01.
*/
public class MyListView extends ListView {
private ImageView mImage;
private int mOriginalHeight;
private int drawableHeight;
public MyListView(Context context) {
super(context);
}
public MyListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
/**
* 設置ImageView 圖片,
* 獲得圖片的高度和ImageView的初始高度
*
* @param image
*/
public void setParallaxImage(ImageView image) {
this.mImage = image;
mOriginalHeight = mImage.getHeight();
drawableHeight = mImage.getDrawable().getIntrinsicHeight();
}
@Override
protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
if (isTouchEvent && deltaY < 0) {
if (mImage.getHeight() <= drawableHeight) {
int newHeight = (int) (mImage.getHeight() + Math.abs(deltaY / 3.0f));
mImage.getLayoutParams().height = newHeight;
mImage.requestLayout();
}
}
return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, maxOverScrollY, isTouchEvent);
}
/**
* 鬆開手回彈
* @param ev ev
* @return boolean
*/
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getActionMasked()){
case MotionEvent.ACTION_UP:
final int startHeight = mImage.getHeight();
final int endHeight = mOriginalHeight;
ResetAnimation animation = new ResetAnimation(mImage, startHeight, endHeight);
startAnimation(animation);
break;
}
return super.onTouchEvent(ev);
}
public class ResetAnimation extends Animation{
private final ImageView mImage;
private final int startHeight;
private final int endHeight;
public ResetAnimation(ImageView image,int startHeight,int endHeight) {
this.mImage=image;
this.startHeight=startHeight;
this.endHeight=endHeight;
setInterpolator(new OvershootInterpolator());
setDuration(500);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
Integer newHeight=evaluate(interpolatedTime,startHeight,endHeight);
mImage.getLayoutParams().height=newHeight;
mImage.requestLayout();
super.applyTransformation(interpolatedTime, t);
}
/**
* 類型估值器
* @param fraction
* @param startValue
* @param endValue
* @return
*/
public Integer evaluate(float fraction, Integer startValue, Integer endValue) {
int startInt = startValue;
return (int)(startInt + fraction * (endValue - startInt));
}
}
}
public class OverScrollByActivity extends AppCompatActivity {
private MyListView mListView;
ArrayList<String> mData=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_over_scroll_by);
mListView = (MyListView) findViewById(R.id.listView);
final View mHeaderView = View.inflate(this, R.layout.view_header, null);
final ImageView mImage = (ImageView) mHeaderView.findViewById(R.id.iv);
mListView.addHeaderView(mHeaderView);
mListView.setOverScrollMode(View.OVER_SCROLL_NEVER);
mHeaderView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// 當佈局填充結束之後, 此方法會被調用
mListView.setParallaxImage(mImage);
mHeaderView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
initData();
initFill();
}
private void initFill() {
mListView.setAdapter(new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,mData));
// 加Header
}
private void initData(){
mData.add("android");
mData.add("ios");
mData.add("xml");
mData.add("java");
mData.add("android");
mData.add("ios");
mData.add("xml");
mData.add("java");
mData.add("android");
mData.add("ios");
mData.add("xml");
mData.add("java");
}
}
R.layout.activity_over_scroll_by
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="mdks.andy.activity.OverScrollByActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<mdks.andy.view.MyListView
android:id="@+id/listView"
android:divider="#e43435"
android:dividerHeight="2dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="160dp"
android:scaleType="centerCrop"
android:src="@mipmap/picture"
/>
</LinearLayout>