Android自定义ScrollView实现放大回弹效果实例代码
作者:阿呆哥哥 时间:2022-10-15 11:00:58
1,刚刚在别人开源的项目中看到了一个挺不错的用户体验,效果图如下:
2,那下面我们就来实现一下,首先看一下布局,由于一般只是我们包含头像的那部分方法,所以这里我们要把布局分成两部分,对应的布局文件效果图如下:
3,自定义ScrollView
第一步:创建一个类,继承自ScrollView,重写相应的构造函数
public class ZoomInScrollView extends ScrollView {
public ZoomInScrollView(Context context) {
this(context, null);
}
public ZoomInScrollView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ZoomInScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
}
第二步:重写OnFinishInflate()方法,并记录第一个子view,即我们的head_fragment
@Override
protected void onFinishInflate() {
super.onFinishInflate();
//设置不可过度滚动,否则上移后下拉会出现部分空白的情况
setOverScrollMode(OVER_SCROLL_NEVER);
View child = getChildAt(0);
if (child != null && child instanceof ViewGroup) {
//获取默认第一个子View
mHeaderView = ((ViewGroup) child).getChildAt(0);
}
}
第三步:重写OnTouchEvent()方法,在Action_Move方法中拿到下滑的距离,通过设置head_view的属性参数来改变它的大小,在UP的时候还原head_view
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (mHeaderView == null)
return super.onTouchEvent(ev);
switch (ev.getAction()) {
case MotionEvent.ACTION_MOVE:
if (!mIsPulling) {
//第一次下拉
if (getScrollY() == 0) {
//在顶部的时候,记录顶部位置
mLastY = (int) ev.getY();
} else {
break;
}
}
if (ev.getY() - mLastY < 0)
return super.onTouchEvent(ev);
int distance = (int) ((ev.getY() - mLastY) * mScaleRatio);
mIsPulling = true;
setZoom(distance);
return true;
case MotionEvent.ACTION_UP:
mIsPulling = false;
replyView();
break;
}
return super.onTouchEvent(ev);
}
在回弹view的时候通过属性动画动态的改变head_view的值,并重写onSizeChange()方法,实时的记录head_view的宽高
/**
** 放大view
*/
private void setZoom(float s) {
float scaleTimes = (float) ((mHeaderWidth + s) / (mHeaderWidth * 1.0));
// 如超过最大放大倍数,直接返回
if (scaleTimes > mScaleTimes) return;
ViewGroup.LayoutParams layoutParams = mHeaderView.getLayoutParams();
layoutParams.width = (int) (mHeaderWidth + s);
layoutParams.height = (int) (mHeaderHeight * ((mHeaderWidth + s) / mHeaderWidth));
// 设置控件水平居中
((MarginLayoutParams) layoutParams).setMargins(-(layoutParams.width - mHeaderWidth) / 2, 0, 0, 0);
mHeaderView.setLayoutParams(layoutParams);
}
/**
* 回弹
*/
private void replyView() {
final float distance = mHeaderView.getMeasuredWidth() - mHeaderWidth;
// 设置动画
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 onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mHeaderWidth = mHeaderView.getMeasuredWidth();
mHeaderHeight = mHeaderView.getMeasuredHeight();
}
这样就实现了我们的效果了,看一下我们自己实现的效果:
demo下载:ZoomInScrollView_jb51.rar
来源:http://www.cnblogs.com/wjtaigwh/p/6549863.html
标签:scrollview,Android
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Hook实现Android 微信、陌陌 、探探位置模拟(附源码下载)
2022-11-14 14:44:43
![](https://img.aspxhome.com/file/2023/1/138941_0s.png)
Android 破解视频App去除广告功能详解及解决办法总结
2022-07-16 23:28:50
![](https://img.aspxhome.com/file/2023/8/108288_0s.png)
基于NET Core 的Nuget包制作、发布和运用流程解析(完整过程)
2022-03-10 01:09:29
![](https://img.aspxhome.com/file/2023/1/96401_0s.jpg)
C#实现XML序列化与反序列化
2022-10-06 19:50:21
JSON各种转换问题(json转List,json转对象等)
2021-09-28 08:53:45
C#常用数据结构和算法总结
2021-10-31 22:05:29
![](https://img.aspxhome.com/file/2023/6/69206_0s.png)
datatable生成excel和excel插入图片示例详解
2022-06-18 21:48:38
![](https://img.aspxhome.com/file/2023/2/116592_0s.jpg)
使用C#代码获取存储过程返回值
2021-08-18 08:29:46
struts2如何使用拦截器进行用户权限控制实例
2023-02-23 18:14:15
![](https://img.aspxhome.com/file/2023/7/81247_0s.png)
c# 引用Nlog插件的步骤
2021-08-10 01:03:43
![](https://img.aspxhome.com/file/2023/7/111257_0s.png)
Java中的javaBean、vo、entity、domain和pojo
2023-03-30 21:41:20
![](https://img.aspxhome.com/file/2023/5/61605_0s.png)
Java中如何对字符串进行utf-8编码
2023-01-20 16:13:04
![](https://img.aspxhome.com/file/2023/8/82438_0s.png)
Java字符编码简介_动力节点Java学院整理
2022-01-24 23:50:38
流式图表拒绝增删改查之kafka核心消费逻辑上篇
2023-04-19 03:32:11
Knife4j 3.0.3 整合SpringBoot 2.6.4的详细过程
2023-11-25 03:54:34
![](https://img.aspxhome.com/file/2023/9/59429_0s.png)
详谈jvm--Java中init和clinit的区别
2022-01-10 10:35:22
Springboot整合mybatis开启二级缓存的实现示例
2023-02-24 13:07:18
![](https://img.aspxhome.com/file/2023/2/81372_0s.jpg)
native.js获取手机硬件基本信息实例代码android版
2023-10-19 13:33:09
Android布局中gravity与layout_gravity属性说明
2021-08-27 10:51:31
![](https://img.aspxhome.com/file/2023/1/93321_0s.jpg)
SpringBoot多模块项目框架搭建过程解析
2022-10-02 06:12:34