自定义评级栏抽屉不会缩放

时间:2011-06-02 04:32:01

标签: android

我创建了一个自定义评级栏,正如kozyr所解释的那样,当我的评级栏设置为我的完整和空的drawables(40dp x40dp)的确切尺寸但是当我尝试设置尺寸时它很有效对于较小的图像没有缩放的图像,它们只是被裁剪。

是否可以在不缩放实际资产的情况下扩展我的评级栏?

1 个答案:

答案 0 :(得分:0)

我遇到了和你一样的问题,因为我希望我的应用程序可以运行各种不同的屏幕分辨率,我希望评级栏能够根据我的需要自动缩放。

实际上创建一个完全符合我需要的类“MyRatingBar”并不困难,它需要的所有输入都是:

  • “已检查”星标的可绘制资源int
  • “unChecked”明星的可绘制资源int
  • 你想要的星星数
  • 您希望评级栏的宽度为
  • 您想要评分栏的高度

    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));
        }
    }
    }       
    }
    
相关问题