This source file includes following definitions.
- initialize
- onDraw
package org.chromium.chrome.browser.banners;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import org.chromium.chrome.R;
import org.chromium.ui.base.LocalizationUtils;
public class RatingView extends View {
private static final int MAX_INCREMENT = 10;
private final Bitmap mStarFull;
private final Bitmap mStarHalf;
private final Bitmap mStarEmpty;
private final boolean mIsLayoutLTR;
private final Rect mDrawingRect;
private int mIncrements;
public RatingView(Context context, AttributeSet params) {
super(context, params);
mIsLayoutLTR = !LocalizationUtils.isLayoutRtl();
mDrawingRect = new Rect();
Resources res = getResources();
Bitmap starHalf = BitmapFactory.decodeResource(res, R.drawable.btn_star_half);
if (!mIsLayoutLTR) {
int width = starHalf.getWidth();
int height = starHalf.getHeight();
Matrix m = new Matrix();
m.preScale(-1, 1);
starHalf = Bitmap.createBitmap(starHalf, 0, 0, width, height, m, false);
}
mStarHalf = starHalf;
mStarFull = BitmapFactory.decodeResource(res, R.drawable.btn_star_full);
mStarEmpty = BitmapFactory.decodeResource(res, R.drawable.btn_star_empty);
}
void initialize(float rating) {
mIncrements = Math.round(rating * 2);
}
@Override
public void onDraw(Canvas canvas) {
int starSize = canvas.getHeight();
mDrawingRect.top = 0;
mDrawingRect.bottom = starSize;
mDrawingRect.left = mIsLayoutLTR ? 0 : (canvas.getWidth() - starSize);
mDrawingRect.right = mDrawingRect.left + starSize;
for (int i = 0; i < MAX_INCREMENT; i += 2) {
Bitmap toShow = mStarEmpty;
if (i < mIncrements) {
boolean isFullStar = (mIncrements - i) >= 2;
toShow = isFullStar ? mStarFull : mStarHalf;
}
canvas.drawBitmap(toShow, null, mDrawingRect, null);
mDrawingRect.left += (mIsLayoutLTR ? 1 : -1) * starSize;
mDrawingRect.right = mDrawingRect.left + starSize;
}
}
}