Android 自定义View 启动页面跳转按钮

看到过有个项目中,启动页面一张图片,右上角是一个圆形跳过,并且有一个逐渐缩小的圆弧


首先,先画一个实心灰色圆,在中间画一个跳转字,在外边画一个圆弧  启动动画就设置圆弧的开始角度和结束角度

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);
    }

}


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值