气泡shader_仿蚂蚁森林气泡

高仿支付宝蚂蚁森林气泡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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值