[英]Margin/Padding of the stars in a RatingBar (or position and size)?
我嘗試為我的應用程序創建一個自定義的RatingBar,它對4星中的每一個都有不同的圖像。 使用以下onDraw方法,可以在我的手機上很好地工作。
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 };
(...)
@Override
protected synchronized void onDraw(Canvas canvas) {
int stars = getNumStars();
float rating = getRating();
float x = 10;
for (int i=0;i<stars;i++) {
Bitmap bitmap;
Resources res = getResources();
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
if ((int) rating-1 == i) {
paint.setAlpha(255);
bitmap = BitmapFactory.decodeResource(res, starArrayColor[i]);
} else {
paint.setAlpha(70);
bitmap = BitmapFactory.decodeResource(res, starArrayGrey[i]);
}
Bitmap scaled = Bitmap.createScaledBitmap(bitmap, 75, 75, true);
canvas.drawBitmap(scaled, x, 12, paint);
x += 96;
}
}
}
但是圖像的x / y位置和大小的“計算”為suc * s! 為了獲得正確的坐標而進行的反復試驗...
如何計算位置/大小等以使其在每台設備上正常工作?
在AbsSeekBar / Progressbar的源代碼中,我找到了名為mPaddingTop,mPaddingBottom等的變量。有沒有辦法獲取這些值?
位圖具有getWidth和getHeight函數。 您可以使用它們來計算位圖的大小。 然后,您可以基於此放置它們(使用您想使用的任何填充物)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.