public MaterialRatingDrawable(Context context, boolean fillBackgroundStars) { super(new Drawable[] { createLayerDrawableWithTintAttrRes(fillBackgroundStars ? R.drawable.mrb_star_icon_black_36dp : R.drawable.mrb_star_border_icon_black_36dp, fillBackgroundStars ? R.attr.colorControlHighlight : R.attr.colorControlNormal, context), fillBackgroundStars ? createClippedLayerDrawableWithTintColor( R.drawable.mrb_star_icon_black_36dp, Color.TRANSPARENT, context) : createClippedLayerDrawableWithTintAttrRes( R.drawable.mrb_star_border_icon_black_36dp, R.attr.colorControlActivated, context), createClippedLayerDrawableWithTintAttrRes(R.drawable.mrb_star_icon_black_36dp, R.attr.colorControlActivated, context) }); setId(0, android.R.id.background); setId(1, android.R.id.secondaryProgress); setId(2, android.R.id.progress); }
@SuppressLint("RtlHardcoded") private static Drawable createClippedLayerDrawableWithTintColor(int tileResId, int tintColor, Context context) { return new ClipDrawableCompat(createLayerDrawableWithTintColor(tileResId, tintColor, context), Gravity.LEFT, ClipDrawableCompat.HORIZONTAL); }
public void setStarCount(int count) { getTileDrawableByLayerId(android.R.id.background).setTileCount(count); getTileDrawableByLayerId(android.R.id.secondaryProgress).setTileCount(count); getTileDrawableByLayerId(android.R.id.progress).setTileCount(count); }
a.recycle(); mDrawable = new MaterialRatingDrawable(getContext(), fillBackgroundStars); mDrawable.setStarCount(getNumStars()); setProgressDrawable(mDrawable);
@SuppressLint("RtlHardcoded") private static Drawable createClippedLayerDrawableWithTintAttrRes(int tileResId, int tintAttrRes, Context context) { return new ClipDrawableCompat(createLayerDrawableWithTintAttrRes(tileResId, tintAttrRes, context), Gravity.LEFT, ClipDrawableCompat.HORIZONTAL); }
@Override public void setNumStars(int numStars) { super.setNumStars(numStars); // mDrawable can be null during super class initialization. if (mDrawable != null) { mDrawable.setStarCount(numStars); } }
@SuppressLint("NewApi") private TileDrawable getTileDrawableByLayerId(int id) { Drawable layerDrawable = findDrawableByLayerId(id); switch (id) { case android.R.id.background: return (TileDrawable) layerDrawable; case android.R.id.secondaryProgress: case android.R.id.progress: { ClipDrawableCompat clipDrawable = (ClipDrawableCompat) layerDrawable; return (TileDrawable) clipDrawable.getDrawable(); } default: // Should never reach here. throw new RuntimeException(); } } }
@Override protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int height = getMeasuredHeight(); int width = Math.round(height * mDrawable.getTileRatio() * getNumStars()); setMeasuredDimension(View.resolveSizeAndState(width, widthMeasureSpec, 0), height); }
public float getTileRatio() { Drawable drawable = getTileDrawableByLayerId(android.R.id.progress).getDrawable(); return (float) drawable.getIntrinsicWidth() / drawable.getIntrinsicHeight(); }
private static Drawable createLayerDrawableWithTintAttrRes(int tileRes, int tintAttrRes, Context context) { int tintColor = ThemeUtils.getColorFromAttrRes(tintAttrRes, context); return createLayerDrawableWithTintColor(tileRes, tintColor, context); }