问题描述
看来我无法设置ShapeDrawable作为progressDrawable为的RatingBar。我尝试以下,但失败:
<的RatingBar
机器人:ID =@ + ID /的RatingBar
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentBottom =真
机器人:numStars =5
机器人:stepSize的=1.0
机器人:等级=3.0
风格=@风格/ myRatingBar
/>
myRatingbar风格:
<样式名称=myRatingBar父=@安卓风格/ Widget.RatingBar>
<项目名称=机器人:progressDrawable> @绘制/ ratingbar_full< /项目>
<项目名称=机器人:indeterminateDrawable> @绘制/ ratingbar_full< /项目>
<项目名称=安卓了minHeight> 48dip< /项目>
<项目名称=安卓了maxHeight> 48dip< /项目>
<项目名称=机器人:scaleType>中心< /项目>
< /风格>
ratingbar_full.xml:
<形状
机器人:造型=环
机器人:innerRadius =10dip
机器人:厚度=1dip>
[固体机器人:色=@色/红/>
<大小
机器人:宽=48dip
机器人:身高=48dip
/>
< /形状>
没有显示在屏幕上。
编辑:使用png格式,而不是形状适用于这一行:
Shape XML Drawables + RatingBar a complete mess.
This is as close as I could get to a solution without writing a whole new class.
My extended class builds the progress drawable correctly for the ProgressBar
clamping it as required.
Replace your empty and full states with the ones I've prepopulated. Not very flexible at the moment, you could easily abstract the setting of empty/full star states.
/**
* Created by chris on 28/08/2014.
* For Yoyo-Android.
*/
public class ShapeDrawableRatingBar extends RatingBar {
/**
* TileBitmap to base the width off of.
*/
@Nullable
private Bitmap mSampleTile;
public ShapeDrawableRatingBar(final Context context, final AttributeSet attrs) {
super(context, attrs);
setProgressDrawable(createProgressDrawable());
}
@Override
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (mSampleTile != null) {
final int width = mSampleTile.getWidth() * getNumStars();
setMeasuredDimension(resolveSizeAndState(width, widthMeasureSpec, 0),
getMeasuredHeight());
}
}
protected LayerDrawable createProgressDrawable() {
final Drawable backgroundDrawable = createBackgroundDrawableShape();
LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{
backgroundDrawable,
backgroundDrawable,
createProgressDrawableShape()
});
layerDrawable.setId(0, android.R.id.background);
layerDrawable.setId(1, android.R.id.secondaryProgress);
layerDrawable.setId(2, android.R.id.progress);
return layerDrawable;
}
protected Drawable createBackgroundDrawableShape() {
final Bitmap tileBitmap = drawableToBitmap(getResources().getDrawable(R.drawable.ic_stamp_circle_empty));
if (mSampleTile == null) {
mSampleTile = tileBitmap;
}
final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape());
final BitmapShader bitmapShader = new BitmapShader(tileBitmap, Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
shapeDrawable.getPaint().setShader(bitmapShader);
return shapeDrawable;
}
protected Drawable createProgressDrawableShape() {
final Bitmap tileBitmap = drawableToBitmap(getResources().getDrawable(R.drawable.ic_stamp_circle_full));
final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape());
final BitmapShader bitmapShader = new BitmapShader(tileBitmap, Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
shapeDrawable.getPaint().setShader(bitmapShader);
return new ClipDrawable(shapeDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
}
Shape getDrawableShape() {
final float[] roundedCorners = new float[]{5, 5, 5, 5, 5, 5, 5, 5};
return new RoundRectShape(roundedCorners, null, null);
}
public static Bitmap drawableToBitmap(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
}
int width = drawable.getIntrinsicWidth();
width = width > 0 ? width : 1;
int height = drawable.getIntrinsicHeight();
height = height > 0 ? height : 1;
final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
}
Calling setMax
setMaxStars
calls requestLayout
so you it will measure the width correctly. No need to work out the android:minWidth
, Just set android:layout_width="wrap_content"
.
Just remember you will need to add a bit of padding to your ShapeDrawables
as they get repeated edge to edge.
这篇关于ShapeDrawable作为progressDrawable为的RatingBar Android中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!