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

标签 java android xml android-layout android-drawable

使用 Java 创建二图后,3 个区域会突出显示,并由于某种原因返回警告。我不确定为什么会出现这种情况。可以采取什么措施来消除此警告?

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

enter image description here enter image description here

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);
    }
}

最佳答案

在类构造函数或字段初始值设定项中创建 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);
    }
}

关于java - 创建多层绘图时避免出现对象分配警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31685191/

相关文章:

java - 有没有办法从executeBatch获取所有错误?

java - 如何在jmeter中使用.csv文件的单元格值?

xml - 为什么我使用 Ansible XML 模块获得带有命名空间的 XML 文档的 "does not reference a node"?

python - 为什么 OpenERP 报表查询结果与 SQL 查询结果不同?

java - jfreechart:向甘特图添加十字线覆盖

java - 在 Java 中访问本地机器证书存储?

java - Android - 连接到指定的 wifi 并检查 SSID

java - 自 API 29 以来,小部件点击振动不起作用

android - Sencha 架构师——部署

php - iTunes 最近添加的 XML 未显示更新结果