Custom Android Ratingbar
<p>I want to implement a custom RatingBar in my workout app. The bar should have 4 stars and a stepsize of 1. The layout looks like this:</p>

<pre><code>&lt;com.example.workouttest.MyBar
    android:id="@+id/rating"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:max="4"
    android:numStars="4"
    android:stepSize="1"
    android:scaleX="0.6"
    android:scaleY="0.6"
    android:layout_gravity="right" /&gt;
</code></pre>

<p>I want to replace the default stars with custom images. But each of the 4 stars should have a different image:</p>

<p>Star 1 = an "X" that means "this item is disabled"</p>

<p>Star 2 = Thumb down</p>

<p>Star 3 = something that represents a "neutral rating"</p>

<p>Star 4 = Thumb up</p>

<p>Addtitionally when, for example, the item is rated with a 3 (neutral rating), all other stars (1,2 and 4) should display a greyed out version of their image.</p>

<p>I tried to extend from RatingBar and came up with the following code:</p>

<pre><code>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
    };

    public MyBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public MyBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyBar(Context context) {
        super(context);
    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        int stars = getNumStars();
        float rating = getRating();

        for (int i=0;i&lt;stars;i++) {
            Bitmap bitmap;
            Resources res = getResources();
            Paint paint = new Paint();

            if ((int) rating == i) {
                bitmap = BitmapFactory.decodeResource(res, starArrayColor[i]);
            } else {
                bitmap = BitmapFactory.decodeResource(res, starArrayGrey[i]);
            }
            canvas.drawBitmap(bitmap, 0, 0, paint);
            canvas.save();
        }

        super.onDraw(canvas);
    }
}
</code></pre>

<p>Sadly it did not work. It draws just the normal stars with my custom images as background.</p>

<p>Is here someone who knows how to help me solve this problem?</p>

<p><strong>UPDATE</strong></p>

<p>Thanks to Gabe my working onDraw method now looks like this:</p>

<pre><code>@Override
protected synchronized void onDraw(Canvas canvas) {
    int stars = getNumStars();
    float rating = getRating();
    float x = 0;

    for (int i=0;i&lt;stars;i++) {
        Bitmap bitmap;
        Resources res = getResources();
        Paint paint = new Paint();
        x += 50;

        if ((int) rating-1 == i) {
            bitmap = BitmapFactory.decodeResource(res, starArrayColor[i]);
        } else {
            bitmap = BitmapFactory.decodeResource(res, starArrayGrey[i]);
        }
        Bitmap scaled = Bitmap.createScaledBitmap(bitmap, 48, 48, true);
        canvas.drawBitmap(scaled, x, 0, paint);
        canvas.save();
    }
}
</code></pre>
