参考:
参考进行了修改
参考网上的一个项目进行了修改,只保留了两个按钮,和一个进度条,可以显示进度到拖动按钮,效果如下:
xml布局文件
<com.tcl.navigator.setting.view.mSeekBar
android:id="@+id/day_light_progress"
android:layout_width="352px"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/btn_screen_brightness_minus"
android:layout_marginLeft="10px"
android:focusable="false"
android:max="15"
android:progressDrawable="@drawable/seekbar_horizonta"
android:paddingLeft="18px"
android:paddingRight="18px"
android:thumbOffset="0px"
android:maxHeight="4px"
android:minHeight="4px" />
SeekBar.java 代码文件
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.SeekBar;
public class mSeekBar extends SeekBar {
//比例对应的原点分辨率
private int ScreenWidth = 720;
private int screenHeight = 1280;
private Drawable thumb;
private Resources res;
private Paint paint;
private Bitmap bmp;
private Drawable mThumb;
public mSeekBar(Context context) {
this(context, null);
}
@SuppressWarnings("deprecation")
public mSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
paint = new Paint();
paint.setTextAlign(Paint.Align.CENTER);
paint.setColor(getResources().getColor(R.color.color_white));
res = context.getResources();
if(Pixels.getpixels_x(100) == 720){
bmp = BitmapFactory.decodeResource(res, R.drawable.seek_thumb);
}else{
bmp = BitmapFactory.decodeResource(res, R.drawable.seek_thumb);
}
thumb = new BitmapDrawable(bmp);
paint.setTextSize(30);
// 设置拖动的图片
setThumb(thumb);
// 图片的位置
setThumbOffset(thumb.getIntrinsicWidth());
}
@Override
public void setThumb(Drawable thumb) {
// TODO Auto-generated method stub
super.setThumb(thumb);
this.mThumb = thumb;
}
public Drawable getSeekBarThumb(){
return mThumb;
}
//设置thumb的偏移数值
@Override
public void setThumbOffset(int thumbOffset) {
// TODO Auto-generated method stub
super.setThumbOffset(thumbOffset/3);
}
String temp_str = "0";
@Override
protected synchronized void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.save();
int data = Integer.parseInt(temp_str);
Rect rect = getSeekBarThumb().getBounds();
float fontwidth = paint.measureText(temp_str);
if(data < 10){
canvas.drawText(temp_str, rect.left + (rect.width()) /2.0F, rect.top - paint.ascent() + (rect.height() - (paint.descent() - paint.ascent()))/2.0F, paint);
}else{
canvas.drawText(temp_str, rect.left + (rect.width() ) /2.0F, rect.top - paint.ascent() + (rect.height() - (paint.descent() - paint.ascent()))/2.0F, paint);
}
canvas.restore();
}
private Bitmap drawableToBitmap(Drawable drawable) {
// 取 drawable 的长宽
int w = drawable.getIntrinsicWidth();
int h = drawable.getIntrinsicHeight();
// 取 drawable 的颜色格式
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565;
// 建立对应 bitmap
Bitmap bitmap = Bitmap.createBitmap(w, h, config);
// 建立对应 bitmap 的画布
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, w, h);
// 把 drawable 内容画到画布中
drawable.draw(canvas);
return bitmap;
}
public void SetValue(String value){
StringBuffer sb = new StringBuffer();
sb.append(value);
temp_str = sb.toString();
invalidate();
}
@SuppressLint("NewApi")
@Override
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
public void setOnSeekBarChangeListener(final OnSeekBarChangeListener l) {
super.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (l != null) {
l.onProgressChanged(seekBar, progress, fromUser);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
if (l != null) {
l.onStartTrackingTouch(seekBar);
}
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
if (l != null) {
l.onStopTrackingTouch(seekBar);
}
}
});
}
}
Pixels.java 代码文件
import android.app.Activity;
import android.util.DisplayMetrics;
/**
* 获得屏幕像素
* @author Administrator
*
*/
public class Pixels {
private static int[] px = new int[101];
private static int[] py = new int[101];
private int w,h;
private DisplayMetrics dm;
public Pixels(Activity activity){
dm = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
w = dm.widthPixels;
h = dm.heightPixels;
int length = px.length;
for(int index=0;indexint)(w*0.01*index);
py[index] = (int)(h*0.01*index);
}
}
/**
* 获得x点值
* @param index
* @return
*/
public static int getpixels_x(int index){
return px[index];
}
/**
* 获得y点值
* @param index
* @return
*/
public static int getpixels_y(int index){
return py[index];
}
}
seekbar_horizonta.xml
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<corners android:radius="5.0dip" />
<solid android:color="#878278"/>
shape>
item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5.0dip" />
<solid android:color="#878278" />
shape>
clip>
item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="5.0dip" />
<solid android:color="#878278" />
shape>
clip>
item>
layer-list>
调用代码文件
private mSeekBar dayLightProgress
dayLightProgress.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// TODO Auto-generated method stub
dayLightProgress.SetValue(progress+"");
}
});