Index: chrome/android/java/src/org/chromium/chrome/browser/banners/RatingView.java |
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/banners/RatingView.java b/chrome/android/java/src/org/chromium/chrome/browser/banners/RatingView.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..536ad4ed6a3a74292a9c4b2810da1befbb3ac486 |
--- /dev/null |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/banners/RatingView.java |
@@ -0,0 +1,99 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+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; |
+ |
+/** |
+ * Displays a set of stars representing a rating for an app. |
newt (away)
2014/02/19 21:52:49
Yay! Class-level JavaDoc!
|
+ */ |
+public class RatingView extends View { |
+ private static final int MAX_INCREMENT = 10; |
+ |
+ // Bitmaps shared across all RatingViews. |
+ private static Bitmap sStarFull; |
newt (away)
2014/02/19 21:52:49
advantage of statics: If many RatingViews exist si
gone
2014/02/19 23:31:43
Done.
|
+ private static Bitmap sStarHalf; |
+ private static Bitmap sStarEmpty; |
David Trainor- moved to gerrit
2014/02/20 01:22:27
Ah I knew it!
|
+ |
+ /** Stores whether or not the layout is left-to-right. */ |
+ private final boolean mIsLayoutLTR; |
+ |
+ /** Variables used for drawing. */ |
+ private final Rect mDrawingRect; |
+ |
+ /** Each increment represents 0.5 stars. */ |
+ private int mIncrements; |
+ |
+ public RatingView(Context context, AttributeSet params) { |
+ super(context, params); |
+ mIsLayoutLTR = !LocalizationUtils.isSystemLayoutDirectionRtl(); |
+ mDrawingRect = new Rect(); |
+ } |
+ |
+ /** |
+ * Initializes the RatingView. |
+ * @param rating How many stars to display. |
+ */ |
+ void initialize(float rating) { |
+ // Ratings are rounded to the nearest 0.5 increment, like in the Play Store. |
+ mIncrements = Math.round(rating * 2); |
+ |
+ // Cache the Bitmaps for any future banners that appear. |
+ if (sStarFull == null) { |
+ Resources res = getContext().getResources(); |
newt (away)
2014/02/19 21:52:49
Simpler:
Resources res = getResources();
gone
2014/02/19 23:31:43
Done.
|
+ |
+ Bitmap starHalf = BitmapFactory.decodeResource(res, R.drawable.btn_star_mini_half); |
newt (away)
2014/02/19 21:52:49
suggestion for simplifying this a bit:
sStarH
gone
2014/02/19 23:31:43
Problem is then you can't mark the bitmap as final
|
+ if (mIsLayoutLTR) { |
+ sStarHalf = starHalf; |
+ } else { |
+ // RTL mode requires flipping the Bitmap. |
+ int width = starHalf.getWidth(); |
+ int height = starHalf.getHeight(); |
+ Matrix m = new Matrix(); |
+ m.preScale(-1, 1); |
+ sStarHalf = Bitmap.createBitmap(starHalf, 0, 0, width, height, m, false); |
+ } |
+ |
+ sStarFull = BitmapFactory.decodeResource(res, R.drawable.btn_star_mini_full); |
+ sStarEmpty = BitmapFactory.decodeResource(res, R.drawable.btn_star_mini_empty); |
+ } |
+ } |
+ |
+ @Override |
+ public void onDraw(Canvas canvas) { |
+ int starDimension = canvas.getHeight(); |
newt (away)
2014/02/19 21:52:49
Is this the 5th dimension?? Just an opinion, but,
gone
2014/02/19 23:31:43
Yep, shot right past the 4th.
|
+ |
+ // Start off on the left for LTR mode, on the right for RTL. |
+ mDrawingRect.top = 0; |
+ mDrawingRect.bottom = starDimension; |
+ mDrawingRect.left = mIsLayoutLTR ? 0 : (canvas.getWidth() - starDimension); |
+ mDrawingRect.right = mDrawingRect.left + starDimension; |
+ |
+ // Draw all the stars. |
+ for (int i = 0; i < MAX_INCREMENT; i += 2) { |
+ Bitmap toShow = sStarEmpty; |
+ if (i < mIncrements) { |
+ boolean isFullStar = (mIncrements - i) >= 2; |
+ toShow = isFullStar ? sStarFull : sStarHalf; |
+ } |
+ canvas.drawBitmap(toShow, null, mDrawingRect, null); |
+ |
+ // Scooch over to show the next star. |
+ mDrawingRect.left += (mIsLayoutLTR ? 1 : -1) * starDimension; |
+ mDrawingRect.right = mDrawingRect.left + starDimension; |
+ } |
+ } |
+} |