GuaGuaKa
类
package com.zhy.view;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Paint.Style;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.v4.widget.SlidingPaneLayout.PanelSlideListener;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import com.example.zhy_path.R;
public class GuaGuaKa extends View
{
/**
* 绘制线条的Paint,即用户手指绘制Path
*/
private Paint mOutterPaint = new Paint();
/**
* 记录用户绘制的Path
*/
private Path mPath = new Path();
/**
* 内存中创建的Canvas
*/
private Canvas mCanvas;
/**
* mCanvas绘制内容在其上
*/
private Bitmap mBitmap;
/**
* ------------------------以下是奖区的一些变量
*/
// private Bitmap mBackBitmap;
private boolean isComplete;
private Paint mBackPint = new Paint();
private Rect mTextBound = new Rect();
private String mText = "¥500,0000";
private int mLastX;
private int mLastY;
public GuaGuaKa(Context context)
{
this(context, null);
}
public GuaGuaKa(Context context, AttributeSet attrs)
{
this(context, attrs, 0);
}
public GuaGuaKa(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
init();
}
private void init()
{
mPath = new Path();
// mBackBitmap = BitmapFactory.decodeResource(getResources(),
// R.drawable.t2);
setUpOutPaint();
setUpBackPaint();
}
/**
* 初始化canvas的绘制用的画笔
*/
private void setUpBackPaint()
{
mBackPint.setStyle(Style.FILL);
mBackPint.setTextScaleX(2f);
mBackPint.setColor(Color.DKGRAY);
mBackPint.setTextSize(32);
mBackPint.getTextBounds(mText, 0, mText.length(), mTextBound);
}
@Override
protected void onDraw(Canvas canvas)
{
// canvas.drawBitmap(mBackBitmap, 0, 0, null);
// 绘制奖项
canvas.drawText(mText, getWidth() / 2 - mTextBound.width() / 2,
getHeight() / 2 + mTextBound.height() / 2, mBackPint);
if (!isComplete)
{
drawPath();
canvas.drawBitmap(mBitmap, 0, 0, null);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = getMeasuredWidth();
int height = getMeasuredHeight();
// 初始化bitmap
mBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
// 绘制遮盖层
// mCanvas.drawColor(Color.parseColor("#c0c0c0"));
mOutterPaint.setStyle(Paint.Style.FILL);
mCanvas.drawRoundRect(new RectF(0, 0, width, height), 30, 30,
mOutterPaint);
mCanvas.drawBitmap(BitmapFactory.decodeResource(getResources(),
R.drawable.s_title), null, new RectF(0, 0, width, height), null);
}
/**
* 设置画笔的一些参数
*/
private void setUpOutPaint()
{
// 设置画笔
// mOutterPaint.setAlpha(0);
mOutterPaint.setColor(Color.parseColor("#c0c0c0"));
mOutterPaint.setAntiAlias(true);
mOutterPaint.setDither(true);
mOutterPaint.setStyle(Paint.Style.STROKE);
mOutterPaint.setStrokeJoin(Paint.Join.ROUND); // 圆角
mOutterPaint.setStrokeCap(Paint.Cap.ROUND); // 圆角
// 设置画笔宽度
mOutterPaint.setStrokeWidth(20);
}
/**
* 绘制线条
*/
private void drawPath()
{
mOutterPaint.setStyle(Paint.Style.STROKE);
mOutterPaint
.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
mCanvas.drawPath(mPath, mOutterPaint);
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
int action = event.getAction();
int x = (int) event.getX();
int y = (int) event.getY();
switch (action)
{
case MotionEvent.ACTION_DOWN:
mLastX = x;
mLastY = y;
mPath.moveTo(mLastX, mLastY);
break;
case MotionEvent.ACTION_MOVE:
int dx = Math.abs(x - mLastX);
int dy = Math.abs(y - mLastY);
if (dx > 3 || dy > 3)
mPath.lineTo(x, y);
mLastX = x;
mLastY = y;
break;
case MotionEvent.ACTION_UP:
new Thread(mRunnable).start();
break;
}
invalidate();
return true;
}
/**
* 统计擦除区域任务
*/
private Runnable mRunnable = new Runnable()
{
private int[] mPixels;
@Override
public void run()
{
int w = getWidth();
int h = getHeight();
float wipeArea = 0;
float totalArea = w * h;
Bitmap bitmap = mBitmap;
mPixels = new int[w * h];
/**
* 拿到所有的像素信息
*/
bitmap.getPixels(mPixels, 0, w, 0, 0, w, h);
/**
* 遍历统计擦除的区域
*/
for (int i = 0; i < w; i++)
{
for (int j = 0; j < h; j++)
{
int index = i + j * w;
if (mPixels[index] == 0)
{
wipeArea++;
}
}
}
/**
* 根据所占百分比,进行一些操作
*/
if (wipeArea > 0 && totalArea > 0)
{
int percent = (int) (wipeArea * 100 / totalArea);
Log.e("TAG", percent + "");
if (percent > 70)
{
Log.e("TAG", "清除区域达到70%,下面自动清除");
isComplete = true;
postInvalidate();
}
}
}
};
}
CircleProcessView
类
package com.zhy.view;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import com.example.zhy_path.R;
/**
* Created by Administrator on 2016/11/1 0001.
*/
public class CircleProcessView extends View {
private Context mContext;
private float circleWidth; //圆环的宽度
private Paint paint, mPaint, nPaint;
private Drawable circlebackground;
private Drawable roundbackground;
private int circleColor;
private int roundProgressColor;
private int circleProgressColor;
private float progress = 0; //当前进度
private float maxProgress; //最大进度
private float circleMargin; //与控件四边的间距
private float circleArc; //当前进度与初始进度的夹角
private float curPoint_x; //当前进度图标的x,y值
private float curPoint_y;
public CircleProcessView(Context context) {
super(context);
mContext=context;
}
public CircleProcessView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext=context;
setCustomAttributes(attrs);
}
public CircleProcessView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mContext=context;
setCustomAttributes(attrs);
}
private void setCustomAttributes(AttributeSet attrs) {
paint = new Paint();
mPaint = new Paint();
TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.CircleProgressView);
circleMargin = a.getColor(R.styleable.CircleProgressView_circleMargin, 0);
maxProgress = a.getColor(R.styleable.CircleProgressView_circleMaxProcess, 100);
circleColor = a.getColor(R.styleable.CircleProgressView_circleColor, Color.RED);
circleProgressColor = a.getColor(R.styleable.CircleProgressView_circleProgressColor, Color.GREEN);
roundProgressColor = a.getColor(R.styleable.CircleProgressView_roundProgressColor, Color.GREEN);
circlebackground = a.getDrawable(R.styleable.CircleProgressView_circleBackground);
roundbackground = a.getDrawable(R.styleable.CircleProgressView_roundBackground);
circleWidth = a.getDimension(R.styleable.CircleProgressView_circleWidth, 5);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setStrokeWidth(circleWidth); //设置圆环的宽度
mPaint.setColor(roundProgressColor); //设置进度的颜色
mPaint.setAntiAlias(true); //消除锯齿
mPaint.setStyle(Paint.Style.STROKE);
nPaint = new Paint();
nPaint.setStrokeWidth(circleWidth);
nPaint.setColor(roundProgressColor);
nPaint.setAntiAlias(true); //消除锯齿
nPaint.setStyle(Paint.Style.FILL);
// Bitmap mbmp = BitmapFactory.decodeResource(getResources(), R.drawable.icon_position);
/*
* 画圆背景
*/
BitmapDrawable bd = (BitmapDrawable) roundbackground;
float left = circleWidth + circleMargin - 1;
float top = circleWidth + circleMargin - 1;
float right = getWidth() - left;
float bottom = getHeight() - top;
RectF dst = new RectF(left, top, right, bottom);
canvas.drawBitmap(bd.getBitmap(), null, dst, mPaint);
Bitmap mbmp = BitmapFactory.decodeResource(getResources(), R.drawable.psb);
/**
* 画最外层的大圆环
*/
float centre = getWidth() / 2; //获取圆心的x坐标
float cRadius = (centre - circleMargin - circleWidth / 2); //圆环的半径
paint.setColor(circleColor); //设置圆环的颜色
paint.setStyle(Paint.Style.STROKE); //设置空心
paint.setStrokeWidth(circleWidth); //设置圆环的宽度
paint.setAntiAlias(true); //消除锯齿
canvas.drawCircle(centre, centre, cRadius, paint); //画出圆环
/**
* 画圆弧 ,画圆环的进度
*/
circleArc = 360 * progress / maxProgress;
float x0 = getWidth() / 2;
float y0 = getHeight() / 2 - (getWidth() / 2 - circleMargin - circleWidth + mbmp.getHeight() / 2);
float r0 = getHeight() / 2 - y0;
curPoint_x = (float) (x0 + r0 * Math.sin(circleArc * Math.PI / 180));
curPoint_y = (float) (y0 + r0 * (1 - Math.cos(circleArc * Math.PI / 180)));
RectF oval = new RectF(centre - cRadius, centre - cRadius, centre + cRadius, centre + cRadius); //用于定义的圆弧的形状和大小的界限
//mPaint.setStrokeCap(Paint.Cap.ROUND);//设置圆角
canvas.drawArc(oval, 270, circleArc, false, mPaint); //根据进度画圆弧
/**
* 进度图标
*/
RectF recf = new RectF(getWidth() * 0.5f - circleWidth * 0.5f + circleWidth / 10.0f, circleWidth + circleMargin - circleWidth - circleWidth / 10.0f,
getWidth() * 0.5f + circleWidth * 0.5f - circleWidth / 10.0f, circleWidth + circleMargin);
canvas.save();
canvas.rotate(circleArc, getWidth() / 2, getHeight() / 2);
canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));
canvas.drawBitmap(mbmp, null, recf, mPaint);
canvas.restore();
}
/**
* 获取进度.需要同步
* @return
*/
public synchronized float getProgress() {
return progress;
}
/**
* 设置进度,此为线程安全控件,由于考虑多线的问题,需要同步
* 刷新界面调用postInvalidate()能在非UI线程刷新
* @param progress
*/
public synchronized void setProgress(float progress) {
if(progress < 0){
throw new IllegalArgumentException("progress not less than 0");
}
if(progress > maxProgress){
progress = maxProgress;
}
if(progress <= maxProgress){
this.progress = progress;
postInvalidate();
}
}
}