Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 自定義電池電量進度條:上下滾動動畫效果實現

編輯:關於 Android 程式設計

近期在開發一款 App 時,需要模擬一個電池電量的進度條,根據電量百分比填充對應區域,並讓圖片不斷上下滾動,呈現出手機充電時那種動態效果。於是我通過自定義 View 實現了這個電量進度條——只需替換圖片,就能達到各自想要的視覺效果。

一、自定義 View:Battery.java

核心思路是循環刷新界面,讓兩張圖片上下滾動,從而營造出持續流動的錯覺。

 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(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 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) {
                    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;
                        }
                        // 每次計算後發送消息進入下一次循環並刷新界面
                        mHandler.sendEmptyMessageDelayed(1, speedTime);
                        postInvalidate();
                        break;
                }
                super.handleMessage(msg);
                postInvalidate();
            }
        };
        // 首次循環刷新界面
        mHandler.sendEmptyMessageDelayed(1, speedTime);
    }
}

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

三、activity_main 佈局

 <LinearLayout xmlns:android="https://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>

整體而言,這個自定義 View 借助 Handler 週期性地更新坐標並調用 postInvalidate() 觸發重繪,配合 clipRect 控制填充比例,就實現了靈活的電池電量動態進度條。替換 loading_pic 這張圖片,即可自訂外觀。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved