感觉话一个圆形进度条挺简单的 ,但是却偏偏给了几张图片让你话,说实话我没接触过,感觉好难,还好百度有大把的资源,一番努力下终于画出来了。 代码如下。
package com.etong.cpms.widget;
import java.net.Proxy.Type;
import com.etong.cpms.activity.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Paint.FontMetricsInt;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
/**
*
*/
public class MyView extends View {
private Bitmap bmpBg;
private Bitmap bmpForbg;
private Bitmap bmpDb;
private Bitmap bmpNq;
private PorterDuffXfermode mMode;
private Paint mXferPaint;
private RectF mOval;
private int mPercent;
private Paint paint;
private Rect targetRect;
private FontMetricsInt fontMetrics;
/**
*
*
* @param context
*/
public MyView(Context context) {
super(context);
}
int widthPixels;
int heightPixels;
int centerX;
int centerY;
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
widthPixels = w;
heightPixels = h;
centerX = widthPixels / 2;
centerY = heightPixels / 2;
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
setFocusable(true);
// setFocusableInTouchMode(true);
bmpBg = (Bitmap) BitmapFactory.decodeResource(getResources(),
R.drawable.wq);
bmpForbg = (Bitmap) BitmapFactory.decodeResource(getResources(),
R.drawable.mdwq);
bmpDb = (Bitmap) BitmapFactory.decodeResource(getResources(),
R.drawable.db);
bmpNq = (Bitmap) BitmapFactory.decodeResource(getResources(), R.drawable.nq);
mMode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);//锟斤拷锟斤拷
mXferPaint = new Paint();
mXferPaint.setXfermode(mMode);//锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷图片锟洁交时模式
mOval = new RectF();
mOval.left = 0;
mOval.top = 0;
mPercent = 0;
paint = new Paint();
paint.setColor(Color.GREEN);
paint.setTypeface(Typeface.DEFAULT_BOLD);
paint.setTextSize(60);
targetRect = new Rect(centerX - bmpBg.getWidth() / 2, centerY - bmpBg.getHeight() / 2, centerX + bmpBg.getWidth() / 2,
centerY + bmpBg.getWidth() / 2);
fontMetrics = paint.getFontMetricsInt();
}
/**
*
*/
@Override
protected void onDraw(Canvas canvas) {
mXferPaint.setXfermode(null);
canvas.drawBitmap(bmpBg, centerX - bmpBg.getWidth() / 2, centerY - bmpBg.getHeight() / 2, mXferPaint);
int saveCount = canvas.saveLayer(centerX - bmpBg.getWidth() / 2, centerY - bmpBg.getHeight() / 2,centerX + bmpBg.getWidth() / 2,
centerY + bmpBg.getWidth() / 2, null, Canvas.MATRIX_SAVE_FLAG
| Canvas.CLIP_SAVE_FLAG
| Canvas.HAS_ALPHA_LAYER_SAVE_FLAG
| Canvas.FULL_COLOR_LAYER_SAVE_FLAG
| Canvas.CLIP_TO_LAYER_SAVE_FLAG);
// mOval.left = 100;
// mOval.top = 100;
// mOval.right = 100 + bmpBg.getWidth();
// mOval.bottom = 100 + bmpBg.getHeight();
mOval.left = centerX - bmpBg.getWidth() / 2;
mOval.top = centerY - bmpBg.getHeight() / 2;
mOval.right = centerX + bmpBg.getWidth() / 2;
mOval.bottom = centerY + bmpBg.getWidth() / 2;
mXferPaint.setXfermode(null);
canvas.drawArc(mOval, -90, 360 * mPercent / 100, true, mXferPaint);
mXferPaint.setXfermode(mMode);
// canvas.drawBitmap(bmpForbg, 100, 100, mXferPaint);
canvas.drawBitmap(bmpForbg, centerX - bmpForbg.getWidth() / 2, centerY - bmpForbg.getHeight() / 2, mXferPaint);
canvas.restoreToCount(saveCount);
int baseline = targetRect.top
+ (targetRect.bottom - targetRect.top - fontMetrics.bottom + fontMetrics.top)
/ 2 - fontMetrics.top;
//
paint.setTextAlign(Align.CENTER);
paint.setTextAlign(Align.CENTER);
canvas.drawBitmap(bmpNq, centerX - bmpNq.getWidth() / 2, centerY - bmpNq.getHeight() / 2, paint);
canvas.drawText(mPercent + "%", centerX, centerY+80, paint);
canvas.drawBitmap(bmpDb, centerX - bmpDb.getWidth() / 2, centerY - bmpDb.getHeight() / 2-50, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
mPercent++;
mPercent %= 100;
//
invalidate();
// postInvalidate();
return true;
}
}