I try to make an custom RatingBar for my Application which has a different image for each of the 4 stars. With the following onDraw method it works quite good on my mobile.
public class MyBar extends RatingBar {
private int[] starArrayColor = { R.drawable.star_1_color, R.drawable.star_2_color, R.drawable.star_3_color, R.drawable.star_4_color };
private int[] starArrayGrey = { R.drawable.star_1_grey, R.drawable.star_2_grey, R.drawable.star_3_grey, R.drawable.star_4_grey };
(...)
@Override
protected synchronized void onDraw(Canvas canvas) {
int stars = getNumStars();
float rating = getRating();
float x = 10;
for (int i=0;i<stars;i++) {
Bitmap bitmap;
Resources res = getResources();
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
if ((int) rating-1 == i) {
paint.setAlpha(255);
bitmap = BitmapFactory.decodeResource(res, starArrayColor[i]);
} else {
paint.setAlpha(70);
bitmap = BitmapFactory.decodeResource(res, starArrayGrey[i]);
}
Bitmap scaled = Bitmap.createScaledBitmap(bitmap, 75, 75, true);
canvas.drawBitmap(scaled, x, 12, paint);
x += 96;
}
}
}
But the "calculation" of the x/y position and size of the images suc*s! Its just trial and error to get the right coordinates...
How can I calculate the position/size etc. to get it working properly on every device?
In the sources of AbsSeekBar/Progressbar I have found variables called mPaddingTop, mPaddingBottom etc. Is there a way to get those values?