Android 可滑动的ProgressBar

目的是为了在最小值的时候能够保留一个制定距离的宽度

Android 可滑动的ProgressBar_第1张图片Android 可滑动的ProgressBar_第2张图片

效果:

Android-progress

滑动的处理
  1. 绘制矩形
  2. 计算距离
  3. 距离转化进度
  4. 滑动时不允许主动设置进度
ScrollProgressBar
package com.example.bannerdemo;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class ScrollProgressBar extends View {
   
    private final static String TAG = "ScrollProgressBar";
    private final static int DEFAULT_WIDTH_SIZE = 90;
    // 默认圆角度数
    private final static int DEFAULT_CONNER_SIZE = 32;
    // 默认最大值
    private final static int DEFAULT_VALUE_SIZE = 100;

    private Paint mPaint;
    private RectF mRectF;
    // 宽度
    private int mWidth;
    private int mHeight;
    // 最大值
    private int max;
    // 最小值
    private int min;
    // 进度
    private int progress;
    // 圆角度数
    private int mRadius = DEFAULT_CONNER_SIZE;
    // 前进距离
    private float mOffsetStep;
    // 最小进度progress
    private int mMinLength = DEFAULT_WIDTH_SIZE;
    // 进度颜色
    private int mProgressColor;
    private int mDisableProgressColor;
    private float scrollAllX;
    //0 横向,1竖着
    private int layoutOrientation = 0;
    private boolean isOnTouch;
    private boolean canChangeTouch;

    // 进度监听
    private OnProgressChangeListener onProgressChangeListener;

    public ScrollProgressBar(Context context) {
   
        this(context, null);
    }

    public ScrollProgressBar(Context context, AttributeSet attrs) {
   
        this(context, attrs, 0);
    }

    public ScrollProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
   
        super(context, attrs, defStyleAttr);
        loadAttrs(context, attrs);
        init();
    }

    private void loadAttrs(Context context, AttributeSet attrs) {
   
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ScrollProgressBar);
        max = typedArray.getInt(R.styleable.ScrollProgressBar_max, DEFAULT_VALUE_SIZE);
        min = typedArray.getInt(R.styleable.ScrollProgressBar_min, 0);
        layoutOrientation = typedArray.

你可能感兴趣的:(Android,UI(自定义控件),android,android,studio,java)