Android battery progress bar, battery)
Recently, to create an app, you need to simulate a battery progress bar and set the percentage based on the power usage. The progress bar keeps rolling up and down, just like the battery progress bar when a mobile phone is charged. I customized the view to implement the power progress bar. Modify the image to achieve the desired effect. 1. Customize the View, Battery. java, and refresh the interface cyclically. The two images scroll up and down to continue 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 Batt Ery 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 (Context context) {super (context ); this. mContext = context;} 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 BMP draw = (BitmapDrawable) res. getDrawable (R. drawable. loading_pic); mBmp = BMP draw. 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) {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.003f; if (percentage> 1) {percentage = 0 ;} // after each calculation, a message is sent to the next cycle, and the mHandler interface is refreshed. sendEmptyMessageDelayed (1, speedTime); postInvalidate (); break;} super. handleMessage (msg); postInvalidate () ;}}; // The mHandler interface is refreshed cyclically for the first time. sendEmptyMessageDelayed (1, speedTime) ;}}ii. 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) ;}}3. 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>