HorizontalScrollView自定义ScrollBar

吃水不忘挖井人,鸣谢老油条同学

链接是垂直滑动的ScrollView自定义的ScrollBar,包括位置和样式
//www.greatytc.com/p/f32f5c2adceb

本文参照此方式,实现HorizontalScrollView自定义ScrollBar的位置和样式,用法相同;
xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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">

<com.xxx.xxx.widget.HorizontalScrollBar
    android:id="@+id/scroll_bar"
    android:layout_width="472dp"
    android:layout_height="6dp"
    android:layout_marginStart="271dp"
    android:layout_marginBottom="18dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintBottom_toBottomOf="parent" />

<HorizontalScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="none">

    <!--你的布局-->

</HorizontalScrollView>

</android.support.constraint.ConstraintLayout>

用法:
控件初始化后:
mScrollBar.attachScrollView(mScrollView);
代码如下:

import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import com.baidu.media.radio.online.R;
import com.baidu.media.ui.view.HorizontalScrollView;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * 母老虎饲养员 on 2021/7/2
 */
public final class HorizontalScrollBar extends View {

    private int mHorizontalThumbHeight;
    private int mHorizontalThumbWidth;
    private int mHorizontalTrackWidth;
    private int mHorizontalThumbStart;
    private Drawable mThumbDrawable;
    private Drawable mTrackDrawable;
    private ObjectAnimator animator;
    private Runnable dismissRunnable;

    public HorizontalScrollBar(@Nullable Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        // 两个drawable文件可以自己更换
        mThumbDrawable = ContextCompat.getDrawable(getContext(), R.drawable.aa_scrollbar);
        mTrackDrawable = ContextCompat.getDrawable(getContext(), R.drawable.aa_scrollbar_bg);
        mHorizontalTrackWidth = 472;
        mHorizontalThumbHeight = 6;
        dismissRunnable = new Runnable() {
            @Override
            public void run() {
                if (isShown()) {
                    animator = ObjectAnimator.ofFloat(this, "alpha",
                            new float[]{getAlpha(), 0.0F}).setDuration(1000);
                    animator.start();
                }
            }
        };
    }

    protected void onDraw(@Nullable Canvas canvas) {
        super.onDraw(canvas);
        if (canvas != null) {
            int start = mHorizontalThumbStart;
            int end = mHorizontalThumbStart + mHorizontalThumbWidth;
            mTrackDrawable.setBounds(0, 0, mHorizontalTrackWidth, mHorizontalThumbHeight);
            mTrackDrawable.draw(canvas);
            mThumbDrawable.setBounds(start, 0, end, mHorizontalThumbHeight);
            mThumbDrawable.draw(canvas);
        }
    }

    @SuppressLint("ClickableViewAccessibility")
    public final void attachScrollView(@NotNull final HorizontalScrollView nestedScrollView) {
        setVisibility(INVISIBLE);
        // 这里判断只有滑动的时候予以显示
        nestedScrollView.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int eventAction = event.getAction();
                switch (eventAction) {
                    case MotionEvent.ACTION_MOVE:
                        setVisibility(VISIBLE);
                        showNow();
                        invalidate();
                        break;
                    case MotionEvent.ACTION_UP:
                        setVisibility(INVISIBLE);
                        showNow();
                        invalidate();
                        break;
                    default:
                        break;
                }
                return false;
            }
        });
        nestedScrollView.setOnScrollChangeListener(new OnScrollChangeListener() {
            public final void onScrollChange(View view, int x, int y, int lastX, int lastY) {
                calculate(nestedScrollView);
            }
        });
        post(new Runnable() {
            @Override
            public void run() {
                calculate(nestedScrollView);
            }
        });
    }

    private void calculate(HorizontalScrollView nestedScrollView) {
        int visibleWidth = nestedScrollView.getMeasuredWidth();
        int contentWidth = nestedScrollView.getChildAt(0).getWidth();
        if (contentWidth > visibleWidth) {
            int scrollX = nestedScrollView.getScrollX();
            mHorizontalThumbWidth = getMeasuredWidth() * visibleWidth / contentWidth;
            mHorizontalThumbStart = (getMeasuredWidth() - mHorizontalThumbWidth) * scrollX / (contentWidth - visibleWidth);
            showNow();
            invalidate();
        }
    }

    private void showNow() {
        if (animator != null) {
            animator.end();
            animator.cancel();
        }
        setAlpha(1.0F);
        postDelayDismissRunnable();
    }

    private void postDelayDismissRunnable() {
        removeCallbacks(dismissRunnable);
        postDelayed(dismissRunnable, 1000);
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容