我想在我的健身应用程序中实现一个定制的RatingBar。酒吧应该有4颗星,步长为1。布局如下:
<com.example.workouttest.MyBar
android:id="@+id/rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="4"
android:numStars="4"
android:stepSize="1"
android:scaleX="0.6"
android:scaleY="0.6"
android:layout_gravity="right" />我想用自定义图像替换默认的星星。但这四颗星中的每一颗都应该有一个不同的形象:
星号1= "X“,意思是”此项目已禁用“。
星2=拇指向下
星号3=代表“中性评级”的东西
星4=大拇指向上
例如,当项目被评定为3(中性等级)时,所有其他恒星(1,2和4)都应该显示其图像的灰色版本。
我试图从RatingBar进行扩展,并提出了以下代码:
public class MyBar extends RatingBar {
private int[] starArrayColor = {
R.drawable.star_1_color,
R.drawable.star_2_color,
R.drawable.star_3_color,
R.drawable.star_4_color
};
private int[] starArrayGrey = {
R.drawable.star_1_grey,
R.drawable.star_2_grey,
R.drawable.star_3_grey,
R.drawable.star_4_grey
};
public MyBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyBar(Context context) {
super(context);
}
@Override
protected synchronized void onDraw(Canvas canvas) {
int stars = getNumStars();
float rating = getRating();
for (int i=0;i<stars;i++) {
Bitmap bitmap;
Resources res = getResources();
Paint paint = new Paint();
if ((int) rating == i) {
bitmap = BitmapFactory.decodeResource(res, starArrayColor[i]);
} else {
bitmap = BitmapFactory.decodeResource(res, starArrayGrey[i]);
}
canvas.drawBitmap(bitmap, 0, 0, paint);
canvas.save();
}
super.onDraw(canvas);
}
}遗憾的是,它没有起作用。它只画正常的星星与我的自定义图像为背景。
有人知道怎么帮我解决这个问题吗?
更新
感谢加布,我正在工作的onDraw方法现在看起来如下所示:
@Override
protected synchronized void onDraw(Canvas canvas) {
int stars = getNumStars();
float rating = getRating();
float x = 0;
for (int i=0;i<stars;i++) {
Bitmap bitmap;
Resources res = getResources();
Paint paint = new Paint();
x += 50;
if ((int) rating-1 == i) {
bitmap = BitmapFactory.decodeResource(res, starArrayColor[i]);
} else {
bitmap = BitmapFactory.decodeResource(res, starArrayGrey[i]);
}
Bitmap scaled = Bitmap.createScaledBitmap(bitmap, 48, 48, true);
canvas.drawBitmap(scaled, x, 0, paint);
canvas.save();
}
}发布于 2013-02-14 20:46:28
不要打电话给超级。那会吸引正常的恒星。从那以后,还有什么不管用的?
https://stackoverflow.com/questions/14883698
复制相似问题