自绘制View-----百分比圆形绘制()
java代码中重写View:
package com.android.systemui.recent; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Style; import android.graphics.RectF; import android.os.Handler; import android.os.Message; import android.util.AttributeSet; import android.view.View; import com.android.systemui.R; public class PercentageView extends View { private RectF mOval; private Paint mPaint = new Paint(); private Paint mPaintBg; private float mSweep; private float mStart; private static final int INCREASE = 0; private static final int DECREASE = 1; private static final int STEP = 3; private static final int DURATION = 10; private static final float STROKE_WIDTH = 2f; private int mDegreeMin; private int mDegreeMax; private Context mContext; private Listener mListener; public interface Listener { public int getDegreeMaxAgain(); public void animEnd(); public void animStart(); } private Handler mHandler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case INCREASE: mSweep += (STEP * 3.6f); if (mSweep >= mDegreeMax * 3.6f) { mSweep = mDegreeMax * 3.6f; invalidate(); this.removeMessages(INCREASE); if (mListener != null) { mListener.animEnd(); } break; } invalidate(); sendEmptyMessageDelayed(INCREASE, DURATION); break; case DECREASE: mSweep -= (STEP * 3.6f); if (mSweep <= mDegreeMin * 3.6f) { mSweep = mDegreeMin * 3.6f; invalidate(); if (mListener != null) { mDegreeMax = mListener.getDegreeMaxAgain(); } this.removeMessages(DECREASE); this.sendEmptyMessage(INCREASE); break; } invalidate(); sendEmptyMessageDelayed(DECREASE, DURATION); break; } } }; public void setDegreeMax(int degree) { mDegreeMax = degree; } public void setDegreeMin(int degree) { mDegreeMin = degree; } public static float dip2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (dpValue * scale + 0.5f); } public static float px2dip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return (pxValue / scale + 0.5f); } public PercentageView(Context context) { this(context, null); } public PercentageView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public PercentageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mContext = context; int bgColor = context.getResources().getColor(R.color.funui_percentage_bg); int degreeColor = context.getResources().getColor(R.color.funui_percentage_degree); this.mPaint.setAntiAlias(true); this.mPaint.setStyle(Paint.Style.STROKE); float width = dip2px(context, STROKE_WIDTH); this.mPaint.setStrokeWidth(width); this.mPaint.setColor(degreeColor); this.mPaint.setAntiAlias(true); this.mPaintBg = new Paint(this.mPaint); this.mPaintBg.setColor(bgColor); mSweep = 0f; mStart = -90f; } private void drawArcs(Canvas paramCanvas, RectF paramRectF, boolean paramBoolean, Paint paramPaint, float paramFloat1, float paramFloat2) { paramCanvas.drawArc(paramRectF, paramFloat1, paramFloat2, paramBoolean, paramPaint); } protected void onDraw(Canvas paramCanvas) { if (this.mOval == null) this.mOval = new RectF(getPaddingLeft(), getPaddingTop(), getWidth() - getPaddingRight(), getHeight() - getPaddingBottom()); drawArcs(paramCanvas, this.mOval, false, this.mPaint, this.mStart, this.mSweep); drawArcs(paramCanvas, this.mOval, false, this.mPaintBg, this.mStart + this.mSweep, 360.0F - this.mSweep); } public void setStrokeWidth(float param) { this.mPaint.setStrokeWidth(param); } public void setListener(Listener listener) { mListener = listener; } public void startAnim(int max, int min) { if (max > min) { setDegreeMax(max); setDegreeMin(min); mHandler.sendEmptyMessage(DECREASE); if (mListener != null) { mListener.animStart(); } } } public void updatePercentage(int degree) { int tmp = Math.min(Math.max(degree, 0), 100); this.mSweep = 3.6f * tmp; invalidate(); } }
xml布局文件加入我们写的View如下:
<com.android.XXXXX.PercentageView android:id="@+id/funui_clear_ring" android:layout_width="60dp" android:layout_height="60dp" android:layout_gravity="center_vertical|center_horizontal" android:padding="16dp" android:visibility="visible" />
java主程序中使用我们的View如下:
private PercentageView mFunuiRingView; mFunuiRingView = (PercentageView) findViewById(R.id.funui_clear_ring); mFunuiRingView.setListener(new PercentageView.Listener() { public int getDegreeMaxAgain() { return degree; } public void animStart() { } public void animEnd() { } }); mFunuiRingView.updatePercentage(usedPecent);
mFunuiRingView.startAnim(degree, 0);