android画图性能分析
来源:互联网 发布:剑网3女神脸数据 编辑:程序博客网 时间:2024/05/16 03:06
一,往直接View里画图。
用onDraw (Canvas g)向View里画图.
我在模拟器上测序了4种向View里画320X480图的性能:
首先,画immutable的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]
+" immutable bitmap:"+times[1]
+" no alpha colors:"+times[2]
+" Alpha clors:"+times[3]);
System.out.println("average time for mutable:"+times[0]/cnt
+" immutable:"+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图的性能:
首先,画immutable的bitmap图最快。(5毫秒)
注意:它比向View直接画immutable的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]
+" immutable bitmap:"+times[1]
+" no alpha colors:"+times[2]
+" Alpha clors:"+times[3]);
System.out.println("average time for mutable:"+times[0]/cnt
+" immutable:"+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格式,得出以下结论:
首先,画immutable的bitmap图最快。(9毫秒)
注意:它和向View直接画immutable的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 voiddrawColor (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);
}
用onDraw (Canvas g)向View里画图.
我在模拟器上测序了4种向View里画320X480图的性能:
其次,画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
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
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]
+" immutable bitmap:"+times[1]
+" no alpha colors:"+times[2]
+" Alpha clors:"+times[3]);
System.out.println("average time for mutable:"+times[0]/cnt
+" immutable:"+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画图
if(bufferBitMap==null)
bufferBitMap=Bitmap.createBitmap(Game.width, Game.height, Bitmap.Config.ARGB_8888);
g=new Canvas(bufferBitMap);
可以创建一个mutable的bitmap,然后得到它的画柄Canvas,
就可以往Bitmap里画图
我在模拟器上测序了4种向
注意:它比向View直接画immutable的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
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
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]
+" immutable bitmap:"+times[1]
+" no alpha colors:"+times[2]
+" Alpha clors:"+times[3]);
System.out.println("average time for mutable:"+times[0]/cnt
+" immutable:"+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格式,得出以下结论:
但是它比用ARGB_8888格式的(5毫秒)
它和用ARGB_8888格式的一样
注意:它和向View里直接画非Alpha的RGB数据速度一样
但是它比ARGB_8888(47毫秒)快
但是它比ARGB_8888(39毫秒)慢
public void
Fill the entire canvas' bitmap (restricted to the current clip) with the specified color, using srcover porterduff mode.
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画图性能分析
- android画图性能分析
- android画图性能分析
- android画图性能分析
- android画图性能分析
- android画图流程分析
- ListView性能分析--Android
- android 代码性能分析
- Android 性能分析案例
- Android性能优化分析
- Android应用性能 分析
- android 性能分析小结
- Android 性能分析案例
- android 性能分析工具
- Android SQLite性能分析
- Android 性能分析案例
- Android 性能分析案例
- Android 性能分析案例
- 最常用的linux操作命令!!
- 人民币(纸币)检测
- 华为公司的笔试题:写一个Stack,注意处理异常。这里给出一个exception c++上讨论的一个简易版本代码。
- hdu 3415 Max Sum of Max-K-sub-sequence
- Linux系统下挂载windows分区方法
- android画图性能分析
- 利用标准遗传算法求解函数
- 贪吃蛇游戏 C语言程序设计
- gcc和g++的区别
- 《悟透JavaScript》学习札记十一之原型真谛一
- 关于CTO 和 职业经理人
- poj 2971 Give Me the Number
- window.parent与window.opener的区别
- LINUX 多线程编程