一
一、往直接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 = 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画图
if (bufferBitMap == null)
bufferBitMap = Bitmap.createBitmap(Game.width, Game.height,
Bitmap.Config.ARGB_8888);
g = new Canvas(bufferBitMap);
可以创建一个mutable的bitmap,然后得到它的画柄Canvas,
就可以往Bitmap里画图
我在模拟器上测序了4种向
注意:它比向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 格式,得出以下结论:
但是它比用ARGB_8888格式的(5毫秒)
它和用ARGB_8888格式的一样
注意:它和向View里直接画非Alpha的RGB数据速度一样
但是它比ARGB_8888(47毫秒)快
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);
}