仿螞蟻森林氣泡

高仿支付寶螞蟻森林氣泡DEMO版本
微信圖片_20191104194925.jpg


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;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章