无图案的滚轮计数 有加速度和惯性

可以通过touch 上滑下滑来修改mValue的值

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

public class ScrollWheel extends View {
    private static final String TAG = "ScrollWheel";
    private static final float MAX_STEP = 8.0f;
    private static final float DEFAULT_STEP = 0.2f;

    private float mValue;
    private float mStep;
    private float mLastY;
    private float mVelocity;
    private float mDirection;


    public ScrollWheel(Context context) {
        super(context);
        mValue = 0;
        mStep = DEFAULT_STEP;
        mLastY = 0;
        mVelocity = 0;
        mDirection = 0;
    }

    public ScrollWheel(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mValue = 0;
        mStep = DEFAULT_STEP;
        mLastY = 0;
        mVelocity = 0;
        mDirection = 0;
    }

    public ScrollWheel(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mValue = 0;
        mStep = DEFAULT_STEP;
        mLastY = 0;
        mVelocity = 0;
        mDirection = 0;
    }

    public ScrollWheel(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mValue = 0;
        mStep = DEFAULT_STEP;
        mLastY = 0;
        mVelocity = 0;
        mDirection = 0;
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float y = event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mLastY = y;
                mVelocity = 0;
                break;

            case MotionEvent.ACTION_MOVE:
                float deltaY = y - mLastY;
                mLastY = y;
                mVelocity = Math.abs(deltaY) * DEFAULT_STEP;
                mStep = Math.min(MAX_STEP, mVelocity);
                mDirection = deltaY > 0 ? -1 : 1;
                mValue += mStep * mDirection;

                Log.d(TAG, "Velocity: " + mVelocity + ", Value: " + mValue);
                break;

            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                startInertiaDeceleration();
                break;
        }
        return true;
    }

    private void startInertiaDeceleration() {
        final float deceleration = 0.5f;
        post(new Runnable() {
            @Override
            public void run() {
                mVelocity *= deceleration;
                mStep = Math.max(DEFAULT_STEP, Math.min(MAX_STEP, mVelocity));
                mValue += mStep * mDirection;

                Log.d(TAG, "Inertia Velocity: " + mVelocity + ", Value: " + mValue);

                if (Math.abs(mVelocity) > DEFAULT_STEP) {
                    post(this);
                }
            }
        });
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值