看到过有个项目中,启动页面一张图片,右上角是一个圆形跳过,并且有一个逐渐缩小的圆弧
首先,先画一个实心灰色圆,在中间画一个跳转字,在外边画一个圆弧 启动动画就设置圆弧的开始角度和结束角度
public class JumpOverView extends View {
private Context context;
//写字画笔
private Paint paintText;
//灰色背景画笔
private Paint paintBg;
//幅度画笔
private Paint paintArc;
//计时器
Timer timer;
private int jindu = 0;
public JumpOverView(Context context) {
super(context);
this.context = context;
init();
}
public JumpOverView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
this.context = context;
init();
}
public JumpOverView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
init();
}
private void init() {
paintText = new Paint();
paintText.setTextSize(35);
paintText.setTextAlign(Paint.Align.CENTER);
paintText.setColor(context.getResources().getColor(R.color.white));
paintText.setAntiAlias(true);
paintBg = new Paint();
paintBg.setStyle(Paint.Style.FILL);
paintBg.setColor(context.getResources().getColor(R.color.textColor99));
paintBg.setAntiAlias(true);
paintArc = new Paint();
paintArc.setColor(context.getResources().getColor(R.color.colorPrimary));
paintArc.setStyle(Paint.Style.STROKE);
paintArc.setStrokeWidth(10);
paintArc.setAntiAlias(true);
//触发动画
this.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
if (jindu < 360) {
jindu = jindu + 10;
} else {
jindu = 360;
timer.cancel();
}
postInvalidate();
}
}, 0, 100); //等待3秒开始 一秒一次
}
});
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(getWidth() / 2, getHeight() / 2, 100, paintBg);
Paint.FontMetrics fontMetrics = paintText.getFontMetrics();
float top1 = fontMetrics.top;//为基线到字体上边框的距离,即上图中的top
float bottom = fontMetrics.bottom;//为基线到字体下边框的距离,即上图中的bottom
int baseLineY = (int) (getHeight() / 2 - top1 / 2 - bottom / 2);//基线中间点的y轴计算公式
canvas.drawText("跳过", getWidth() / 2, baseLineY, paintText);
RectF oval = new RectF(getWidth() / 2 - 100, getHeight() / 2 - 100, getWidth() / 2 + 100, getHeight() / 2 + 100);
canvas.drawArc(oval, -90 + jindu, 360 - jindu, false, paintArc);
}
}