自定义CenterSeekBar代码如下
package com.example.myapplication;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
/**
* 中间向左右滑动的滑动条
*/
public class CenterSeekBar extends View {
private static final String TAG = CenterSeekBar.class.getSimpleName();
private static final int CLICK_ON_PRESS = 1;
/*中间的拖动bar*/
private Drawable mThumb;
//默认的背景
private Drawable mBackgroundDrawable;
//滑动后的背景
private Drawable mProgressDrawable;
private int mSeekBarWidth;
private int mSeekBarHeight;
private int mThumbWidth;
private int mThumbHeight;
//thumb的中心位置
private int mThumbCenterPosition = 0;
private OnSeekBarChangeListener mSeekBarChangeListener;
private int mFlag;
int mMinWidth;
int mMaxWidth;
int mMinHeight = 12;
int mMaxHeight;
protected int mPaddingLeft = 0;
protected int mPaddingRight = 0;
protected int mPaddingTop = 0;
protected int mPaddingBottom = 0;
private int maxProgress;
private int minProgress = 0;
private int progress;
public CenterSeekBar(Context context) {
this(context, null);
}
public CenterSeekBar(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CenterSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
final TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.CenterSeekBar, defStyleAttr, defStyleAttr);
mThumb = a.getDrawable(R.styleable.CenterSeekBar_thumb);
if (mThumb == null) {
mThumb = getResources().getDrawable(R.drawable.seekbar_thumb_selector);
}
mProgressDrawable = a.getDrawable(R.styleable.CenterSeekBar_progressDrawable);
if (mProgr

本文介绍了如何通过自定义View实现CenterSeekBar,使用滑动圆点作为拇指和渐变背景来表示进度。代码中详细展示了Drawable的选择、测量尺寸、绘制过程以及触摸事件处理。通过设置属性max、min和progress,可以灵活调整滑动范围。
&spm=1001.2101.3001.5002&articleId=124041379&d=1&t=3&u=3205d66dd1c74dafaa7e3d7206ab7a05)
7207

被折叠的 条评论
为什么被折叠?



