自定义ScrollView下拉头部变大,弹性动画缩回原型
2016-12-25 20:50
393 查看
自定义ScrollView下拉头部变大,弹性动画缩回原型;
源码下载:点击打开链接下载源码
package demo.com.customviewdemo.customview; import android.animation.Animator; import android.animation.ObjectAnimator; import android.animation.ValueAnimator; import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.ScrollView; /** * Created by Alv_chi */ public class HeadZoomScrollView extends ScrollView { private static final String TAG = "Test"; private View zoomHeadView; private float mDownY; private float mMoveY; private float mDeltaY; private boolean isZoomed; private float zoomHeight; private double zoomWindth; private float ratio = 0.4f; private int measuredWidth; private int measuredHeight; public HeadZoomScrollView(Context context) { this(context, null); } public HeadZoomScrollView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public HeadZoomScrollView(Context context, AttributeSet attrs, int defStyleAttr) { this(context, attrs, defStyleAttr, 0); } public HeadZoomScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } @Override protected void onFinishInflate() { super.onFinishInflate(); int childCount = getChildCount(); if (childCount > 0) { View contentView = getChildAt(0); if (contentView != null && contentView instanceof ViewGroup) { ViewGroup vg = (ViewGroup) contentView;//ScroView中只能包含一个View; int childNum = vg.getChildCount(); if (childNum > 0) { zoomHeadView = vg.getChildAt(0); } } } } @Override public boolean dispatchTouchEvent(MotionEvent ev) { if (measuredWidth <= 0 || measuredHeight <= 0) { // 获取测量到的可变大得View原始参数 measuredWidth = zoomHeadView.getMeasuredWidth(); measuredHeight = zoomHeadView.getMeasuredHeight(); } if (zoomHeadView == null || measuredWidth <= 0 || measuredHeight <= 0) { return super.onTouchEvent(ev); } switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: mDownY = ev.getY(); break; case MotionEvent.ACTION_MOVE: mMoveY = ev.getY(); mDeltaY = mMoveY - mDownY; if (mDeltaY > 10 && getScrollY() == 0) { zoom(mDeltaY); isZoomed = true; return true; } break; case MotionEvent.ACTION_UP: if (isZoomed) { replyView(); } break; } return super.dispatchTouchEvent(ev); } private void zoom(float factor) { // 通过改变参数改变view的大小: ViewGroup.LayoutParams layoutParams = zoomHeadView.getLayoutParams(); zoomHeight = measuredHeight + factor * ratio; zoomWindth = measuredWidth + factor * ratio; layoutParams.height = (int) zoomHeight; layoutParams.width = (int) zoomWindth; zoomHeadView.setLayoutParams(layoutParams); } private void replyView() { float delta = zoomHeadView.getMeasuredWidth() - measuredHeight; // 设置动画,由delta * ratio变到0过程: ValueAnimator anim = ObjectAnimator.ofFloat(delta * ratio, 0.0F).setDuration((long) (delta * ratio)); // 添加一个过程更新监听器: anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { //逐帧拿到数据,逐帧缩小 zoom((Float) animation.getAnimatedValue()); } }); anim.addListener(new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animation) { } @Override public void onAnimationEnd(Animator animation) { isZoomed = false; } @Override public void onAnimationCancel(Animator animation) { } @Override public void onAnimationRepeat(Animator animation) { } }); anim.start(); } }
源码下载:点击打开链接下载源码
相关文章推荐
- Android PullToRrefresh 自定义下拉刷新动画 (listview、scrollview等)
- 自定义ScrollView实现图片下拉放大(弹性效果)+ 悬浮框
- 源码推荐:自定义下拉刷新动画 弹性的分段选择控件
- 自定义ScrollView实现下拉放大头部图片
- 自定义ScrollView实现图片下拉放大(弹性效果)+ 悬浮框
- Android PullToRrefresh 自定义下拉刷新动画 (listview、scrollview等)
- 源码推荐:自定义下拉刷新动画 弹性的分段选择控件
- 弹性ScrollView,和下啦刷新的效果类似 实现下拉弹回和上拉弹回
- 自定义scrollView实现顶部图片下拉放大
- 自定义scrollView实现顶部图片下拉放大
- Android自定义渐变式炫酷ListView下拉刷新动画
- Android自定义下拉刷新动画--仿百度外卖下拉刷新
- 使用SpringAnimation实现带下拉弹簧动画的 ScrollView
- 自定义动画下拉刷新,可仿美团、京东
- android 自定义ScrollView控件上拉和下拉有拉伸效果
- android自定义弹性ScrollVIew ,十分简单!
- Android 自定义ScrollView实现拉下伸缩头部view
- android 自定义scrollview 仿QQ空间效果 下拉伸缩顶部图片,上拉回弹 上拉滚动顶部title 颜色渐变
- iOS 利用MJRefresh实现上拉刷新下拉加载(自定义动画)
- android 下拉放大头部图片的ScrollView