▲ Android 边框提示效果

前言

最近公司给我安排个效果,就是当网络差或者断网的时候,边框有警告效果。正好用最近复习的自定义控件来练手。

效果图

img

代码实现

public class WarningView extends View {

    private int mWarningColor;
    private Paint mPaint;
    private int mBorderWidth = 10;
    public WarningView(Context context) {
        this(context,null);
    }

    public WarningView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public WarningView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setLayerType(LAYER_TYPE_SOFTWARE, null);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.WarningView);
        mWarningColor = array.getColor(R.styleable.WarningView_warningColor, mWarningColor);
        mBorderWidth = (int) array.getDimension(R.styleable.WarningView_borderWidth, mBorderWidth);
        array.recycle();
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(mBorderWidth);
        mPaint.setColor(mWarningColor);
        mPaint.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(width,height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setMaskFilter(new BlurMaskFilter(20, BlurMaskFilter.Blur.SOLID));
        RectF rectF = new RectF(0, 0, getWidth(), getHeight());
        canvas.drawRoundRect(rectF, 0, 0, mPaint);
        
    }
}

		

attrs.xml 文件

```
<declare-styleable name="WarningView">
    <attr name="warningColor" format="color" />
    <attr name="borderWidth" format="dimension" />
</declare-styleable>

```

MainActivity 代码

```
    ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(mWarning, "alpha", 1f, 0.5f,0f);
    objectAnimator.setRepeatCount(ValueAnimator.INFINITE);
    objectAnimator.setDuration(2400);
    mWarning.setVisibility(View.VISIBLE);
    objectAnimator.start();
```
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值