实现接口函数:
this.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
if (SeekBarChange != null) {
SeekBarChange.onStopTrackingTouch(seekBarDemo.this);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
if (SeekBarChange != null) {
SeekBarChange.onStartTrackingTouch(seekBarDemo.this);
}
}
@Override
public void onProgressChanged(final SeekBar seekBar,
final int progress, boolean fromUser) {
if (SeekBarChange != null) {
SeekBarChange.onProgressChanged(seekBarDemo.this, progress,
fromUser);
}
hand = getHandler(1, seekBarDemo.this, progress);
}
});
完整的参考代码如下:
package org.lytsing.Android.qzoneloading;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.SeekBar;
public class seekBarDemo extends SeekBar implements OnTouchListener {
private onLong longClick;
/**
* 长按接口
* @author terry
*
*/
public interface onLong {
public boolean onLongClick(View v);
}
private onChange SeekBarChange;
/**
* 进度改变接口
* @author terry
*
*/
public interface onChange {
public void onStopTrackingTouch(seekBarDemo seekBar);
public void onStartTrackingTouch(seekBarDemo seekBar);
public void onProgressChanged(seekBarDemo seekBar, int progress,
boolean fromUser);
}
private Handler hand;
private Runnable runable;
private Thread th;
public static int i = 0;
private boolean isStop = false;
public static int pp = 0;
public int index = 0;
public seekBarDemo(Context context) {
this(context, null);
// TODO Auto-generated constructor stub
}
public seekBarDemo(Context context, AttributeSet attrs) {
super(context, attrs);
this.setOnTouchListener(this);
this.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
if (SeekBarChange != null) {
SeekBarChange.onStopTrackingTouch(seekBarDemo.this);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
if (SeekBarChange != null) {
SeekBarChange.onStartTrackingTouch(seekBarDemo.this);
}
}
@Override
public void onProgressChanged(final SeekBar seekBar,
final int progress, boolean fromUser) {
if (SeekBarChange != null) {
SeekBarChange.onProgressChanged(seekBarDemo.this, progress,
fromUser);
}
hand = getHandler(1, seekBarDemo.this, progress);
}
});
/**
* 为runable 赋值
*/
runable = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
do {
i++;
try {
Thread.sleep(400);
Message msg = hand.obtainMessage();
msg.arg1 = i;
msg.sendToTarget();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} while (isStop);
}
};
}
/**
* 获取一个handler 对象
* @param 0代表onTouch 1代表onChange
* @param 视图对象
* @param 进度
* @return 返回一个handler对象
*/
public Handler getHandler(final int j, final View v, final int progress) {
Handler h = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
switch (j) {
case 0:
if (msg.arg1 == 3) {
if (longClick != null) {
longClick.onLongClick(v);
}
}
break;
case 1:
if (msg.arg1 == 1) {
pp = progress;
}
if (msg.arg1 == 2) {
if (pp != progress) {
i = 0;
}
}
if (msg.arg1 == 3) {
i = 0;
if (pp == progress) {
if (longClick != null) {
longClick.onLongClick(seekBarDemo.this);
}
}
}
break;
}
super.handleMessage(msg);
}
};
return h;
}
/**
* 设置长按事件
* @param longClick
*/
public void setOnLongSeekBarClick(onLong longClick) {
this.longClick = longClick;
}
/**
* 设置进度改变事件
* @param change
*/
public void setOnSeekBarChange(onChange change) {
this.SeekBarChange = change;
}
@Override
public boolean onTouch(final View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
isStop = true;
th = new Thread(runable);
th.start();
i = 0;
hand = getHandler(0, v, 0);
break;
case MotionEvent.ACTION_UP:
isStop = false;
break;
}
return false;
}
}
在布局的XML可以如下定义:
<org.lytsing.android.qzoneloading.seekBarDemo
android:max="200" android:id="@+id/seek" android:layout_width="fill_parent"
android:layout_height="wrap_content">
</org.lytsing.android.qzoneloading.seekBarDemo>
功能至此完成,在前台可以像我们之前使用SeekBar 一样使用,功能一样,大家也可以根据自己的需求再另行扩展。这里给出的只是一个思路。