android画图性能分析

android画图的性能分析
<wbr style="line-height:25px">一,往直接View里画图</wbr><wbr style="line-height:25px">。<br style="line-height:25px"> 用onDraw(Canvasg)向View里画图.<br style="line-height:25px"> 我在模拟器上测序了4种向View里画320X480图的性能:<br style="line-height:25px"><wbr style="line-height:25px"><span style="color:#993300; line-height:25px">首先</span><span style="color:#000080; line-height:25px">,画</span><span style="color:#0000ff; line-height:25px">inmutable</span><span style="color:#000080; line-height:25px">的bitmap图最快。(9毫秒)</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">其次</span><span style="color:#000080; line-height:25px">,画</span><span style="color:#0000ff; line-height:25px">mutable</span><span style="color:#000080; line-height:25px">的bitmap图比较慢。(19毫秒)</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">再其次</span><span style="color:#000080; line-height:25px">,画</span><span style="color:#0000ff; line-height:25px">非Alpha</span><span style="color:#000080; line-height:25px">的RGB数据更慢。(34毫秒)</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">最后</span><span style="color:#000080; line-height:25px">,画</span><span style="color:#993300; line-height:25px">Alpha</span><span style="color:#000080; line-height:25px">的</span><span style="color:#0000ff; line-height:25px">RGB</span><span style="color:#000080; line-height:25px">数据最慢。(43毫秒)</span><wbr style="line-height:25px"><br style="line-height:25px"> 测试代码1:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">longtimes[]=newlong[4];<br style="line-height:25px"> intcnt=0;<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">protectedvoid</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">onDraw</span><span style="color:#3366ff; line-height:25px">(Canvasg)<br style="line-height:25px"> {<br style="line-height:25px"> longtime=System.currentTimeMillis();<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">if</span><span style="color:#3366ff; line-height:25px">(img!=null)<br style="line-height:25px"> {<br style="line-height:25px"> booleanhasAlpha=!true;<br style="line-height:25px"> time=System.currentTimeMillis();<br style="line-height:25px"> g.drawBitmap(img.getBitMap(),0,0,null);<br style="line-height:25px"> time=(System.currentTimeMillis()-time);<br style="line-height:25px"> times[0]+=time;<br style="line-height:25px"> int[]colors=newint[Game.width*Game.height];<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">for</span><span style="color:#3366ff; line-height:25px">(inti=0;i&lt;colors.length;i++)<br style="line-height:25px"> colors<wbr style="line-height:25px">=0xFF0000FF;<br style="line-height:25px"> BitmapbitMap=Bitmap.createBitmap(colors,Game.width,Game.height,Bitmap.Config.ARGB_8888);<br style="line-height:25px"> time=System.currentTimeMillis();<br style="line-height:25px"> g.drawBitmap(bitMap,0,0,null);<br style="line-height:25px"> time=(System.currentTimeMillis()-time);<br style="line-height:25px"> times[1]+=time;<br style="line-height:25px"> time=System.currentTimeMillis();<br style="line-height:25px"> hasAlpha=false;<br style="line-height:25px"> g.drawBitmap(colors,0,Game.width,0,0,Game.width,Game.height,hasAlpha,null);<br style="line-height:25px"> time=(System.currentTimeMillis()-time);<br style="line-height:25px"> times[2]+=time;<br style="line-height:25px"><br style="line-height:25px"> time=System.currentTimeMillis();<br style="line-height:25px"> for(inti=0;i&lt;colors.length;i++)<br style="line-height:25px"></wbr></span><span style="line-height:25px; font-style:normal"><span style="color:#3366ff; line-height:25px">colors<wbr style="line-height:25px">=0xF0000FFF;<br style="line-height:25px"> hasAlpha=true;<br style="line-height:25px"> time=System.currentTimeMillis();<br style="line-height:25px"> g.drawBitmap(colors,0,Game.width,0,0,Game.width,Game.height,hasAlpha,null);<br style="line-height:25px"> time=(System.currentTimeMillis()-time);<br style="line-height:25px"> times[3]+=time;<br style="line-height:25px"> cnt++;<br style="line-height:25px"> }<br style="line-height:25px"></wbr></span><span style="color:#993300; line-height:25px">if</span><span style="color:#3366ff; line-height:25px">(cnt%10==0)<br style="line-height:25px"> {<br style="line-height:25px"> System.out.println("runcnt:"+cnt);<br style="line-height:25px"> System.out.println("timeformutablebitmap:"+times[0]<br style="line-height:25px"> +"inmutablebitmap:"+times[1]<br style="line-height:25px"> +"noalphacolors:"+times[2]<br style="line-height:25px"> +"Alphaclors:"+times[3]);<br style="line-height:25px"> System.out.println("averagetimeformutable:"+times[0]/cnt<br style="line-height:25px"> +"inmutable:"+times[1]/cnt<br style="line-height:25px"> +"noalphacolors:"+times[2]/cnt<br style="line-height:25px"> +"Alphaclors:"+times[3]/cnt);<br style="line-height:25px"> }<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">elseif</span><span style="color:#3366ff; line-height:25px">(cnt&gt;0xFFFFFFF)<br style="line-height:25px"> {<br style="line-height:25px"> cnt=0;<br style="line-height:25px"> times[0]=0;<br style="line-height:25px"> times[1]=0;<br style="line-height:25px"> times[2]=0;<br style="line-height:25px"> times[3]=0;<br style="line-height:25px"> }<br style="line-height:25px"> Paintp=newPaint();<br style="line-height:25px"> p.setColor(0xFF0000FF);<br style="line-height:25px"> g.drawLine(0,420,Game.width,420,p);<br style="line-height:25px"> g.drawLine(0,20,Game.width-20,Game.height,p);<br style="line-height:25px"> g.drawText("hello",50,50,p);<br style="line-height:25px"> blServiceDraw=false;<br style="line-height:25px"> }</span></span><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">二,往Bitmap里画图。</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="color:#000080; line-height:25px">通过如下的方式来实现向Bitmap画图</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">if(bufferBitMap==null)<br style="line-height:25px"> bufferBitMap=Bitmap.createBitmap(Game.width,Game.height,Bitmap.Config.ARGB_8888);<br style="line-height:25px"> g=newCanvas(bufferBitMap);</span><br style="line-height:25px"><span style="color:#000080; line-height:25px">可以创建一个mutable的bitmap,然后得到它的画柄Canvas,</span><br style="line-height:25px"><span style="color:#000080; line-height:25px">就可以往Bitmap里画图</span><br style="line-height:25px"> 我在模拟器上测序了4种向<span style="line-height:25px"><wbr style="line-height:25px">ARGB_8888</wbr></span><wbr style="line-height:25px">格式的Bitmap里画320X480图的性能:<br style="line-height:25px"><wbr style="line-height:25px"><span style="color:#993300; line-height:25px">首先</span><span style="color:#000080; line-height:25px">,画</span><span style="color:#99cc00; line-height:25px">inmutable</span><span style="color:#000080; line-height:25px">的</span><span style="color:#0000ff; line-height:25px">bitmap</span><span style="color:#000080; line-height:25px">图最快。(5毫秒)</span><br style="line-height:25px"><span style="color:#000080; line-height:25px">注意:它比向View直接画inmutable的bitmap图(9毫秒)快很多,快了4秒</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">其次</span><span style="color:#000080; line-height:25px">,画</span><span style="color:#808000; line-height:25px">mutable</span><span style="color:#000080; line-height:25px">的</span><span style="color:#0000ff; line-height:25px">bitmap</span><span style="color:#000080; line-height:25px">图比较慢。(11毫秒)</span><br style="line-height:25px"><span style="color:#000080; line-height:25px">注意:它比向View里直接画mutable的bitmap图(19毫秒)快很多,快了8秒</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">再其次</span><span style="color:#000080; line-height:25px">,画</span><span style="color:#339966; line-height:25px">Alpha</span><span style="color:#000080; line-height:25px">的</span><span style="color:#0000ff; line-height:25px">RGB</span><span style="color:#000080; line-height:25px">数据更慢。(39毫秒)</span><br style="line-height:25px"><span style="color:#000080; line-height:25px">注意:它居然比向View里直接画Alpha的RGB数据(43毫秒)快</span><br style="line-height:25px"><span style="color:#000080; line-height:25px">但是它仍然比向View里直接画非Alpha的RGB数据(34毫秒)慢</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">最后</span><span style="color:#000080; line-height:25px">,画</span><span style="color:#008000; line-height:25px">非Alpha</span><span style="color:#000080; line-height:25px">的</span><span style="color:#0000ff; line-height:25px">RGB</span><span style="color:#000080; line-height:25px">数据最慢。(47毫秒)<br style="line-height:25px"> 注意:它的表现太差了。它比向View里直接画Alpha的RGB数据(43毫秒)都要慢<wbr style="line-height:25px">。</wbr></span><br style="line-height:25px"> 为什么在往Bitmap里画非Alpha的RGB的数据还要慢呢?<br style="line-height:25px"> Puzzle!<br style="line-height:25px"> 测试代码2:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">longtimes[]=newlong[4];</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">intcnt=0;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">BitmapbufferBitMap;</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">protectedvoid</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">onDraw</span><span style="color:#3366ff; line-height:25px">(Canvasg)<br style="line-height:25px"> {<br style="line-height:25px"> longtime=System.currentTimeMillis();<br style="line-height:25px"> Canvasgg=g;<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">if</span><span style="color:#3366ff; line-height:25px">(bufferBitMap==null)<br style="line-height:25px"> bufferBitMap=Bitmap.createBitmap(Game.width,Game.height,</span><span style="color:#99cc00; line-height:25px">Bitmap.Config.ARGB_8888)</span><span style="color:#3366ff; line-height:25px">;</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">g=newCanvas(bufferBitMap);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">if</span><span style="color:#3366ff; line-height:25px">(img!=null)<br style="line-height:25px"> {<br style="line-height:25px"> booleanhasAlpha=!true;<br style="line-height:25px"> time=System.currentTimeMillis();<br style="line-height:25px"> g.drawBitmap(img.getBitMap(),0,0,null);<br style="line-height:25px"> time=(System.currentTimeMillis()-time);<br style="line-height:25px"> times[0]+=time;<br style="line-height:25px"> int[]colors=newint[Game.width*Game.height];<br style="line-height:25px"> for(inti=0;i&lt;colors.length;i++)<br style="line-height:25px"> colors<wbr style="line-height:25px">=0xFF0000FF;<br style="line-height:25px"> BitmapbitMap=Bitmap.createBitmap(colors,Game.width,Game.height,Bitmap.Config.ARGB_8888);<br style="line-height:25px"> time=System.currentTimeMillis();<br style="line-height:25px"> g.drawBitmap(bitMap,0,0,null);<br style="line-height:25px"> time=(System.currentTimeMillis()-time);<br style="line-height:25px"> times[1]+=time;<br style="line-height:25px"> time=System.currentTimeMillis();<br style="line-height:25px"> hasAlpha=false;<br style="line-height:25px"> g.drawBitmap(colors,0,Game.width,0,0,Game.width,Game.height,hasAlpha,null);<br style="line-height:25px"> time=(System.currentTimeMillis()-time);<br style="line-height:25px"> times[2]+=time;<br style="line-height:25px"><br style="line-height:25px"> time=System.currentTimeMillis();<br style="line-height:25px"> for(inti=0;i&lt;colors.length;i++)<br style="line-height:25px"> colors<wbr style="line-height:25px">=0xF0000FFF;<br style="line-height:25px"> hasAlpha=true;<br style="line-height:25px"> time=System.currentTimeMillis();<br style="line-height:25px"> g.drawBitmap(colors,0,Game.width,0,0,Game.width,Game.height,hasAlpha,null);<br style="line-height:25px"> time=(System.currentTimeMillis()-time);<br style="line-height:25px"> times[3]+=time;<br style="line-height:25px"> cnt++;<br style="line-height:25px"> }<br style="line-height:25px"></wbr></wbr></span><span style="color:#993300; line-height:25px">if</span><span style="color:#3366ff; line-height:25px">(cnt%10==0)<br style="line-height:25px"> {<br style="line-height:25px"> System.out.println("runcnt:"+cnt);<br style="line-height:25px"> System.out.println("timeformutablebitmap:"+times[0]<br style="line-height:25px"> +"inmutablebitmap:"+times[1]<br style="line-height:25px"> +"noalphacolors:"+times[2]<br style="line-height:25px"> +"Alphaclors:"+times[3]);<br style="line-height:25px"> System.out.println("averagetimeformutable:"+times[0]/cnt<br style="line-height:25px"> +"inmutable:"+times[1]/cnt<br style="line-height:25px"> +"noalphacolors:"+times[2]/cnt<br style="line-height:25px"> +"Alphaclors:"+times[3]/cnt);<br style="line-height:25px"> }<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">elseif</span><span style="color:#3366ff; line-height:25px">(cnt&gt;0xFFFFFFF)<br style="line-height:25px"> {<br style="line-height:25px"> cnt=0;<br style="line-height:25px"> times[0]=0;<br style="line-height:25px"> times[1]=0;<br style="line-height:25px"> times[2]=0;<br style="line-height:25px"> times[3]=0;<br style="line-height:25px"> }<br style="line-height:25px"> g=gg;<br style="line-height:25px"> Paintp=newPaint();<br style="line-height:25px"> p.setColor(0xFF0000FF);<br style="line-height:25px"> g.drawLine(0,420,Game.width,420,p);<br style="line-height:25px"> g.drawLine(0,20,Game.width-20,Game.height,p);<br style="line-height:25px"> g.drawText("hello",50,50,p);<br style="line-height:25px"> blServiceDraw=false;<br style="line-height:25px"> }</span><br style="line-height:25px"> 如是把测试代码2中的<span style="color:#99cc00; line-height:25px">ARGB_8888</span>格式改为<span style="color:#808000; line-height:25px">RGB_565</span>格式,得出以下结论:<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">首先,</wbr></span><span style="color:#000080; line-height:25px">画inmutable的bitmap图最快。(9毫秒)</span><br style="line-height:25px"><wbr style="line-height:25px">注意:它和向View直接画inmutable的bitmap图(9毫秒)<span style="line-height:25px"><wbr style="line-height:25px">一样,</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"> 但是它比用ARGB_8888格式的(5毫秒)<span style="line-height:25px"><wbr style="line-height:25px">慢</wbr></span><wbr style="line-height:25px">。<br style="line-height:25px"><wbr style="line-height:25px"><span style="line-height:25px">其次,</span><span style="color:#000080; line-height:25px">画mutable的bitmap图比较慢。(11毫秒)</span><br style="line-height:25px"><wbr style="line-height:25px">注意:它比向View里直接画mutable的bitmap图(19毫秒)<span style="line-height:25px"><wbr style="line-height:25px">快</wbr></span><wbr style="line-height:25px">,<br style="line-height:25px"> 它和用ARGB_8888格式的一样<span style="line-height:25px"><wbr style="line-height:25px">快</wbr></span><wbr style="line-height:25px">。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">再其次,</wbr></span><span style="color:#000080; line-height:25px">画非Alpha的RGB数据更慢。(34毫秒)</span><wbr style="line-height:25px"><br style="line-height:25px"> 注意:它和向View里直接画非Alpha的RGB数据速度一样<br style="line-height:25px"> 但是它比ARGB_8888(47毫秒)快<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">最后,</wbr></span><span style="color:#000080; line-height:25px">画Alpha的RGB数据最慢。(43毫秒)</span><br style="line-height:25px"><wbr style="line-height:25px">注意:它和向View里直接画Alpha的RGB数据速度一样,但是它比ARGB_8888(39毫秒)慢<br style="line-height:25px"><wbr style="line-height:25px"><span style="color:#993300; line-height:25px">Android</span><span style="color:#003366; line-height:25px">的</span><span style="color:#0000ff; line-height:25px">fillRect</span><span style="color:#003366; line-height:25px">速度很快(1毫秒)</span><wbr style="line-height:25px"><br style="line-height:25px"><wbr style="line-height:25px"><span style="color:#993300; line-height:25px">Android</span><span style="color:#003366; line-height:25px">的</span><span style="color:#ff6600; line-height:25px">Canvas</span><span style="color:#003366; line-height:25px">本身没有提供</span><span style="color:#0000ff; line-height:25px">fillRect</span><span style="color:#003366; line-height:25px">函数但是它提供了个功能相近的函数<wbr style="line-height:25px">。</wbr></span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicvoid</span><wbr style="line-height:25px"><span style="color:#ff6600; line-height:25px">drawColor</span><wbr style="line-height:25px">(intcolor)<br style="line-height:25px"> Filltheentirecanvas'bitmap(restrictedtothecurrentclip)withthespecifiedcolor,usingsrcoverporterduffmode.<br style="line-height:25px"><wbr style="line-height:25px"><span style="color:#000080; line-height:25px">可以把它封装到一个fillRect函数,以便调用<wbr style="line-height:25px">。</wbr></span><br style="line-height:25px"> publicvoidfillRect(intx,inty,intw,inth)<br style="line-height:25px"> {<br style="line-height:25px"><span style="color:#0000ff; line-height:25px">g.clipRect(x,y,w,h);<br style="line-height:25px"> g.drawColor(p.getColor());<br style="line-height:25px"> g.clipRect(rect);</span><br style="line-height:25px"> }</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值