Recently, to create an app, you need to simulate a battery power progress bar, depending on the amount of battery to set the percentage, the progress bar constantly scrolling up and down, just like the usual phone charge the same as the battery power progress bar. I've implemented a power progress bar on my custom view. Modify the picture to achieve your desired effect
One, custom View,battery.java, cycle refresh interface, two pictures scroll up and down to achieve the effect of moving to the right. Very 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 ();p Rivat E Context mcontext;private Handler mhandler;private Bitmap mbmp;private int speedtime = 20;private float with = 200;privat e float height = 50;private float percentage = 0.5f;public Battery (context context) {super (context); This.mcontext = Contex t;} Public Battery (context 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 (Message msg) {switch (msg.what) {C ASE 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.003f;if (Percentage > 1) {percentage = 0;} Send a message to the next loop after each calculation and refresh the interface mhandler.sendemptymessagedelayed (1, speedtime);p ostinvalidate (); break;} Super.handlemessage (msg);p ostinvalidate ();}};/ /first Cycle Refresh interface mhandler.sendemptymessagedelayed (1, Speedtime);}}
Second, mainactivity
Package Com.example.battery;import Android.app.activity;import Android.os.bundle;public class MainActivity extends Activity { @Override protected void onCreate (Bundle savedinstancestate) { super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main); }}
Third, Activity_main
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Fill_ Parent " android:layout_height=" fill_parent " android:layout_gravity=" center " android:gravity=" Center "> <com.example.battery.battery android:layout_width=" 300DP " android:layout_height=" 10DP " android:layout_gravity=" center " android:gravity=" center " android:padding=" 10DP "/> </LinearLayout>
Iv. attached picture effect
Five, download path
Download:http://download.csdn.net/detail/lqw770737185/7824695</>
This address: http://www.cnblogs.com/liqw/p/3938422.html
Android battery power progress bar, scroll up and down the picture's progress bar (battery)