0

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?

4

1 に答える 1

0

Bitmap には getWidth 関数と getHeight 関数があります。これらを使用して、ビットマップのサイズを計算できます。次に、それに基づいてそれらを配置できます(使用したいパディングを使用して)。

于 2013-02-15T19:14:37.043 に答える