How to create Custom Ratings bar in Android

前端 未结 13 1232
故里飘歌
故里飘歌 2020-11-22 06:28

Hello all i need to perform Ratings in my application... SO i need to create custom Ratings bar... Can Anyone Help me in this?

相关标签:
13条回答
  • 2020-11-22 07:24

    The following code works:

    @Override
    protected synchronized void onDraw(Canvas canvas)
    {
        int stars = getNumStars();
        float rating = getRating();
        try
        {
            bitmapWidth = getWidth() / stars;
        }
        catch (Exception e)
        {
            bitmapWidth = getWidth();
        }
        float x = 0;
    
        for (int i = 0; i < stars; i++)
        {
            Bitmap bitmap;
            Resources res = getResources();
            Paint paint = new Paint();
    
            if ((int) rating > i)
            {
                bitmap = BitmapFactory.decodeResource(res, starColor);
            }
            else
            {
                bitmap = BitmapFactory.decodeResource(res, starDefault);
            }
            Bitmap scaled = Bitmap.createScaledBitmap(bitmap, getHeight(), getHeight(), true);
            canvas.drawBitmap(scaled, x, 0, paint);
            canvas.save();
            x += bitmapWidth;
        }
    
        super.onDraw(canvas);
    }
    
    0 讨论(0)
提交回复
热议问题