gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:29:22 27 4
gpt4 key购买 nike

使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com