Recently, the creation of an app, the need to simulate a battery of the progress bar, based on the amount of electricity to set a percentage, the progress bar constantly rolling up and down, like the usual mobile phone charging the same battery progress bar. I implemented the power progress bar on my custom view. Modify the picture to achieve the desired effect
First, the custom View,battery.java, the circulation refreshes the interface, two picture scroll up and down, achieves the effect which moves to the right continuously. It's kind of interesting.
Package com.example.battery;
Import Android.content.Context;
Import android.content.res.Resources;
Import Android.graphics.Bitmap;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.Paint;
Import android.graphics.drawable.BitmapDrawable;
Import Android.os.Handler;
Import Android.os.Message;
Import Android.util.AttributeSet;
Import Android.view.View;
public class Battery extends View {public float currentx = 80;
public float currenty = 80;
private float Secondy = 80;
Private Paint Mpaint = new Paint ();
Private context Mcontext;
Private Handler Mhandler;
Private Bitmap mbmp;
private int speedtime = 20;
Private float with = 200;
private float height = 50;
Private float percentage = 0.5f;
Public Battery {Super (context);
This.mcontext = context;
//View more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/ Public Battery (context, AttributeSet set) {Super (context, set);
This.mcontext = context;
Init ();
public void OnDraw (Canvas Canvas) {Super.ondraw (Canvas);
with = This.getwidth ();
Height = this.getheight ();
Mpaint.setcolor (Color.Blue);
Resources res = mcontext.getresources ();
Bitmapdrawable Bmpdraw = (bitmapdrawable) res. getdrawable (r.drawable.loading_pic);
Mbmp = Bmpdraw.getbitmap ();
Canvas.cliprect (0, 0, with*percentage, height);
Canvas.drawbitmap (mbmp, 0, CurrentY, mpaint);
Canvas.drawbitmap (mbmp, 0, Secondy, mpaint);
private void init () {percentage = 0;
Mhandler = new Handler () {public void Handlemessage (msg) {switch (msg.what) {
Case 1:currentx + +; CurrentY + +;
if (mbmp!= null && currenty > Mbmp.getheight ()) {currenty =-mbmp.getheight ();
} if (mbmp!= null) {Secondy = Currenty+mbmp.getheight ();
if (Secondy >= mbmp.getheight ()) {Secondy = Currenty-mbmp.getheight (); } percentage = percentage + 0.0
03f;
if (Percentage > 1) {percentage = 0;
Send a message into the next loop after each calculation and refresh the interface mhandler.sendemptymessagedelayed (1, speedtime);
Postinvalidate ();
Break
Super.handlemessage (msg);
Postinvalidate ();
}
}; First cycle Refresh Interface Mhandler.sendemptymessaGedelayed (1, speedtime); }
}