本文主要是介绍Android 自定义竖立的SeekBar实现音量调节,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
xml 调用
<com.dnake.widget.MyVerticalSeekBarandroid:id="@+id/talk_volume"android:layout_width="wrap_content"android:layout_height="230dp"android:layout_centerHorizontal="true"android:layout_marginTop="30dp"android:maxHeight="4dp"android:minHeight="4dp"android:progressDrawable="@drawable/seekbar_color"android:thumb="@drawable/tuoyuan"android:thumbOffset="0dp" />
效果
自定义控件,同时实现了竖立seekbar的滑动监听OnSeekBarChangeListener
@SuppressLint("AppCompatCustomView")
public class MyVerticalSeekBar extends SeekBar {private Drawable mThumb;public interface OnSeekBarChangeListener {void onProgressChanged(MyVerticalSeekBar VerticalSeekBar, int progress, boolean fromUser);void onStartTrackingTouch(MyVerticalSeekBar VerticalSeekBar);void onStopTrackingTouch(MyVerticalSeekBar VerticalSeekBar);}void onStartTrackingTouch() {if (mOnSeekBarChangeListener != null) {mOnSeekBarChangeListener.onStartTrackingTouch(this);}}void onStopTrackingTouch() {if (mOnSeekBarChangeListener != null) {mOnSeekBarChangeListener.onStopTrackingTouch(this);}}void onProgressRefresh(float scale, boolean fromUser) {Log.i("6", "onProgressRefresh==>scale" + scale);Drawable thumb = mThumb;if (thumb != null) {setThumbPos(getHeight(), thumb, scale, Integer.MIN_VALUE);invalidate();}if (mOnSeekBarChangeListener != null) {mOnSeekBarChangeListener.onProgressChanged(this, getProgress(), fromUser);}}public void setOnSeekBarChangeListener(MyVerticalSeekBar.OnSeekBarChangeListener l) {mOnSeekBarChangeListener = l;}private void setThumbPos(int w, Drawable thumb, float scale, int gap) {Log.i("6", "setThumbPos==>w" + w);int available = w + getPaddingLeft() - getPaddingRight();int thumbWidth = thumb.getIntrinsicWidth();int thumbHeight = thumb.getIntrinsicHeight();available -= thumbWidth;// The extra space for the thumb to move on the trackavailable += getThumbOffset() * 2;int thumbPos = (int) (scale * available);int topBound, bottomBound;if (gap == Integer.MIN_VALUE) {Rect oldBounds = thumb.getBounds();topBound = oldBounds.top;bottomBound = oldBounds.bottom;} else {topBound = gap;bottomBound = gap + thumbHeight;}thumb.setBounds(thumbPos, topBound, thumbPos + thumbWidth, bottomBound);}private MyVerticalSeekBar.OnSeekBarChangeListener mOnSeekBarChangeListener;public MyVerticalSeekBar(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);// TODO Auto-generated constructor stub}public MyVerticalSeekBar(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stub}public MyVerticalSeekBar(Context context) {super(context);// TODO Auto-generated constructor stub}protected void onSizeChanged(int w, int h, int oldw, int oldh) {super.onSizeChanged(h, w, oldh, oldw);}@Overrideprotected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(heightMeasureSpec, widthMeasureSpec);setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());}protected void onDraw(Canvas c) {c.rotate(-90);c.translate(-getHeight(), 0);super.onDraw(c);}@Overridepublic synchronized void setProgress(int progress) {super.setProgress(progress);onSizeChanged(getWidth(), getHeight(), 0, 0);}@Overridepublic boolean onTouchEvent(MotionEvent event) {if (!isEnabled()) {return false;}switch (event.getAction()) {case MotionEvent.ACTION_DOWN:Log.e("TAG", "onKeyDown: 22222222222222222222222");onStartTrackingTouch();setProgress(getMax() - (int) (getMax() * event.getY() / getHeight()));onSizeChanged(getWidth(), getHeight(), 0, 0);break;case MotionEvent.ACTION_MOVE:setProgress(getMax() - (int) (getMax() * event.getY() / getHeight()));onSizeChanged(getWidth(), getHeight(), 0, 0);break;case MotionEvent.ACTION_UP:onStopTrackingTouch();setProgress(getMax() - (int) (getMax() * event.getY() / getHeight()));onSizeChanged(getWidth(), getHeight(), 0, 0);break;case MotionEvent.ACTION_CANCEL:onStopTrackingTouch();break;}return true;}}
这篇关于Android 自定义竖立的SeekBar实现音量调节的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!