您的位置:首页 > 移动开发 > Android开发

android画图性能分析

2011-11-10 15:13 204 查看
android画图的性能分析
一,往直接View里画图。

用onDraw  (Canvas g)向View里画图.

我在模拟器上测序了4种向View里画320X480图的性能:
首先,画inmutable的bitmap图最快。(9毫秒)
其次,画mutable的bitmap图比较慢。(19毫秒)
再其次,画非Alpha的RGB数据更慢。(34毫秒)
最后,画Alpha的RGB数据最慢。(43毫秒)

测试代码1:

 long times[]=new long[4];

 int cnt=0;

 protected void  onDraw  (Canvas g)

 {

  long time=System.currentTimeMillis();

  if(img!=null)

  {

   boolean hasAlpha=!true;

   time=System.currentTimeMillis();

   g.drawBitmap(img.getBitMap(), 0, 0, null);

   time=(System.currentTimeMillis()-time);

   times[0]+=time;

   int [] colors=new int [Game.width*Game.height];

   for(int i=0;i<colors.length;i++)

    colors=0xFF0000FF;

   Bitmap bitMap=Bitmap.createBitmap(colors, Game.width, Game.height, Bitmap.Config.ARGB_8888);

   time=System.currentTimeMillis();

   g.drawBitmap(bitMap, 0, 0, null);

   time=(System.currentTimeMillis()-time);

   times[1]+=time;

   time=System.currentTimeMillis();

    hasAlpha=false;

   g.drawBitmap(colors, 0, Game.width, 0, 0, Game.width, Game.height, hasAlpha, null);

   time=(System.currentTimeMillis()-time);

   times[2]+=time;

   

   time=System.currentTimeMillis();

   for(int i=0;i<colors.length;i++)
    colors=0xF0000FFF;

   hasAlpha=true;

   time=System.currentTimeMillis();

   g.drawBitmap(colors, 0, Game.width, 0, 0, Game.width, Game.height, hasAlpha, null);

   time=(System.currentTimeMillis()-time);

   times[3]+=time;

   cnt++;

  }

  if(cnt%10==0)

  {

   System.out.println("run cnt:"+cnt);

   System.out.println("time for mutable bitmap:"+times[0]

                       +" inmutable bitmap:"+times[1]

                       +" no alpha colors:"+times[2]

                       +" Alpha clors:"+times[3]);

   System.out.println("average time for mutable:"+times[0]/cnt

                                                +" inmutable:"+times[1]/cnt

                                                +" no alpha colors:"+times[2]/cnt

                                                +" Alpha clors:"+times[3]/cnt);

  }

  else if(cnt>0xFFFFFFF)

  {

   cnt=0;

   times[0]=0;

   times[1]=0;

   times[2]=0;

   times[3]=0;

  }

  Paint p=new Paint();

  p.setColor(0xFF0000FF);

  g.drawLine(0, 420, Game.width, 420, p);

  g.drawLine(0, 20, Game.width-20, Game.height, p);

  g.drawText("hello", 50, 50, p);

  blServiceDraw=false;

 }
二,往Bitmap里画图。
通过如下的方式来实现向Bitmap画图
  if(bufferBitMap==null)

   bufferBitMap=Bitmap.createBitmap(Game.width, Game.height, Bitmap.Config.ARGB_8888);

  g=new Canvas(bufferBitMap);
 可以创建一个mutable的bitmap,然后得到它的画柄Canvas,
 就可以往Bitmap里画图

我在模拟器上测序了4种向ARGB_8888格式的Bitmap里画320X480图的性能:
首先,画inmutable的bitmap图最快。(5毫秒)
 注意:它比向View直接画inmutable的bitmap图(9毫秒)快很多,快了4秒
其次,画mutable的bitmap图比较慢。(11毫秒)
 注意:它比向View里直接画mutable的bitmap图(19毫秒)快很多,快了8秒
再其次,画Alpha的RGB数据更慢。(39毫秒)
 注意:它居然比向View里直接画Alpha的RGB数据(43毫秒)快
 但是它仍然比向View里直接画非Alpha的RGB数据(34毫秒)慢
最后,画非Alpha的RGB数据最慢。(47毫秒)

 注意:它的表现太差了。它比向View里直接画Alpha的RGB数据(43毫秒)都要慢。

为什么在往Bitmap里画非Alpha的RGB的数据还要慢呢?

Puzzle!

测试代码2:
 long times[]=new long[4];
 int cnt=0;
 Bitmap bufferBitMap;
 protected void  onDraw  (Canvas g)

 {

  long time=System.currentTimeMillis();

  Canvas gg=g;

  if(bufferBitMap==null)

   bufferBitMap=Bitmap.createBitmap(Game.width, Game.height, Bitmap.Config.ARGB_8888);
  g=new Canvas(bufferBitMap);
  if(img!=null)

  {

   boolean hasAlpha=!true;

   time=System.currentTimeMillis();

   g.drawBitmap(img.getBitMap(), 0, 0, null);

   time=(System.currentTimeMillis()-time);

   times[0]+=time;

   int [] colors=new int [Game.width*Game.height];

   for(int i=0;i<colors.length;i++)

    colors=0xFF0000FF;

   Bitmap bitMap=Bitmap.createBitmap(colors, Game.width, Game.height, Bitmap.Config.ARGB_8888);

   time=System.currentTimeMillis();

   g.drawBitmap(bitMap, 0, 0, null);

   time=(System.currentTimeMillis()-time);

   times[1]+=time;

   time=System.currentTimeMillis();

    hasAlpha=false;

   g.drawBitmap(colors, 0, Game.width, 0, 0, Game.width, Game.height, hasAlpha, null);

   time=(System.currentTimeMillis()-time);

   times[2]+=time;

   

   time=System.currentTimeMillis();

   for(int i=0;i<colors.length;i++)

    colors=0xF0000FFF;

   hasAlpha=true;

   time=System.currentTimeMillis();

   g.drawBitmap(colors, 0, Game.width, 0, 0, Game.width, Game.height, hasAlpha, null);

   time=(System.currentTimeMillis()-time);

   times[3]+=time;

   cnt++;

  }

  if(cnt%10==0)

  {

   System.out.println("run cnt:"+cnt);

   System.out.println("time for mutable bitmap:"+times[0]

                       +" inmutable bitmap:"+times[1]

                       +" no alpha colors:"+times[2]

                       +" Alpha clors:"+times[3]);

   System.out.println("average time for mutable:"+times[0]/cnt

                                                +" inmutable:"+times[1]/cnt

                                                +" no alpha colors:"+times[2]/cnt

                                                +" Alpha clors:"+times[3]/cnt);

  }

  else if(cnt>0xFFFFFFF)

  {

   cnt=0;

   times[0]=0;

   times[1]=0;

   times[2]=0;

   times[3]=0;

  }

  g=gg;

  Paint p=new Paint();

  p.setColor(0xFF0000FF);

  g.drawLine(0, 420, Game.width, 420, p);

  g.drawLine(0, 20, Game.width-20, Game.height, p);

  g.drawText("hello", 50, 50, p);

  blServiceDraw=false;

 }

如是把测试代码2中的ARGB_8888格式改为RGB_565格式,得出以下结论:
首先,画inmutable的bitmap图最快。(9毫秒)
 注意:它和向View直接画inmutable的bitmap图(9毫秒)一样,

 但是它比用ARGB_8888格式的(5毫秒)慢。
其次,画mutable的bitmap图比较慢。(11毫秒)
 注意:它比向View里直接画mutable的bitmap图(19毫秒)快,

 它和用ARGB_8888格式的一样快。
再其次,画非Alpha的RGB数据更慢。(34毫秒)

 注意:它和向View里直接画非Alpha的RGB数据速度一样

   但是它比ARGB_8888(47毫秒)快
最后,画Alpha的RGB数据最慢。(43毫秒)
 注意:它和向View里直接画Alpha的RGB数据速度一样, 但是它比ARGB_8888(39毫秒)慢
Android的fillRect速度很快(1毫秒)
Android的Canvas本身没有提供fillRect函数但是它提供了个功能相近的函数。
public void drawColor (int color) 

Fill the entire canvas' bitmap (restricted to the current clip) with the specified color, using srcover porterduff mode.
可以把它封装到一个fillRect函数,以便调用。

 public void fillRect(int x,int y,int w,int h)

 {
  g.clipRect(x, y, w, h);

  g.drawColor(p.getColor());

  g.clipRect(rect);

 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android colors null 测试