OSDN Git Service

2f174fb73791a17bc120b77c81bd1cea5e4517c2
[android-x86/packages-apps-Settings.git] / src / com / android / settings / deviceinfo / PercentageBarChart.java
1 /*
2  * Copyright (C) 2010 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.settings.deviceinfo;
18
19 import com.android.settings.R;
20
21 import android.content.Context;
22 import android.content.res.TypedArray;
23 import android.graphics.Canvas;
24 import android.graphics.Color;
25 import android.graphics.Paint;
26 import android.util.AttributeSet;
27 import android.view.View;
28
29 import java.util.Collection;
30
31 /**
32  * 
33  */
34 public class PercentageBarChart extends View {
35     private final Paint mEmptyPaint = new Paint();
36
37     private Collection<Entry> mEntries;
38
39     public static class Entry {
40         public final float percentage;
41         public final Paint paint;
42
43         protected Entry(float percentage, Paint paint) {
44             this.percentage = percentage;
45             this.paint = paint;
46         }
47     }
48
49     public PercentageBarChart(Context context, AttributeSet attrs) {
50         super(context, attrs);
51
52         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PercentageBarChart, 0, 0);
53
54         int emptyColor = Color.BLACK;
55
56         int n = a.getIndexCount();
57         for (int i = 0; i < n; i++) {
58             int attr = a.getIndex(i);
59
60             switch (attr) {
61                 case R.styleable.PercentageBarChart_emptyColor:
62                     emptyColor = a.getColor(attr, 0);
63                     break;
64             }
65         }
66
67         a.recycle();
68
69         mEmptyPaint.setColor(emptyColor);
70         mEmptyPaint.setStyle(Paint.Style.FILL);
71     }
72
73     @Override
74     protected void onDraw(Canvas canvas) {
75         super.onDraw(canvas);
76
77         final int left = getPaddingLeft();
78         final int right = getWidth() - getPaddingRight();
79         final int top = getPaddingTop();
80         final int bottom = getHeight() - getPaddingBottom();
81
82         final int width = right - left;
83
84         int lastX = left;
85
86         if (mEntries != null) {
87             for (final Entry e : mEntries) {
88                 final int entryWidth;
89                 if (e.percentage == 0f) {
90                     entryWidth = 0;
91                 } else {
92                     entryWidth = Math.max(1, (int) (width * e.percentage));
93                 }
94
95                 final int nextX = lastX + entryWidth;
96                 if (nextX >= right) {
97                     break;
98                 }
99
100                 canvas.drawRect(lastX, top, nextX, bottom, e.paint);
101                 lastX = nextX;
102             }
103         }
104
105         canvas.drawRect(lastX, top, lastX + width, bottom, mEmptyPaint);
106     }
107
108     /**
109      * Sets the background for this chart. Callers are responsible for later
110      * calling {@link #invalidate()}.
111      */
112     public void setBackgroundColor(int color) {
113         mEmptyPaint.setColor(color);
114     }
115
116     /**
117      * Adds a new slice to the percentage bar chart. Callers are responsible for
118      * later calling {@link #invalidate()}.
119      * 
120      * @param percentage the total width that
121      * @param color the color to draw the entry
122      */
123     public static Entry createEntry(float percentage, int color) {
124         final Paint p = new Paint();
125         p.setColor(color);
126         p.setStyle(Paint.Style.FILL);
127
128         return new Entry(percentage, p);
129     }
130
131     public void setEntries(Collection<Entry> entries) {
132         mEntries = entries;
133     }
134 }