避免创建多层绘图时出现对象分配警告

Avoid object allocations warning appears when creating multi-layer drawing

使用 Java 创建二连词后,3 个区域被突出显示,并且 return 出于某种原因出现警告。我不确定为什么会出现这种情况。如何消除此警告?

Avoid object allocations during draw/layout operations (preallocate and reuse instead)

public class Diagram extends View {
    private int measuredWidth, measuredHeight;
    private Paint mBackgroundPaint, mYellowLinePaint, mWhiteLinePaint;
    private RectF mBackgroundRect, mYellowLineRectF, mWhiteLineRectF;


    public Diagram(Context context) {
        super(context);
        init(context, null, 0);
    }

    public Diagram(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public Diagram(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    private void init(Context context, AttributeSet attributeSet, int defStyle) {

        mBackgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBackgroundPaint.setColor(0xFF3C3C3C);
        mBackgroundPaint.setStyle(Paint.Style.FILL);

        mYellowLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mYellowLinePaint.setColor(0xFFFFFF00);
        mYellowLinePaint.setStyle(Paint.Style.FILL);

        mWhiteLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mWhiteLinePaint.setColor(0xFFFFFFFF);
        mWhiteLinePaint.setStyle(Paint.Style.FILL);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        measuredHeight = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
        measuredWidth = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);

        mBackgroundRect = new RectF(0, 0, measuredWidth, measuredHeight);
        mYellowLineRectF = new RectF(0, 0.2f * measuredHeight, measuredWidth, 0.3f * measuredHeight);
        mWhiteLineRectF = new RectF(0, 0.0f * measuredHeight, measuredWidth, 0.1f * measuredHeight);

        setMeasuredDimension(measuredWidth, measuredHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (measuredHeight == 0 || measuredWidth == 0)
            return;

        canvas.drawRect(mBackgroundRect, mBackgroundPaint);
        canvas.drawRect(mYellowLineRectF, mYellowLinePaint);
        canvas.drawRect(mWhiteLineRectF, mWhiteLinePaint);
    }
}

更新代码

public class Diagram extends View {
    private int measuredWidth, measuredHeight;
    private Paint mBackgroundPaint, mYellowLinePaint, mWhiteLinePaint;

    private final RectF mBackgroundRect = new RectF();
    private final RectF mYellowLineRectF = new RectF();
    private final RectF mWhiteLineRectF = new RectF();

    public Diagram(Context context) {
        super(context);
        init(context, null, 0);
    }

    public Diagram(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public Diagram(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    private void init(Context context, AttributeSet attributeSet, int defStyle) {

        mBackgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBackgroundPaint.setColor(0xFF3C3C3C);
        mBackgroundPaint.setStyle(Paint.Style.FILL);

        mYellowLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mYellowLinePaint.setColor(0xFFFFFF00);
        mYellowLinePaint.setStyle(Paint.Style.FILL);

        mWhiteLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mWhiteLinePaint.setColor(0xFFFFFFFF);
        mWhiteLinePaint.setStyle(Paint.Style.FILL);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        measuredHeight = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
        measuredWidth = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);

        mBackgroundRect.set(0, 0, measuredWidth, measuredHeight);
        mYellowLineRectF.set(0, 0.2f * measuredHeight, measuredWidth, 0.3f * measuredHeight);
        mWhiteLineRectF.set(0, 0.0f * measuredHeight, measuredWidth, 0.1f * measuredHeight);

        setMeasuredDimension(measuredWidth, measuredHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (measuredHeight == 0 || measuredWidth == 0)
            return;

        canvas.drawRect(mBackgroundRect, mBackgroundPaint);
        canvas.drawRect(mYellowLineRectF, mYellowLinePaint);
        canvas.drawRect(mWhiteLineRectF, mWhiteLinePaint);
    }
}

改为在 class 构造函数或字段初始值设定项中创建 3 个 RectF 实例,然后在 onMeasure().

中使用 RectF.set()
public class Diagram extends View {
    private final RectF mBackgroundRect = new RectF();
    private final RectF mYellowLineRectF = new RectF();
    private final RectF mWhiteLineRectF = new RectF();

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        measuredHeight = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
        measuredWidth = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);

        mBackgroundRect.set(0, 0, measuredWidth, measuredHeight);
        mYellowLineRectF.set(0, 0.2f * measuredHeight, measuredWidth, 0.3f * measuredHeight);
        mWhiteLineRectF.set(0, 0.0f * measuredHeight, measuredWidth, 0.1f * measuredHeight);

        setMeasuredDimension(measuredWidth, measuredHeight);
    }
}