java - Object allocation during draw/layout? -


i 3 warning 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 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 error on new rect(); , on both new paint();

the exact error avoid object allocation during draw/layout operations (prelocate , reuse instead)

well, 'error' points exact problem. ondraw() method called os many-many times, allocating within function extremely bad idea. need allocate rect , paint beforehand , use them within 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 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 - can moved init()         middlerect.set(0, 400, canvas.getwidth(), 550);          canvas.drawrect(middlerect, ourblue);     } } 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -