import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
/**
* DrawHook
* Created by Zane on 2015/3/4.
*/
public class DrawHookView extends View {
public static final String TAG = "DrawHookView";
//绘制圆弧的进度值
public int progress = 0;
//线1的x轴
private int line1_x = 0;
//线1的y轴
private int line1_y = 0;
//线2的x轴
private int line2_x = 0;
//线2的y轴
private int line2_y = 0;
private boolean isStart;
private Paint paint;
public DrawHookView(Context context) {
super(context);
init();
}
public DrawHookView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public DrawHookView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
//绘制
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
progress += 3;
//获取圆心的x坐标
int center = getWidth() / 2;
int center1 = center - getWidth() / 5;
//圆弧半径
int radius = getWidth() / 2 - 5;
//定义的圆弧的形状和大小的界限
RectF rectF = new RectF(center - radius - 1, center - radius - 1, center + radius + 1, center + radius + 1);
//根据进度画圆弧
canvas.drawArc(rectF, 235, -360 * progress / 100, false, paint);
/**
* 绘制对勾
*/
//先等圆弧画完,才话对勾
if (progress >= 100) {
if (line1_x < radius / 3) {
line1_x++;
line1_y++;
}
//画第一根线
canvas.drawLine(center1, center, center1 + line1_x, center + line1_y, paint);
if (line1_x == radius / 3) {
line2_x = line1_x;
line2_y = line1_y;
line1_x++;
line1_y++;
}
if (line1_x >= radius / 3 && line2_x <= radius) {
line2_x++;
line2_y--;
if (line2_x > radius) {
mHandler.removeCallbacks(r);
}
}
//画第二根线
canvas.drawLine(center1 + line1_x - 1, center + line1_y, center1 + line2_x, center + line2_y, paint);
}
//每隔10毫秒界面刷新
// postInvalidateDelayed(10);
if (!isStart) {
isStart = true;
mHandler.postDelayed(r, 10);
}
}
Handler mHandler = new Handler();
Runnable r = new Runnable() {
@Override
public void run() {
mHandler.postDelayed(r, 10);
postInvalidate();
};
};
private void init(){
/**
* 绘制圆弧
*/
paint = new Paint();
//设置画笔颜色
paint.setColor(getResources().getColor(R.color.colorAccent));
//设置圆弧的宽
paint.setStrokeWidth(8);
//设置圆弧为空心
paint.setStyle(Paint.Style.STROKE);
//消除锯齿
paint.setAntiAlias(true);
ViewGroup d;
}
}
DrawHookView的实现
最新推荐文章于 2020-08-25 13:30:17 发布