Any ideas?
When using the default progressDrawable
or a progressDrawable
set through a theme all will be ok as in the constructor for the RatingBar
(its superclass ProgressBar
to be more precise) widget a method will be called to "make tiles" from that drawable. When using the setProgressDrawable
method this doesn't happen and if you pass a simple BitmapDrawable
or a LayerDrawable
(with simple BitmapDrawables
) that Drawable
will simply be stretched to cover the widget's background area(what you see know).
In order to make it work you would need to manually do what the RatingBar
does at start, create the tiles along with the ClipDrawables
that it uses. I've written a method for this, following the source code of the ProgressBar
widget:
private Drawable buildRatingBarDrawables(Bitmap[] images) {
final int[] requiredIds = { android.R.id.background,
android.R.id.secondaryProgress, android.R.id.progress };
final float[] roundedCorners = new float[] { 5, 5, 5, 5, 5, 5, 5, 5 };
Drawable[] pieces = new Drawable[3];
for (int i = 0; i < 3; i++) {
ShapeDrawable sd = new ShapeDrawable(new RoundRectShape(
roundedCorners, null, null));
BitmapShader bitmapShader = new BitmapShader(images[i],
Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
sd.getPaint().setShader(bitmapShader);
ClipDrawable cd = new ClipDrawable(sd, Gravity.LEFT,
ClipDrawable.HORIZONTAL);
if (i == 0) {
pieces[i] = sd;
} else {
pieces[i] = cd;
}
}
LayerDrawable ld = new LayerDrawable(pieces);
for (int i = 0; i < 3; i++) {
ld.setId(i, requiredIds[i]);
}
return ld;
}
Then you would use the LayerDrawable
returned by this method with the setProgressDrawable
method. The RatingBar
set its width multiplying the width of one of the state bitmaps with the number of stars, so in order to show the right amount of stars this has to be calculated as well.