上一次咱們試驗了有彈性的ScrollView。詳情html
這一次,咱們來試驗有彈性的ScrollView。java
國際慣例,效果圖:android
主要代碼:ide
public class FlexibleScrollView extends ScrollView { private Context mContext; private static int mMaxOverDistance = 50; public FlexibleScrollView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.mContext = context; initView(); } public FlexibleScrollView(Context context, AttributeSet attrs) { super(context, attrs); this.mContext = context; initView(); } public FlexibleScrollView(Context context) { super(context); this.mContext = context; initView(); } private void initView() { DisplayMetrics metrics = mContext.getResources().getDisplayMetrics(); float density = metrics.density; mMaxOverDistance = (int) (density * mMaxOverDistance); } @Override protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) { return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, mMaxOverDistance, isTouchEvent); } }經過上面這個類也能夠實現彈性效果
2、仿朋友圈背景圖片下拉佈局
public class ScrollDampView extends ScrollView { /** 該屬性具體參數 怎麼控制 未解!!!! */ private static final int LEN = 0xc8; /** 回彈時所用的時間 */ private static final int DURATION = 200; /** 最大Y座標 其值通常設定爲Scroller對應控件的高度 */ private static final int MAX_DY = 200; private Scroller mScroller; /** 阻尼係數 */ private static final float OFFSET_RADIO = 2.5f; private float startY; private int imageViewH; private ImageView imageView; private boolean scrollerType; public ScrollDampView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public ScrollDampView(Context context, AttributeSet attrs) { super(context, attrs); mScroller = new Scroller(context); } public ScrollDampView(Context context) { super(context); } public void setImageView(ImageView imageView) { this.imageView = imageView; } float curY; @Override public boolean dispatchTouchEvent(MotionEvent event) { int action = event.getAction(); if (!mScroller.isFinished()) { return super.onTouchEvent(event); } switch (action) { case MotionEvent.ACTION_DOWN:// 變量賦初始值 imageViewH = imageView.getHeight(); startY = event.getY(); break; case MotionEvent.ACTION_MOVE: if (imageView.isShown()) { float deltaY = (event.getY() - startY ) / OFFSET_RADIO; Log.i("syso", "deltaY: "+deltaY+" imageTop: "+imageView.getTop()); //往下拉 if (deltaY > 0 && deltaY <= imageView.getBottom() + LEN) { android.view.ViewGroup.LayoutParams params = imageView.getLayoutParams(); params.height = (int) (imageViewH + deltaY);// 改變高度 imageView.setLayoutParams(params); } scrollerType = false; } break; case MotionEvent.ACTION_UP: scrollerType = true; // 開始一個動畫控制,由(startX , startY)在duration時間內前進(dx,dy)個單位 // ,即到達座標爲(startX+dx , startY+dy)處 mScroller.startScroll(imageView.getLeft(), imageView.getBottom(), 0 - imageView.getLeft(), imageViewH - imageView.getBottom(), DURATION); invalidate(); break; } return super.dispatchTouchEvent(event); } // //該mScroller針對於imageView的變化 // 被父視圖調用,用於必要時候對其子視圖的值(mScrollX和mScrollY) // 進行更新。典型的狀況如:父視圖中某個子視圖使用一個Scroller對象來實現滾動操做,會使得此方法被調用。 @Override public void computeScroll() { if (mScroller.computeScrollOffset()) { int x = mScroller.getCurrX(); int y = mScroller.getCurrY();// ImageView的當前Y座標 imageView.layout(0, 0, x + imageView.getWidth(), y);// 使imageView自己作相應變化 invalidate(); // 滑動還未完成時,手指擡起時,當前y座標大於其實imageView的高度時 // 設定imageView的佈局參數 做用:使除imageView以外的控件作相應變化 if (!mScroller.isFinished() && scrollerType && y > MAX_DY) { android.view.ViewGroup.LayoutParams params = imageView .getLayoutParams(); params.height = y; imageView.setLayoutParams(params); } } } }
<com.example.dampview.ScrollDampView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/dampview" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <!--此處必須設置imageview的scaleType爲centerCrop,固然在代碼中設置也能夠--> <ImageView android:id="@+id/img" android:layout_width="match_parent" android:layout_height="160dp" android:scaleType="centerCrop" android:src="@drawable/image" /> <ImageView android:id="@+id/iv_photo" android:layout_width="64dp" android:layout_height="64dp" android:layout_marginTop="-32dp" android:src="@drawable/ic_launcher" /> </LinearLayout> </com.example.dampview.ScrollDampView>