一,效果图:
图一为所做项目效果展示图:
图二为单独这个View的效果图:
二,使用方法:
1,由于是自定义的View,所以需要在attrs.xml里加入如下属性定义:
<?xml version="1.0" encoding="utf-8"?> <resources> <attr name="batteryLevel" format="float" /> <attr name="radius" format="integer" /> <attr name="flowRadius" format="dimension" /> <attr name="textColor" format="color" /> <attr name="textSize" format="dimension" /> <attr name="flowViewColor" format="color" /> <declare-styleable name="BatteryView"> <attr name="batteryLevel" /> <attr name="radius" /> <attr name="flowRadius" /> <attr name="textColor" /> <attr name="textSize" /> <attr name="flowViewColor"/> </declare-styleable> </resources>2,在布局文件中引入:
xmlns:zhangxutong="http://schemas.android.com/apk/res-auto"
<mcxtzhang.weixin521.help.BatteryView android:background="@drawable/battery_view_bg_circle" android:id="@+id/batteryView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:padding="10dp" zhangxutong:flowViewColor="#ffaaffaa"/>以上自定义的属性,如果我们需要自定义波浪滚动的圆球的颜色,则传入 flowViewColor,否则可完全使用默认属性。默认属性如图一,为半透明白色。
3,定义自定义View:
代码如下图:有详细注释:
package mcxtzhang.weixin521.help; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.PaintFlagsDrawFilter; import android.graphics.Path; import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; import android.graphics.Rect; import android.graphics.drawable.BitmapDrawable; import android.util.AttributeSet; import android.util.TypedValue; import android.view.View; import mcxtzhang.weixin521.R; import mcxtzhang.weixin521.utils.TipsUtils; /** * @author zhangxutong */ public class BatteryView extends View { private Context mContext; /** * 电池电量 */ private float level; /** * 控件半径 */ private int radius;