How to make a battery charge in Android: a progress bar for scrolling up and down the picture (battery)

Source: Internet
Author: User

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); }
}

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.