您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)android中怎么利用ScrollView實(shí)現(xiàn)下拉放大頭部圖片,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
public class HeadZoomScrollView extends ScrollView { public HeadZoomScrollView(Context context) { super(context); } public HeadZoomScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public HeadZoomScrollView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } // 用于記錄下拉位置 private float y = 0f; // zoomView原本的寬高 private int zoomViewWidth = 0; private int zoomViewHeight = 0; // 是否正在放大 private boolean mScaling = false; // 放大的view,默認(rèn)為第一個(gè)子view private View zoomView; public void setZoomView(View zoomView) { this.zoomView = zoomView; } // 滑動(dòng)放大系數(shù),系數(shù)越大,滑動(dòng)時(shí)放大程度越大 private float mScaleRatio = 0.4f; public void setmScaleRatio(float mScaleRatio) { this.mScaleRatio = mScaleRatio; } // 最大的放大倍數(shù) private float mScaleTimes = 2f; public void setmScaleTimes(int mScaleTimes) { this.mScaleTimes = mScaleTimes; } // 回彈時(shí)間系數(shù),系數(shù)越小,回彈越快 private float mReplyRatio = 0.5f; public void setmReplyRatio(float mReplyRatio) { this.mReplyRatio = mReplyRatio; } @Override protected void onFinishInflate() { super.onFinishInflate(); // 不可過度滾動(dòng),否則上移后下拉會(huì)出現(xiàn)部分空白的情況 setOverScrollMode(OVER_SCROLL_NEVER); // 獲得默認(rèn)第一個(gè)view if (getChildAt(0) != null && getChildAt(0) instanceof ViewGroup && zoomView == null) { ViewGroup vg = (ViewGroup) getChildAt(0); if (vg.getChildCount() > 0) { zoomView = vg.getChildAt(0); } } } @Override public boolean onTouchEvent(MotionEvent ev) { if (zoomViewWidth <= 0 || zoomViewHeight <=0) { zoomViewWidth = zoomView.getMeasuredWidth(); zoomViewHeight = zoomView.getMeasuredHeight(); } if (zoomView == null || zoomViewWidth <= 0 || zoomViewHeight <= 0) { return super.onTouchEvent(ev); } switch (ev.getAction()) { case MotionEvent.ACTION_MOVE: if (!mScaling) { if (getScrollY() == 0) { y = ev.getY();//滑動(dòng)到頂部時(shí),記錄位置 } else { break; } } int distance = (int) ((ev.getY() - y)*mScaleRatio); if (distance < 0) break;//若往下滑動(dòng) mScaling = true; setZoom(distance); return true; case MotionEvent.ACTION_UP: mScaling = false; replyView(); break; } return super.onTouchEvent(ev); } /**放大view*/ private void setZoom(float s) { float scaleTimes = (float) ((zoomViewWidth+s)/(zoomViewWidth*1.0)); // 如超過最大放大倍數(shù),直接返回 if (scaleTimes > mScaleTimes) return; ViewGroup.LayoutParams layoutParams = zoomView.getLayoutParams(); layoutParams.width = (int) (zoomViewWidth + s); layoutParams.height = (int)(zoomViewHeight*((zoomViewWidth+s)/zoomViewWidth)); // 設(shè)置控件水平居中 ((MarginLayoutParams) layoutParams).setMargins(-(layoutParams.width - zoomViewWidth) / 2, 0, 0, 0); zoomView.setLayoutParams(layoutParams); } /**回彈*/ private void replyView() { final float distance = zoomView.getMeasuredWidth() - zoomViewWidth; // 設(shè)置動(dòng)畫 ValueAnimator anim = ObjectAnimator.ofFloat(distance, 0.0F).setDuration((long) (distance * mReplyRatio)); anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { setZoom((Float) animation.getAnimatedValue()); } }); anim.start(); } @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { super.onScrollChanged(l, t, oldl, oldt); if (onScrollListener!=null) onScrollListener.onScroll(l,t,oldl,oldt); } private OnScrollListener onScrollListener; public void setOnScrollListener(OnScrollListener onScrollListener) { this.onScrollListener = onScrollListener; } /**滑動(dòng)監(jiān)聽*/ public interface OnScrollListener{ void onScroll(int scrollX,int scrollY,int oldScrollX, int oldScrollY); } }
看完上述內(nèi)容,你們對android中怎么利用ScrollView實(shí)現(xiàn)下拉放大頭部圖片有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。