23

I get 3 warning for object allocation during draw/layout

super.onDraw(canvas);
canvas.drawColor(Color.WHITE);
Paint textPaint = new Paint();
textPaint.setARGB(50,100,100,250);
textPaint.setTextAlign(Align.CENTER);
textPaint.setTextSize(50);
textPaint.setTypeface(font);
canvas.drawText("Logan is awesom",canvas.getWidth()/2,200,textPaint);
canvas.drawBitmap(pBall, (canvas.getWidth()/2), changingY, null);
if (changingY <canvas.getHeight()){
changingY += 10;
}else{
changingY=0;
}
Rect middleRect = new Rect();
middleRect.set(0, 400, canvas.getWidth(), 550);
Paint ourBlue = new Paint();
ourBlue.setColor(Color.BLUE);
canvas.drawRect(middleRect, ourBlue);

I get an error on new Rect(); and on both new Paint();

The exact error is avoid object allocation during draw/layout operations (prelocate and reuse instead)

4

3 回答 3

35

好吧,您的“错误”指向确切的问题。onDraw()方法被操作系统多次调用,因此在这个函数中分配一些东西是非常糟糕的主意。你需要预先分配你的Rectand并在其中使用它们PaintonDraw

class YourClass extends View
{
    Rect middleRect;
    Paint ourBlue;
    Paint textPaint;

    public YourClass()
    {
         //constructor
         init();
    }

    private void init()
    {
        middleRect = new Rect();
        ourBlue; = new Paint();
        textPaint = new Paint();

        ourBlue.setColor(Color.BLUE);
        textPaint.setARGB(50,100,100,250);
        textPaint.setTextAlign(Align.CENTER);
        textPaint.setTextSize(50);
        textPaint.setTypeface(font);
    }

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

        canvas.drawText("Logan is awesom",canvas.getWidth()/2,200,textPaint);
        canvas.drawBitmap(pBall, (canvas.getWidth()/2), changingY, null);
        if (changingY <canvas.getHeight()){
            changingY += 10;
        }else{
            changingY=0;
        }

        //if canvas size doesn't change - this can be moved to init() as well
        middleRect.set(0, 400, canvas.getWidth(), 550);

        canvas.drawRect(middleRect, ourBlue);
    }
}
于 2013-05-10T00:12:14.740 回答
0

对我来说,我在布局中遇到了这个错误,尤其是在使用显示度量时。

我用过这个图像视图:

   <com.steve.thirdparty.ScalableImageView
        android:id="@+id/iv_posted_img_home"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:background="@drawable/golive_load_image"
        android:layout_below="@+id/tv_user_posted_msg_post_items_home"
        android:contentDescription="@string/cont_desc"/>

ScalableImageView.java:

导入静态 com.steve.TabHomeActivity.displaymetrics;

public class ScalableImageView extends ImageView {


        ((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);

}

我通过在 Activity 的 onCreate() 方法中添加静态 DisplayMetrices 解决了这个问题。

TabHomeActivity.java:

public static DisplayMetrics displaymetrics;

*inside onCreate()*

 displaymetrics = new DisplayMetrics();
于 2016-10-11T12:04:10.667 回答
-4

在绘制/布局操作期间避免对象分配(而不是预先定位和重用)

该消息为您提供了一个解决方案,所以我不明白您的问题是什么。

您必须将新对象的创建移至 onCreate 方法,因此它们只创建一次并稍后在 onDraw 方法中使用它们。

于 2013-05-10T00:00:37.683 回答