Add custom attributes to DonutView.

Note: These changes are required for Data balance UI.
Bug: 62349208
Test: Manual
Change-Id: I50f304580f681dc1461fb82d84ce0fede21b4b87
This commit is contained in:
Rajeev Kumar
2017-07-10 11:26:39 -07:00
parent 250595ae11
commit eaceb1906e
2 changed files with 55 additions and 12 deletions

View File

@@ -171,4 +171,12 @@
<attr name="android:gravity" /> <attr name="android:gravity" />
</declare-styleable> </declare-styleable>
<!-- For DonutView -->
<declare-styleable name="DonutView">
<attr name="meterBackgroundColor" format="color" />
<attr name="meterConsumedColor" format="color" />
<attr name="applyColorAccent" format="boolean" />
<attr name="showPercentString" format="boolean" />
<attr name="thickness" format="dimension" />
</declare-styleable>
</resources> </resources>

View File

@@ -17,16 +17,17 @@ package com.android.settings.widget;
import android.content.Context; import android.content.Context;
import android.content.res.Resources; import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.ColorFilter; import android.graphics.ColorFilter;
import android.graphics.Paint; import android.graphics.Paint;
import android.graphics.PorterDuff; import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter; import android.graphics.PorterDuffColorFilter;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.support.annotation.ColorRes;
import android.text.TextPaint; import android.text.TextPaint;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.View; import android.view.View;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.Utils; import com.android.settings.Utils;
@@ -46,6 +47,7 @@ public class DonutView extends View {
private TextPaint mBigNumberPaint; private TextPaint mBigNumberPaint;
private String mPercentString; private String mPercentString;
private String mFullString; private String mFullString;
private boolean mShowPercentString = true;
public DonutView(Context context) { public DonutView(Context context) {
super(context); super(context);
@@ -53,29 +55,50 @@ public class DonutView extends View {
public DonutView(Context context, AttributeSet attrs) { public DonutView(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
mStrokeWidth = context.getResources().getDimension(R.dimen.storage_donut_thickness); int meterBackgroundColor = context.getColor(R.color.meter_background_color);
final ColorFilter mAccentColorFilter = int meterConsumedColor = Utils.getDefaultColor(mContext, R.color.meter_consumed_color);
new PorterDuffColorFilter( boolean applyColorAccent = true;
Utils.getColorAttr(context, android.R.attr.colorAccent), Resources resources = context.getResources();
PorterDuff.Mode.SRC_IN); mStrokeWidth = resources.getDimension(R.dimen.storage_donut_thickness);
if (attrs != null) {
TypedArray styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.DonutView);
meterBackgroundColor = styledAttrs.getColor(R.styleable.DonutView_meterBackgroundColor,
meterBackgroundColor);
meterConsumedColor = styledAttrs.getColor(R.styleable.DonutView_meterConsumedColor,
meterConsumedColor);
applyColorAccent = styledAttrs.getBoolean(R.styleable.DonutView_applyColorAccent,
true);
mShowPercentString = styledAttrs.getBoolean(R.styleable.DonutView_showPercentString,
true);
mStrokeWidth = styledAttrs.getDimensionPixelSize(R.styleable.DonutView_thickness,
(int) mStrokeWidth);
styledAttrs.recycle();
}
mBackgroundCircle = new Paint(); mBackgroundCircle = new Paint();
mBackgroundCircle.setAntiAlias(true); mBackgroundCircle.setAntiAlias(true);
mBackgroundCircle.setStrokeCap(Paint.Cap.BUTT); mBackgroundCircle.setStrokeCap(Paint.Cap.BUTT);
mBackgroundCircle.setStyle(Paint.Style.STROKE); mBackgroundCircle.setStyle(Paint.Style.STROKE);
mBackgroundCircle.setStrokeWidth(mStrokeWidth); mBackgroundCircle.setStrokeWidth(mStrokeWidth);
mBackgroundCircle.setColorFilter(mAccentColorFilter); mBackgroundCircle.setColor(meterBackgroundColor);
mBackgroundCircle.setColor(context.getColor(R.color.meter_background_color));
mFilledArc = new Paint(); mFilledArc = new Paint();
mFilledArc.setAntiAlias(true); mFilledArc.setAntiAlias(true);
mFilledArc.setStrokeCap(Paint.Cap.BUTT); mFilledArc.setStrokeCap(Paint.Cap.BUTT);
mFilledArc.setStyle(Paint.Style.STROKE); mFilledArc.setStyle(Paint.Style.STROKE);
mFilledArc.setStrokeWidth(mStrokeWidth); mFilledArc.setStrokeWidth(mStrokeWidth);
mFilledArc.setColor(Utils.getDefaultColor(mContext, R.color.meter_consumed_color)); mFilledArc.setColor(meterConsumedColor);
mFilledArc.setColorFilter(mAccentColorFilter);
if (applyColorAccent) {
final ColorFilter mAccentColorFilter =
new PorterDuffColorFilter(
Utils.getColorAttr(context, android.R.attr.colorAccent),
PorterDuff.Mode.SRC_IN);
mBackgroundCircle.setColorFilter(mAccentColorFilter);
mFilledArc.setColorFilter(mAccentColorFilter);
}
Resources resources = context.getResources();
mTextPaint = new TextPaint(); mTextPaint = new TextPaint();
mTextPaint.setColor(Utils.getColorAccent(getContext())); mTextPaint.setColor(Utils.getColorAccent(getContext()));
mTextPaint.setAntiAlias(true); mTextPaint.setAntiAlias(true);
@@ -98,7 +121,9 @@ public class DonutView extends View {
protected void onDraw(Canvas canvas) { protected void onDraw(Canvas canvas) {
super.onDraw(canvas); super.onDraw(canvas);
drawDonut(canvas); drawDonut(canvas);
drawInnerText(canvas); if (mShowPercentString) {
drawInnerText(canvas);
}
} }
private void drawDonut(Canvas canvas) { private void drawDonut(Canvas canvas) {
@@ -154,6 +179,16 @@ public class DonutView extends View {
invalidate(); invalidate();
} }
public void setMeterBackgroundColor(@ColorRes int meterBackgroundColor) {
mBackgroundCircle.setColor(meterBackgroundColor);
invalidate();
}
public void setMeterConsumedColor(@ColorRes int meterConsumedColor) {
mFilledArc.setColor(meterConsumedColor);
invalidate();
}
private float getTextHeight(TextPaint paint) { private float getTextHeight(TextPaint paint) {
// Technically, this should be the cap height, but I can live with the descent - ascent. // Technically, this should be the cap height, but I can live with the descent - ascent.
return paint.descent() - paint.ascent(); return paint.descent() - paint.ascent();