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);
	}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值