Custom rating graphs do not scale

I created a custom rating panel, as explained by kozyr , and it works fine when I have my rating scale installed with the exact dimensions, my full and empty pictures (40dp x40dp), but when I try to set the size to something smaller, the images don't scale, they are just cropped.

Can I scale my rating scale without scaling my actual assets?

+3
source share
1 answer

I had the same problem as you, since I want my application to run at different screen resolutions, I want the rating panel to automatically scale according to my needs.

"MyRatingBar", , , :

  • , ""
  • , "unChecked"
  • , ,
  • , ,

    public class MyRatingBar extends RelativeLayout implements OnClickListener {
    
    private int bitmapChecked;
    private int bitmapUnChecked;
    private byte rating;
    
    public MyRatingBar(Context context, int bitmapChecked, int bitmapUnChecked, int numSelectors, int ratingBarWidth, int ratingBarHeight) {
    super(context);
    this.bitmapChecked = bitmapChecked;
    this.bitmapUnChecked = bitmapUnChecked;
    
    int selectorWidth = ratingBarWidth / numSelectors;
    this.rating = -1;
    
    for (byte i = 0; i < numSelectors; i++) {
        ImageView newSelector = new ImageView(context);
        newSelector.setImageResource(bitmapUnChecked);
        this.addView(newSelector);
        newSelector.setLayoutParams(new RelativeLayout.LayoutParams(selectorWidth, ratingBarHeight));
        ((RelativeLayout.LayoutParams) newSelector.getLayoutParams()).setMargins(selectorWidth * i, 0, 0, 0);
        newSelector.setOnClickListener(this);
    }
    }
    
    public byte getRating() {
        return this.rating;
    }
    
    public void setRating(byte rating) {
    this.rating = rating;
    for (int currentChildIndex = 0; currentChildIndex < this.getChildCount(); currentChildIndex++) {
        ImageView currentChild = (ImageView) this.getChildAt(currentChildIndex);
        if (currentChildIndex < rating) {
            currentChild.setImageResource(this.bitmapChecked);
        }
        else {
            currentChild.setImageResource(this.bitmapUnChecked);
        }
    }       
    }
    
    public void onClick(View clickedView) {
    for (byte currentChildIndex = 0; currentChildIndex < this.getChildCount(); currentChildIndex++) {
        if (this.getChildAt(currentChildIndex).equals(clickedView)) {
            this.setRating((byte) (currentChildIndex + 1));
        }
    }
    }       
    }
    
0

All Articles