2011-06-02 53 views
3

我創建了一個自定義評級欄,如kozyr所解釋的那樣,當我的評級欄設置爲我的完整和空白drawables(40dp x40dp)的確切尺寸時它工作得很好,但是當我嘗試要將尺寸設置爲更小的圖像不縮放,他們只是裁剪。自定義評分欄drawables不成比例

是否可以縮放我的評級欄而不縮放我的實際資產?

回答

0

我和你有同樣的問題,因爲我想我的應用程序運行在各種不同的屏幕分辨率上,我希望評級欄根據我的需要自動調整。

它實際上是不難創建自己的類「MyRatingBar」這確實正是我需要的,它所需要的輸入是:

  • 一個「檢查」明星
  • 可繪製資源INT
  • 爲「未登記」明星
  • 數星星你想
  • 所需的寬度可繪製資源INT您的等級吧是
  • 你希望你的等級欄的高度be

    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)); 
        } 
    } 
    }  
    }