1 /* 2 * Copyright (C) 2009 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.fuelgauge; 18 19 import android.graphics.Canvas; 20 import android.graphics.ColorFilter; 21 import android.graphics.PixelFormat; 22 import android.graphics.drawable.Drawable; 23 24 /** 25 * A drawable for drawing a bar with a background. 26 */ 27 class PercentageBar extends Drawable { 28 29 Drawable bar; 30 double percent; 31 int lastWidth = -1; 32 33 @Override 34 public void draw(Canvas canvas) { 35 if (lastWidth == -1) { 36 lastWidth = getBarWidth(); 37 bar.setBounds(0, 0, lastWidth, bar.getIntrinsicHeight()); 38 } 39 bar.draw(canvas); 40 } 41 42 @Override 43 public int getOpacity() { 44 return PixelFormat.TRANSLUCENT; 45 } 46 47 @Override 48 public void setAlpha(int alpha) { 49 // Ignore 50 } 51 52 @Override 53 public void setColorFilter(ColorFilter cf) { 54 // Ignore 55 } 56 57 private int getBarWidth() { 58 int width = (int) ((this.getBounds().width() * percent) / 100); 59 int intrinsicWidth = bar.getIntrinsicWidth(); 60 return Math.max(width, intrinsicWidth); 61 } 62 63 @Override 64 public int getIntrinsicHeight() { 65 return bar.getIntrinsicHeight(); 66 } 67 } 68