Android battery progress bar, battery)

Source: Internet
Author: User

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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.