Android 自定义ScrollView - 头部可拉伸

一、直接上代码

public class HeadZoomScrollView extends ScrollView {

    //头部View
    private View mZoomView;
    // View的宽度
    private int mZoomViewWidth;
    // View的高度
    private int mZoomViewHeight;

    //是否在滑动
    private boolean isScrolling = false;
    //第一次按下的坐标
    private float firstPosition;
    //缩放系数
    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); }

    /**
     * 布局加载完后调用此方法,
     * 根据xml布局getChildAt(0)拿到的是LinearLayout,vg.getChildAt(0)拿到的是RelativeLayout。
     */
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        // 判断指定位置的视图是否为空。
        if(getChildAt(0) != null){
            // 返回组中指定位置的视图。
            ViewGroup vg = (ViewGroup) getChildAt(0);
            if (vg.getChildAt(0) != null){
                // 拿到所需的视图
                mZoomView = vg.getChildAt(0);
            }
        }
    }

    /**
     * 触摸事件
     * @param ev
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        // 获取mZoomView的宽高
        if (mZoomViewWidth <= 0 || mZoomViewHeight <= 0){
            mZoomViewWidth = mZoomView.getMeasuredWidth();
            mZoomViewHeight = mZoomView.getMeasuredHeight();
        }
        // 返回正在执行的操作的类型。
        switch (ev.getAction()){
            case MotionEvent.ACTION_MOVE: // 滑动
                if (!isScrolling){
                    // 等于0说明没有滑动,是第一次滑动,记录第一次按下的坐标
                    if (getScrollY() == 0) {
                        firstPosition = ev.getY();
                    }else {
                        break;
                    }
                }
                // 计算缩放值
                // 公式:(当前的位置 - 第一次按下的位置) * 缩放系数
                int distance = (int) ((ev.getY() - firstPosition) * mScrollRate);
                if (distance < 0) {
                    break;
                }
                isScrolling = true;
                setZoomView(distance);
                break;
            case MotionEvent.ACTION_UP: // 抬起
                isScrolling = false;
                replyZoomView();
                break;
        }


        return true;
    }

    /**
     * 回弹动画
     */
    private void replyZoomView() {
        // ViewGroup.LayoutParams lp = mZoomView.getLayoutParams();
        // lp.width = mZoomViewWidth;
        // lp.height = mZoomViewHeight;
        // ((MarginLayoutParams)lp).setMargins(0,0,0,0);
        // mZoomView.setLayoutParams(lp);

        // 计算下拉的缩放值再让属性动画根据这个值复原
        // 现在的宽 - 原本的宽
        int distance = mZoomView.getMeasuredWidth() - mZoomViewWidth;
        // 实现属性动画
        ValueAnimator valueAnimator = ValueAnimator
                .ofFloat(distance, 0)
                .setDuration((long) (distance * mReplyRate));
        // 将监听器添加到在动画生命周期内发送更新事件的监听器集中。需要添加监听器否则调用start(),也没有效果
        valueAnimator.addUpdateListener(animation -> {
            setZoomView((Float) animation.getAnimatedValue());
        });
        valueAnimator.start();
    }

    /**
     * 缩放View
     * @param zoom
     */
    private void setZoomView(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));
        // 设置间距
        // 公式:- (lp.width - mZoomViewWidth) / 2
        ((MarginLayoutParams)lp).setMargins(- (lp.width - mZoomViewWidth) / 2,0,0,0);
        mZoomView.setLayoutParams(lp);
    }
}

二、使用

<com.chenfuhai.framework.view.HeadZoomScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <!--头部-->
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="240dp">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/img_me_bg" />
        </RelativeLayout>

        <!--个人信息-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <!--个人信息-->
            <LinearLayout
                android:id="@+id/ll_me_info"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="@android:color/white"
                android:gravity="center_vertical"
                android:orientation="horizontal"
                android:paddingLeft="15dp"
                android:paddingRight="15dp">
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/img_user_info_icon" />
                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_weight="1"
                    android:text="@string/text_me_item_title_1"
                    android:textColor="@android:color/black" />
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/img_right_arrow" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</com.chenfuhai.framework.view.HeadZoomScrollView>

三、注意事项

1、ScrollView继承自ViewGroup,所以ScrollView属于自定义ViewGroup。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Android Hai

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值