1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.launcher3.discovery; 18 19 import android.content.Context; 20 import android.graphics.Canvas; 21 import android.graphics.drawable.ClipDrawable; 22 import android.graphics.drawable.Drawable; 23 import android.util.AttributeSet; 24 import android.view.Gravity; 25 import android.view.View; 26 27 import com.android.launcher3.R; 28 29 /** 30 * A simple rating view that shows stars with a rating from 0-5. 31 */ 32 public class RatingView extends View { 33 34 private static final float WIDTH_FACTOR = 0.9f; 35 private static final int MAX_LEVEL = 10000; 36 private static final int MAX_STARS = 5; 37 38 private final Drawable mStarDrawable; 39 private final int mColorGray; 40 private final int mColorHighlight; 41 42 private float rating; 43 RatingView(Context context)44 public RatingView(Context context) { 45 this(context, null); 46 } 47 RatingView(Context context, AttributeSet attrs)48 public RatingView(Context context, AttributeSet attrs) { 49 this(context, attrs, 0); 50 } 51 RatingView(Context context, AttributeSet attrs, int defStyle)52 public RatingView(Context context, AttributeSet attrs, int defStyle) { 53 super(context, attrs, defStyle); 54 mStarDrawable = getResources().getDrawable(R.drawable.ic_star_rating, null); 55 mColorGray = 0x1E000000; 56 mColorHighlight = 0x8A000000; 57 } 58 setRating(float rating)59 public void setRating(float rating) { 60 this.rating = Math.min(Math.max(rating, 0), MAX_STARS); 61 } 62 63 @Override onDraw(Canvas canvas)64 protected void onDraw(Canvas canvas) { 65 drawStars(canvas, MAX_STARS, mColorGray); 66 drawStars(canvas, rating, mColorHighlight); 67 } 68 drawStars(Canvas canvas, float stars, int color)69 private void drawStars(Canvas canvas, float stars, int color) { 70 int fullWidth = getLayoutParams().width; 71 int cellWidth = fullWidth / MAX_STARS; 72 int starWidth = (int) (cellWidth * WIDTH_FACTOR); 73 int padding = cellWidth - starWidth; 74 int fullStars = (int) stars; 75 float partialStarFactor = stars - fullStars; 76 77 for (int i = 0; i < fullStars; i++) { 78 int x = i * cellWidth + padding; 79 Drawable star = mStarDrawable.getConstantState().newDrawable().mutate(); 80 star.setTint(color); 81 star.setBounds(x, padding, x + starWidth, padding + starWidth); 82 star.draw(canvas); 83 } 84 if (partialStarFactor > 0f) { 85 int x = fullStars * cellWidth + padding; 86 ClipDrawable star = new ClipDrawable(mStarDrawable, 87 Gravity.LEFT, ClipDrawable.HORIZONTAL); 88 star.setTint(color); 89 star.setLevel((int) (MAX_LEVEL * partialStarFactor)); 90 star.setBounds(x, padding, x + starWidth, padding + starWidth); 91 star.draw(canvas); 92 } 93 } 94 } 95