seekbar按照百分比设置thumb的大小:
在代码里面设置seekbar的进度条高度,和thumb的大小(为了百分比适配),
设置thumb圆角:
float[] outerR = new float[]{1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000};
ShapeDrawable shapeDrawable = new ShapeDrawable(new RoundRectShape(outerR, null, null));
shapeDrawable.setIntrinsicHeight(DensityUtil.widthPercentToPix(0.02734375));
shapeDrawable.setIntrinsicWidth(DensityUtil.widthPercentToPix(0.02734375));
shapeDrawable.getPaint().setColor(Color.WHITE);
shapeDrawable.getPaint().setStyle(Paint.Style.FILL);
lightbright_seekbar.setThumb(shapeDrawable);
一般设置了thumb之后进度条会和thumb的高度一样,所以通过maxheight来限制大小,但是在代码里面只能通过反射来设置。
//反射修改seekbar的maxheight
try {
Class<?> superclass = lightbright_seekbar.getClass().getSuperclass().getSuperclass();
Field mMaxHeight = superclass.getDeclaredField("mMaxHeight");
mMaxHeight.setAccessible(true);
mMaxHeight.set(lightbright_seekbar, DensityUtil.widthPercentToPix(0.009765625));
} catch (Exception e) {
e.printStackTrace();
}
seekbar在代码中设置按下和正常的不同效果:
如果seekbar的thumb需要有按下和正常的效果,可以通过写shape.xml来实现,但是这种方式不能很好的适配也不能使用大小不一的drawable,thumb的selector中的drawable如果是不一样大小的,不论是shapedrawable还是图片都会导致thumb的形状出现异常。所以可以在seekbar的监听里面重新设置thumb来完成:
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
Bitmap bm = BitmapUtil.zoomImg(NewMakeUpActivity.this, R.drawable.v240_icon_seekbarthumb_press, DensityUtil.transDPtoPIX(36), DensityUtil.transDPtoPIX(36));
seekBar.setThumb(new BitmapDrawable(bm));
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
Bitmap bm = BitmapUtil.zoomImg(NewMakeUpActivity.this, R.drawable.v240_icon_seekbarthumb_normal, DensityUtil.transDPtoPIX(27), DensityUtil.transDPtoPIX(27));
seekBar.setThumb(new BitmapDrawable(bm));
}
至于大小可以通过缩放bitmap来完成,以下为2种缩放的方式:
/**
* 等比缩放图片
* @param resId
* @param newWidth
* @param newHeight
* @return
*/
public static Bitmap zoomImg(Context context,int resId, int newWidth, int newHeight) {
// 获得图片的宽高
Bitmap bm = BitmapFactory.decodeResource(context.getResources(), resId);
int width = bm.getWidth();
int height = bm.getHeight();
// 计算缩放比例
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 取得想要缩放的matrix参数
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 得到新的图片
Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
Log.i(TAG, "zoomImg: "+scaleWidth+"\n"+scaleHeight+"\n"+width+"\n"+height+"\n"+newWidth+"\n"+newHeight);
return newbm;
}
/**
* 调用函数缩小图片
*
* @param context
* @param restId
* @param dstWidth
* @param dstHeight
* @return
*/
public BitmapDrawable getNewDrawable(Activity context, int restId, int dstWidth, int dstHeight) {
Bitmap Bmp = BitmapFactory.decodeResource(
context.getResources(), restId);
Bitmap bmp = Bitmap.createScaledBitmap(Bmp, dstWidth, dstHeight, true);
BitmapDrawable d = new BitmapDrawable(bmp);
Bitmap bitmap = d.getBitmap();
if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
d.setTargetDensity(context.getResources().getDisplayMetrics());
}
return d;
}
但是以上2种方式无论哪一种中的宽高都必须是由dp转px,因为系统在读取同一张图片时在不同的设备上获得的宽高是不同的,比如在1X屏上读的就是原大小,在3X屏上读到的宽高就是放大3倍的,所以为了保证图片计算的缩放比例正确,参数中的缩放后的尺寸必须是dp转化的px,这样才能保证图片的缩放比例在任何设备上都是一样的。
本来drawable可以通过setbounds(left,top,right,bottom)来设置大小。但是在seekbar中,left和top的值很难确定,也就无法确定这个thumb在seekbar的什么位置了。