java 绘制/布局期间的对象分配?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16472529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:03:56  来源:igfitidea点击:

Object allocation during draw/layout?

javaandroid

提问by lramos15

I get 3 warning for object allocation during draw/layout

我在绘制/布局期间收到 3 个对象分配警告

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

我在 new Rect() 上遇到错误;和两个 new Paint();

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

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

回答by Pavel Dudka

Well, your 'error' points to exact problem. onDraw()method is called by OS many-many times, thus allocating something within this function is extremely bad idea. You need to allocate your Rectand Paintbeforehand and just use them within onDraw

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

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

回答by Steve

For me, I have got this error in layout especially when using display metrices.

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

I have used this imageview :

我用过这个图像视图:

   <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:

ScalableImageView.java:

import static com.steve.TabHomeActivity.displaymetrics;

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

public class ScalableImageView extends ImageView {


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

}

I solved this issue by adding the static DisplayMetrices in onCreate() method in Activity.

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

TabHomeActivity.java:

TabHomeActivity.java:

public static DisplayMetrics displaymetrics;

*inside onCreate()*

 displaymetrics = new DisplayMetrics();

回答by Gustek

avoid object allocation during draw/layout operations (prelocate and reuse instead)

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

The message give You a solution on plate, so I don't understand what is Your question.

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

You have to move creation of new objects to onCreate method, so they are created just once and use them later in onDraw method.

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