简体   繁体   English

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

[英]Object allocation during draw/layout?

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(); 我在新的Rect()上遇到错误; and on both new Paint(); 并在新的Paint();

The exact error is avoid object allocation during draw/layout operations (prelocate and reuse instead) 确切的错误是在绘制/布局操作期间避免对象分配(预定位和重用)

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. 操作系统多次调用onDraw()方法,因此在这个函数中分配一些东西是非常糟糕的主意。 You need to allocate your Rect and Paint beforehand and just use them within onDraw 您需要事先分配RectPaint ,并在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);
    }
}

For me, I have got this error in layout especially when using display metrices. 对我来说,我在布局中遇到了这个错误,尤其是在使用显示矩阵时。

I have used this imageview : 我使用过这个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; import static 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();

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方法中使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM