Android自定义电源view

2019-07-14 01:17发布


101 是正在充电:会循环显示充电 1-100 自定义显示
import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.RectF; import android.os.Handler; import android.os.Message; import android.util.AttributeSet; import android.view.View; /** * Created by Administrator on 2016/6/25 0025. */ public class BatteryView extends View { private Paint imgPaint; private Paint textPaint; private int batteryColor =0xFF4ECB77; private Bitmap bitmap; private int progress = 101; private int mProgress = 20; private int delay = 1500; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case 101: if (progress == 101) { mProgress+=20; if (mProgress > 100) { mProgress = 20; } invalidate(); handler.sendEmptyMessageDelayed(101,delay) ; } break; } } }; public BatteryView(Context context) { super(context); init(context); } public BatteryView(Context context, AttributeSet attrs) { super(context, attrs); init(context); } public BatteryView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context); } private void init(Context context) { if (bitmap != null) { bitmap.recycle(); } bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ico_battery_bg); imgPaint = new Paint(); imgPaint.setColor(batteryColor); imgPaint.setAntiAlias(true); imgPaint.setAlpha(255); imgPaint.setStyle(Paint.Style.FILL); textPaint = new Paint(); textPaint.setColor(batteryColor); textPaint.setAntiAlias(true); textPaint.setAlpha(255); textPaint.setStyle(Paint.Style.FILL); textPaint.setTextSize(getContext().getResources().getDimensionPixelSize(R.dimen.text_14)); } public void setBatteryColor(int color) { this.batteryColor = color; } public void setProgress(int progress){ if (progress >=0 && progress <= 100) { if (progress <= 20) { batteryColor =0xFFFF0000; } else { batteryColor =0xFF4ECB77; } if (handler.hasMessages(101)) { handler.removeMessages(101); } } else if (progress == 101){ batteryColor =0xFF4ECB77; if (!handler.hasMessages(101)) { handler.sendEmptyMessageDelayed(101, delay); } } this.progress = progress; textPaint.setColor(batteryColor); imgPaint.setColor(batteryColor); invalidate(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int bitmapWidth = bitmap.getWidth(); int bitmapHeight = bitmap.getHeight(); int rectWidth = bitmapWidth - CommonUtils.dip2px(getContext(),7); int rectHeight = bitmapHeight - CommonUtils.dip2px(getContext(),4); RectF rectF = new RectF(0, (getHeight()-bitmapHeight)/2,bitmapWidth,bitmapHeight+(getHeight()-bitmapHeight)/2); canvas.drawBitmap(bitmap,null,rectF,new Paint()); String text = ""; if (progress == 101) { float realWidth = Float.valueOf(rectWidth * mProgress)/100; RectF rectF2 = new RectF(CommonUtils.dip2px(getContext(),2),(getHeight()-rectHeight)/2,realWidth+CommonUtils.dip2px(getContext(),2),rectHeight+(getHeight()-rectHeight)/2); canvas.drawRect(rectF2,imgPaint); text = getContext().getString(R.string.battery_Charging); } else if (progress >=0 && progress <101) { float realWidth = Float.valueOf(rectWidth * progress)/100; RectF rectF2 = new RectF(CommonUtils.dip2px(getContext(),2),(getHeight()-rectHeight)/2,realWidth+CommonUtils.dip2px(getContext(),2),rectHeight+(getHeight()-rectHeight)/2); canvas.drawRect(rectF2,imgPaint); text = progress + "%"; } Paint.FontMetrics fontMetrics = textPaint.getFontMetrics(); float fontHeight = fontMetrics.descent - fontMetrics.ascent; canvas.drawText(text, bitmapWidth + CommonUtils.dip2px(getContext(), 5), (getHeight() + fontHeight) / 2 - CommonUtils.dip2px(getContext(), 5), textPaint); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } }