在做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;