Android实现绘制发光效果

共 2051字,需浏览 5分钟

 ·

2022-05-22 18:30

之前在看别人写自定义view作绘制的时候,看到别人家的view自带发光效果,看起来也是蛮炫酷的,于是自己也抽出时间来试用一下,这里做了一个模仿太阳的各种状态样式。

先上效果先上效果:



实现方式:
public BlurMaskFilter(float radius, Blur style) 


实现是使用的Paint类的setMaskFilter()方法,传入BlurMaskFilter对象实现高斯模糊发光。

  • float radius  设置模糊半径


  • Blur style   设置发光样式,包括
    Blur.INNER(内发光),Blur.NORMAL(内外发光)
    Blur.SOLID(外发光),Blur.OUTER(仅外发光可见)


贴上代码:
public class MaskFilterView extends View {    private Paint lightPaint;    private int centerX, centerY;    /** 发光范围 */    private int radioRadius = 70;
public MaskFilterView(Context context) { super(context); init(); }
public MaskFilterView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(); }
private void init() { lightPaint = new Paint(); setLayerType(LAYER_TYPE_SOFTWARE, null); lightPaint.setColor(Color.parseColor("#EC3E3E")); lightPaint.setMaskFilter(new BlurMaskFilter(radioRadius, BlurMaskFilter.Blur.INNER)); }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { centerX = getLeft() + getMeasuredWidth()/2; centerY = getTop() + getMeasuredHeight()/2; super.onMeasure(widthMeasureSpec, widthMeasureSpec); }
public void setBlurType(int blurType) { switch (blurType) { case 0: lightPaint.setMaskFilter(new BlurMaskFilter(radioRadius, BlurMaskFilter.Blur.INNER)); break; case 1: lightPaint.setMaskFilter(new BlurMaskFilter(radioRadius, BlurMaskFilter.Blur.NORMAL)); break; case 2: lightPaint.setMaskFilter(new BlurMaskFilter(radioRadius, BlurMaskFilter.Blur.SOLID)); break; case 3: lightPaint.setMaskFilter(new BlurMaskFilter(radioRadius, BlurMaskFilter.Blur.OUTER)); break; }
invalidate(); }
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas);
canvas.drawCircle(centerX, centerY, 150, lightPaint); }}


到这里就结束啦。
浏览 70
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报