BatteryLevelView.java
Go to the documentation of this file.
00001 package com.github.rosjava.android_extras.gingerbread.view;
00002 
00003 import com.github.rosjava.android_extras.gingerbread.R;
00004 
00005 import android.content.Context;
00006 import android.graphics.Bitmap;
00007 import android.graphics.BitmapFactory;
00008 import android.graphics.Canvas;
00009 import android.graphics.Paint;
00010 import android.graphics.PorterDuffColorFilter;
00011 import android.graphics.Rect;
00012 import android.graphics.PorterDuff;
00013 import android.util.AttributeSet;
00014 import android.view.View;
00015 
00016 public class BatteryLevelView extends View {
00017           private Bitmap silhouette;
00018           private Bitmap plug;
00019           private Paint green;
00020           private Paint yellow;
00021           private Paint red;
00022           private Paint gray;
00023           private float levelPercent;
00024           private boolean pluggedIn;
00025           public BatteryLevelView(Context ctx) {
00026             super(ctx);
00027             init(ctx);
00028           }
00029           public BatteryLevelView(Context context, AttributeSet attrs, int defStyle) {
00030             super(context, attrs, defStyle);
00031             init(context);
00032           }
00033           public BatteryLevelView(Context context, AttributeSet attrs) {
00034             super(context, attrs);
00035             init(context);
00036           }
00037 
00038           private Paint makePaint( int color ) {
00039             Paint paint = new Paint();
00040             paint.setColorFilter( new PorterDuffColorFilter( 0xff000000 | color, PorterDuff.Mode.SRC_ATOP ));
00041             return paint;
00042           }
00043           private void init(Context context) {
00044             silhouette = BitmapFactory.decodeResource(context.getResources(), R.drawable.battery_silhouette);
00045             plug = BitmapFactory.decodeResource(context.getResources(), R.drawable.battery_charging);
00046             green = makePaint( 0x00ff00 );
00047             yellow = makePaint( 0xffff00 );
00048             red = makePaint( 0xff0000 );
00049             gray = makePaint( 0x808080 );
00050             levelPercent = 0;
00051             pluggedIn = false;
00052           }
00053           public void setBatteryPercent(float percent) {
00054             levelPercent = percent;
00055             invalidate();
00056           }
00057           public void setPluggedIn(boolean plugged) {
00058             pluggedIn = plugged;
00059             invalidate();
00060           }
00061           @Override
00062           protected void onDraw(Canvas canvas) {
00063             super.onDraw(canvas);
00064             // draw the entire background battery image in gray
00065             Rect srcRect = new Rect(0, 0, silhouette.getWidth(), silhouette.getHeight());
00066             Rect destRect = new Rect(0, 0, getWidth(), getHeight());
00067             canvas.drawBitmap(silhouette, srcRect, destRect, gray);
00068             Paint fillPaint;
00069             if( levelPercent < 20 ) {
00070               fillPaint = red;
00071             } else if( levelPercent < 50 ) {
00072               fillPaint = yellow;
00073             } else {
00074               fillPaint = green;
00075             }
00076 
00077             // draw a portion of the foreground battery image with the width coming from levelPercent.
00078             srcRect.set(0, 0, (int)(silhouette.getWidth() * levelPercent / 100f), silhouette.getHeight());
00079             destRect.set(0, 0, (int)(getWidth() * levelPercent / 100f), getHeight());
00080             canvas.drawBitmap(silhouette, srcRect, destRect, fillPaint);
00081 
00082             if( pluggedIn ) {
00083               srcRect.set(0, 0, plug.getWidth(), plug.getHeight());
00084               destRect.set(0,0,getWidth(), getHeight());
00085               canvas.drawBitmap(plug, srcRect, destRect, new Paint());
00086             }
00087           }
00088         }


android_extras
Author(s): Daniel Stonier
autogenerated on Thu Jun 6 2019 20:13:19