自定义ScrollView实现下拉放大头部图片
程序员文章站
2022-05-31 10:01:46
...
下拉实现放大头部布局,先看一下下图的效果:
代码实现如下:
public class HeadZoomScrollView extends ScrollView {
private View mZoomView;
private int mZoomViewWidth;
private int mZoomViewHeight;
private float firstPosition;//记录第一次按下的位置
private boolean isScrolling;//是否正在缩放
private float mScrollRate = 0.3f;//缩放系数,缩放系数越大,变化的越大
private float mReplyRate = 0.5f;//回调系数,越大,回调越慢
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);
}
public void setmZoomView(View mZoomView) {
this.mZoomView = mZoomView;
}
public void setmScrollRate(float mScrollRate) {
this.mScrollRate = mScrollRate;
}
public void setmReplyRate(float mReplyRate) {
this.mReplyRate = mReplyRate;
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
init();
}
private void init() {
setOverScrollMode(OVER_SCROLL_NEVER);
if (getChildAt(0) != null) {
ViewGroup vg = (ViewGroup) getChildAt(0);
if (vg.getChildAt(0) != null) {
mZoomView = vg.getChildAt(0);
}
}
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (mZoomViewWidth <= 0 || mZoomViewHeight <= 0) {
mZoomViewWidth = mZoomView.getMeasuredWidth();
mZoomViewHeight = mZoomView.getMeasuredHeight();
}
switch (ev.getAction()) {
case MotionEvent.ACTION_UP:
//手指离开后恢复图片
isScrolling = false;
replyImage();
break;
case MotionEvent.ACTION_MOVE:
if (!isScrolling) {
if (getScrollY() == 0) {
firstPosition = ev.getY();// 滚动到顶部时记录位置,否则正常返回
} else {
break;
}
}
int distance = (int) ((ev.getY() - firstPosition) * mScrollRate); // 滚动距离乘以一个系数
if (distance < 0) { // 当前位置比记录位置要小,正常返回
break;
}
// 处理放大
isScrolling = true;
setZoom(distance);
return true; // 返回true表示已经完成触摸事件,不再处理
}
return true;
}
//回弹动画
private void replyImage() {
float distance = mZoomView.getMeasuredWidth() - mZoomViewWidth;
ValueAnimator valueAnimator = ValueAnimator.ofFloat(distance, 0f).setDuration((long) (distance * mReplyRate));
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
setZoom((Float) animation.getAnimatedValue());
}
});
valueAnimator.start();
}
public void setZoom(float zoom) {
if (mZoomViewWidth <= 0 || mZoomViewHeight <= 0) {
return;
}
ViewGroup.LayoutParams lp = mZoomView.getLayoutParams();
lp.width = (int) (mZoomViewWidth + zoom);
lp.height = (int) (mZoomViewHeight * ((mZoomViewWidth + zoom) / mZoomViewWidth));
((MarginLayoutParams) lp).setMargins(-(lp.width - mZoomViewWidth) / 2, 0, 0, 0);
mZoomView.setLayoutParams(lp);
}
}
详细代码见我的Gitbub:https://github.com/makewhat/HeadBlowScrollView
上一篇: PHP内核-源码安装与介绍详解
下一篇: Java字符编码总结
推荐阅读
-
利用iOS手势与scrollView代理实现图片的放大缩小
-
iOS tableView实现下拉图片放大效果
-
iOS tableView实现下拉图片放大效果
-
Android自定义ScrollView实现放大回弹效果实例代码
-
android 自定义ScrollView实现背景图片伸缩的实现代码及思路
-
Android自定义ScrollView实现放大回弹效果
-
android 自定义ScrollView实现背景图片伸缩的实现代码及思路
-
Android自定义ScrollView实现放大回弹效果实例代码
-
自定义ScrollView 实现图片放大,上滑修改标题栏状态
-
Android自定义ImageView实现图片缩放滑动,双击放大缩小,多点触控旋转,圆角矩形,圆形和仿刮刮卡效果