自定义评分条图标不会按比例缩放

3
我按照kozyr的说明创建了自定义评级栏,当我将评级栏设置为完整和空绘制的确切尺寸(40dp x 40dp)时,它非常有效,但是当我尝试将大小调整为较小的值时,图像不会缩放,而只是被裁剪。
是否可能在不缩放实际资产的情况下缩放我的评级栏?
1个回答

0

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

实际上,创建一个名为“MyRatingBar”的自定义类并不困难,它可以完全满足我的需求,它所需要的所有输入都是:

  • 一个 "选中" 星形的可绘制资源 int
  • 一个 "未选中" 星形的可绘制资源 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)); } } } }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接