仿蚂蚁森林气泡

53次阅读

共计 2912 个字符,预计需要花费 8 分钟才能阅读完成。

高仿支付宝蚂蚁森林气泡 DEMO 版本


public class BubbleView extends View {
    private Paint textPaint;
    private Paint bgPaint;
    private Paint whitePaint;
    private float offset;
    private RectF rectF;
    private RectF innerRectF;
    private int padding = 20;
    private int innerPadding = 35;

    private int BUBBLEOFFSET=35;
    private ValueAnimator valueAnimator;
    private String text="5g";

    public BubbleView(Context context) {super(context);
        init();}

    public BubbleView(Context context, @Nullable AttributeSet attrs) {super(context, attrs);
        init();}

    public BubbleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);
        init();}
    int[] colors = {0xFFCCFE5F,0xffBDF041};
    private void init() {bgPaint=new Paint();
        bgPaint.setColor(Color.GREEN);
        bgPaint.setAntiAlias(true);
        bgPaint.setStrokeWidth(10);
        bgPaint.setStyle(Paint.Style.FILL);

        whitePaint=new Paint();
        whitePaint.setColor(Color.WHITE);
        whitePaint.setAntiAlias(true);
        whitePaint.setStrokeWidth(10);
        whitePaint.setStyle(Paint.Style.STROKE);
        whitePaint.setStrokeCap(Paint.Cap.ROUND);

        textPaint = new Paint();
        textPaint.setColor(0xff339855);
        textPaint.setAntiAlias(true);
        textPaint.setStrokeWidth(35);
        textPaint.setStyle(Paint.Style.FILL);
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setTextSize(75);
        textPaint.setTypeface(Typeface.DEFAULT_BOLD);
        // 文字居中
        Paint.FontMetrics fontMetrics = new Paint.FontMetrics();
        textPaint.getFontMetrics(fontMetrics);
        offset = (fontMetrics.descent + fontMetrics.ascent) / 2;
        // 动画
        valueAnimator = ObjectAnimator.ofFloat(this, "translationY", BUBBLEOFFSET,-BUBBLEOFFSET);
        valueAnimator.setDuration(1500);
        valueAnimator.setRepeatMode(ObjectAnimator.REVERSE);
        valueAnimator.setRepeatCount(INFINITE);
        valueAnimator.start();}
    // 处理 warp_content, 实际项目需要更多的处理
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {int mode= MeasureSpec.getMode(widthMeasureSpec);
       if(mode==MeasureSpec.AT_MOST||mode==MeasureSpec.UNSPECIFIED){setMeasuredDimension(240,240);
       }else{super.onMeasure(widthMeasureSpec, widthMeasureSpec);
       }

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {super.onSizeChanged(w, h, oldw, oldh);
        rectF = new RectF(padding, padding, w - padding, h - padding);
        innerRectF = new RectF(innerPadding, innerPadding, w - innerPadding, h - innerPadding);
        // 渐变色
        Shader shader1 = new RadialGradient(rectF.centerX(), rectF.centerY(),rectF.centerX()-rectF.left,colors,new float[]{0.8f,02f},Shader.TileMode.CLAMP);
        bgPaint.setShader(shader1);
    }

    @Override
    protected void onDraw(Canvas canvas) {super.onDraw(canvas);
        if (rectF == null) return;
        // 背景
        canvas.drawCircle(rectF.centerX(), rectF.centerY(), rectF.centerX()-rectF.left, bgPaint);
        // 高亮部分
        whitePaint.setColor(0xefffffff);
        canvas.drawArc(innerRectF,180,60,false,whitePaint);
        canvas.drawArc(innerRectF,250,2,false,whitePaint);
        whitePaint.setColor(0xaaffffff);
        canvas.drawArc(innerRectF,15,60,false,whitePaint);
        // 文字
        canvas.drawText(text, rectF.centerX(), rectF.centerY() - offset, textPaint);
    }

    @Override
    protected void onDetachedFromWindow() {super.onDetachedFromWindow();
        if(valueAnimator!=null)valueAnimator.cancel();
        valueAnimator=null;
    }
}

正文完
 0