仿ios下拉效果

仿ios下拉效果

经常看到ios应用有这样的效果,即使控件已经滑动到顶部了,仍然可以往下继续滑。而我们用scrollview写出相同的界面,滑动到顶部或者底部的时候就很僵了。反正,继续滑是不可能继续滑的,用ios控件来写android又不会,只有自己改个控件才能维持得了生活这个样子。

效果大概就是这样。
画质感人,无码效果自己脑补

代码比较简单,就直接贴了
public class MyScrollView extends ScrollView {

    private static final int ratio = 4;
    private View inner;
    private Rect normal = new Rect();;
    private float oldY;

    public MyScrollView(Context context) {
        super(context);
    }

    public MyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        if (getChildCount() > 0) {
            inner = getChildAt(0);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (inner == null) {
            return super.onTouchEvent(ev);
        } else {
            processTouchEvent(ev);
        }
        return super.onTouchEvent(ev);
    }

    public void processTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                oldY = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                final float preY = oldY;
                float nowY = ev.getY();
                /**
                 * ratio=4 表示view移动的距离为拖动距离的1/4
                 */
                int deltaY = (int) (preY - nowY) / ratio;
                oldY = nowY;
                //是否需要移动布局
                if (isNeedMove()) {
                    if (normal.isEmpty()) {
                        // 保存正常的布局位置
                        normal.set(inner.getLeft(), inner.getTop(),
                                inner.getRight(), inner.getBottom());
                        return;
                    }
                    int yy = inner.getTop() - deltaY;

                    // 移动布局
                    inner.layout(inner.getLeft(), yy, inner.getRight(),
                            inner.getBottom() - deltaY);
                }
                break;
            case MotionEvent.ACTION_UP:
                if (isNeedAnimation()) {
                    animation();
                }
                break;
            default:
                break;
        }
    }

    // 开启动画移动
    public void animation() {
        // 开启移动动画
        TranslateAnimation ta = new TranslateAnimation(0, 0, inner.getTop(),
                normal.top);
        ta.setDuration(200);
        ta.setInterpolator(new OvershootInterpolator());
        inner.startAnimation(ta);
        //回到正常的布局位置
        inner.layout(normal.left, normal.top, normal.right, normal.bottom);
        normal.setEmpty();
    }

    // 是否需要开启动画
    public boolean isNeedAnimation() {
        return !normal.isEmpty();
    }

    // 是否需要移动布局
    public boolean isNeedMove() {
        //getMeasuredHeight是view的真实高度,即包含不可见部分的高度,getHeight是在屏幕上显示的高度(可见部分)
        int offset = inner.getMeasuredHeight() - getHeight();
        int scrollY = getScrollY();
        if (scrollY == 0 || scrollY == offset) {//处在头部或者在底部
            return true;
        }
        return false;
    }

}

布局什么的

<com.onlyway.testandroid.MyScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:scaleType="fitXY"
            android:src="@mipmap/header" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_marginTop="20dp"
            android:gravity="center"
            android:text="苟"
            android:textColor="#000000"
            android:textStyle="bold"
            />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:gravity="center"
            android:text="利"
            android:textColor="#000000"
            android:textStyle="bold"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:gravity="center"
            android:text="国"
            android:textColor="#000000"
            android:textStyle="bold"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:gravity="center"
            android:text="家"
            android:textColor="#000000"
            android:textStyle="bold"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:gravity="center"
            android:text="生"
            android:textColor="#000000"
            android:textStyle="bold"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:gravity="center"
            android:text="死"
            android:textColor="#000000"
            android:textStyle="bold"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:gravity="center"
            android:text="以"
            android:textColor="#000000"
            android:textStyle="bold"/>


    </LinearLayout>


</com.onlyway.testandroid.MyScrollView>

首秀有人愿意捧场就太好不过了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值