Android 电池电量进度条,上下滚动图片的进度条(battery)

Android 电池电量进度条,上下滚动图片的进度条(battery),第1张

概述模拟一个电池电量进度条,根据电量多少来设置百分比,进度条不断上下滚动,就像平时手机充电一样的电池电量进度条。

最近,制作一个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,with*percentage,height);				canvas.drawBitmap(mBmp,currentY,mPaint);				canvas.drawBitmap(mBmp,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="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>

四、附图片效果

 

五、下载路径

下载:http://download.csdn.net/detail/lqw770737185/7824695</>

本文地址:http://www.cnblogs.com/liqw/p/3938422.html

 

总结

以上是内存溢出为你收集整理的Android 电池电量进度条,上下滚动图片的进度条(battery)全部内容,希望文章能够帮你解决Android 电池电量进度条,上下滚动图片的进度条(battery)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1119277.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存