|
| 1 | +/* |
| 2 | + * Copyright 2014 OpenMarket Ltd |
| 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 | +package im.vector.view; |
| 17 | + |
| 18 | +import android.content.Context; |
| 19 | +import android.content.res.TypedArray; |
| 20 | +import android.graphics.Canvas; |
| 21 | +import android.graphics.Paint; |
| 22 | +import android.graphics.RectF; |
| 23 | +import android.util.AttributeSet; |
| 24 | +import android.view.View; |
| 25 | + |
| 26 | +import im.vector.R; |
| 27 | + |
| 28 | +/** |
| 29 | + * View that displays a disc representing a percentage. |
| 30 | + */ |
| 31 | +public class PieFractionView extends View { |
| 32 | + |
| 33 | + private static final int START_ANGLE = -90; |
| 34 | + |
| 35 | + // fraction between 0 and 100 |
| 36 | + private int fraction = 0; |
| 37 | + |
| 38 | + private final RectF rectF; |
| 39 | + private final Paint paint; |
| 40 | + |
| 41 | + private int fillColor; |
| 42 | + private int restColor; |
| 43 | + |
| 44 | + public int getFillColor() { |
| 45 | + return getResources().getColor(R.color.vector_green_color); |
| 46 | + } |
| 47 | + |
| 48 | + public int getRestColor() { |
| 49 | + return getResources().getColor(R.color.vector_dark_grey_color); |
| 50 | + } |
| 51 | + |
| 52 | + @SuppressWarnings("ResourceType") |
| 53 | + public PieFractionView(Context context, AttributeSet attrs) { |
| 54 | + super(context, attrs); |
| 55 | + |
| 56 | + int[] attrArray = new int[] {android.R.attr.layout_width, android.R.attr.layout_height}; |
| 57 | + TypedArray typedArray = context.obtainStyledAttributes(attrs, attrArray); |
| 58 | + int width = typedArray.getDimensionPixelSize(0, 0); |
| 59 | + int height = typedArray.getDimensionPixelSize(1, 0); |
| 60 | + typedArray.recycle(); |
| 61 | + |
| 62 | + rectF = new RectF(0, 0, width, height); |
| 63 | + paint = new Paint(); |
| 64 | + |
| 65 | + fillColor = getFillColor(); |
| 66 | + restColor = getRestColor(); |
| 67 | + } |
| 68 | + |
| 69 | + public void setFraction(int fraction) { |
| 70 | + // check bounds and avoid useless refresh |
| 71 | + if ((0 <= fraction) && (fraction <= 100) && (this.fraction != fraction)) { |
| 72 | + this.fraction = fraction; |
| 73 | + invalidate(); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + protected void onDraw(Canvas canvas) { |
| 79 | + int angle = fraction * 360 / 100; |
| 80 | + |
| 81 | + // draw the fill section |
| 82 | + paint.setColor(fillColor); |
| 83 | + canvas.drawArc(rectF, START_ANGLE, angle, true, paint); |
| 84 | + |
| 85 | + // draw the rest of the circle |
| 86 | + paint.setColor(restColor); |
| 87 | + canvas.drawArc(rectF, START_ANGLE + angle, 360 - angle, true, paint); |
| 88 | + } |
| 89 | +} |
0 commit comments