自定义竖直Seekbar

在做Android车载系统蓝牙音乐的交互界面时,有一个交互界面(音乐播放的音效界面)是需要做一个竖直的SeekBar。谷歌给出的SeekBar是水平的,而且美观上也不是很理想。因此UI做了一套新的界面,这就需要重新写SeekBar了。

参考了谷歌源码里对SeekBar的处理。给出Android里的相关SeekBar源码如下:

packageandroid.widget;

importandroid.content.Context;

importandroid.util.AttributeSet;


publicclassSeekBarextendsAbsSeekBar{


publicinterfaceOnSeekBarChangeListener {

voidonProgressChanged(SeekBar seekBar,intprogress,booleanfromUser);

voidonStartTrackingTouch(SeekBar seekBar);

voidonStopTrackingTouch(SeekBar seekBar);

}

privateOnSeekBarChangeListener mOnSeekBarChangeListener;


publicSeekBar(Context context) {

this(context,null);

}


publicSeekBar(Context context, AttributeSet attrs) {

this(context,attrs,com.android.internal.R.attr.seekBarStyle);

}

publicSeekBar(Context context, AttributeSet attrs, intdefStyle) {

super(context,attrs, defStyle);

}

@Override

voidonProgressRefresh(floatscale,booleanfromUser) {

super.onProgressRefresh(scale,fromUser);

if(mOnSeekBarChangeListener !=null){

mOnSeekBarChangeListener.onProgressChanged(this,getProgress(), fromUser);

}

}

publicvoidsetOnSeekBarChangeListener(OnSeekBarChangeListener l) {

mOnSeekBarChangeListener= l;

}


@Override

voidonStartTrackingTouch(){

if(mOnSeekBarChangeListener !=null){

mOnSeekBarChangeListener.onStartTrackingTouch(this);

}

}


@Override

voidonStopTrackingTouch(){

if(mOnSeekBarChangeListener !=null){

mOnSeekBarChangeListener.onStopTrackingTouch(this);

}

}


}

seekBar源码里我们看到,首先是定义了一个接口,这个结构有三个方法:

voidonProgressChanged(SeekBar seekBar, intprogress,booleanfromUser);

voidonStartTrackingTouch(SeekBar seekBar);

voidonStopTrackingTouch(SeekBar seekBar);

这三个方法是用来监听SeekBar的拖动监听用的,非常有用。在我们进行SeekBar的监听时要对这三个方法进行重写。从方法的名字就可以看出这三个方法的用处。第一个很明显是在SeekBar拖动时会被调用的,第二个是在开始拖动时进行调用的,第三个是拖动结束时进行调用的。我会在下面具体的例子进行详细的说明。

接下来需要考虑的就是让SeekBar竖起来,让Thumb可以上下拖动,这里我们需要对AbsSeekBar的源码进行分析。如下是源码:


/*

*Copyright (C) 2007 TheAndroid Open Source Project

*

*Licensed under theApache License, Version 2.0 (the"License");

*you may not use this file except in compliance with the License.

*You may obtain a copy of the License at

*

* http://www.apache.org/licenses/LICENSE-2.0

*

*Unless required by applicable law or agreed to in writing, software

*distributed under the License is distributed on an "AS IS"BASIS,

*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express orimplied.

*See the License for the specific language governing permissions and

*limitations under the License.

*/

packageandroid.widget;

importandroid.content.Context;

importandroid.content.res.TypedArray;

importandroid.graphics.Canvas;

importandroid.graphics.Rect;

importandroid.graphics.drawable.Drawable;

importandroid.util.AttributeSet;

importandroid.view.KeyEvent;

importandroid.view.MotionEvent;

publicabstractclassAbsSeekBarextendsProgressBar{

privateDrawable mThumb;

privateintmThumbOffset;

/**

*On touch, this offset plus the scaled value from the position of the

*touch will form the progress value. Usually 0.

*/

floatmTouchProgressOffset;

/**

*Whether this is userseekable.

*/

booleanmIsUserSeekable =true;

/**

*On key presses (right or left), the amount to increment/decrement the

*progress.

*/

privateintmKeyProgressIncrement = 1;

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值