Android开发仿qq详情下拉头像变大
个人详情界面,很多都有下拉头像变大的效果,其实我觉得这效果还不如点击头像看大图呢
一、思路:
自定义ScrollView
二、效果图:
看视频更直观点:
Android开发教程案例分享-仿qq详情下拉头像变大
三、关键代码:
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,默认为第一个子viewprivate View zoomView;public void setZoomView(View zoomView) {this.zoomView = zoomView;}// 滑动放大系数,系数越大,滑动时放大程度越大private float mScaleRatio = 0.4f;public void setmScaleRatio(float mScaleRatio) {this.mScaleRatio = mScaleRatio;}// 最大的放大倍数private float mScaleTimes = 2f;public void setmScaleTimes(int mScaleTimes) {this.mScaleTimes = mScaleTimes;}// 回弹时间系数,系数越小,回弹越快private float mReplyRatio = 0.5f;public void setmReplyRatio(float mReplyRatio) {this.mReplyRatio = mReplyRatio;}@Overrideprotected void onFinishInflate() {super.onFinishInflate();
// 不可过度滚动,否则上移后下拉会出现部分空白的情况setOverScrollMode(OVER_SCROLL_NEVER);
// 获得默认第一个viewif (getChildAt(0) != null && getChildAt(0) instanceof ViewGroup && zoomView == null) {ViewGroup vg = (ViewGroup) getChildAt(0);if (vg.getChildCount() > 0) {zoomView = vg.getChildAt(0);}}}@Overridepublic 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();//滑动到顶部时,记录位置} else {break;}}int distance = (int) ((ev.getY() - y) * mScaleRatio);if (distance < 0) break;//若往下滑动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));
// 如超过最大放大倍数,直接返回if (scaleTimes > mScaleTimes) return;ViewGroup.LayoutParams layoutParams = zoomView.getLayoutParams();layoutParams.width = (int) (zoomViewWidth + s);layoutParams.height = (int) (zoomViewHeight * ((zoomViewWidth + s) / zoomViewWidth));
// 设置控件水平居中((MarginLayoutParams) layoutParams).setMargins(-(layoutParams.width - zoomViewWidth) / 2, 0, 0, 0);zoomView.setLayoutParams(layoutParams);}
四、项目demo源码结构图
有问题或者需要完整demo源码的私信我,我每天都看私信