Android graphic Performance Analysis
1. Draw a picture in the direct view.
Draw a picture in the view with ondraw (canvas g.
On the simulator, I sequencing the performance of four 320xforwardeddiagrams into the View:
First, the bitmap graph for inmutable is the fastest. (9 ms)
Second, it is slow to draw a mutable bitmap. (19 ms)
Second, it is slower to draw RGB data without Alpha. (34 Ms)
Finally, the RGB data for Alpha painting is the slowest. (43 ms)
Test code 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> 0 xfffffff)
{
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;
}
2. Draw a picture in bitmap.
The following method is used to draw images to bitmap:
If (bufferbitmap = NULL)
Bufferbitmap = bitmap. createbitmap (game. Width, game. Height, bitmap. config. argb_8888 );
G = new canvas (bufferbitmap );
You can create a mutable bitmap and get its painting handle canvas,
You can draw images in bitmap.
I sequencing four directions on the simulator.Argb_8888The performance of the 320x480 graph in the bitmap format:
First, the bitmap graph for inmutable is the fastest. (5 ms)
Note: It is much faster than the bitmap graph (9 ms) that draws inmutable directly to the view, which is 4 seconds faster.
Second, it is slow to draw a mutable bitmap. (11 ms)
Note: It is much faster than directly drawing mutable Bitmap (19 ms) to the view, 8 seconds faster
Next, it is slower to draw Alpha RGB data. (39 MS)
Note: It is faster than painting Alpha RGB data directly to the view (43 ms)
However, it is still slower than directly drawing non-Alpha RGB data to the view (34 ms ).
Finally, the RGB data for non-Alpha painting is the slowest. (47 Ms)
Note: its performance is too bad. It is slower than the RGB data (43 ms) that draws Alpha directly to the view..
Why is it even slower to draw non-Alpha RGB data into bitmap?
Puzzle!
Test code 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> 0 xfffffff)
{
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;
}
If the format of argb_8888 in test code 2 is changed to rgb_565, the following conclusions are drawn:
First,The bitmap graph for inmutable is the fastest. (9 ms)
Note: It and the bitmap graph that directly draws inmutable to the view (9 ms)Same,
However, it is better than argb_8888 format (5 ms)Slow.
Second, it is slow to draw a mutable bitmap. (11 ms)
Note: It is more efficient than directly drawing a mutable bitmap to the view (19 ms)Fast,
It is the same as argb_8888 format.Fast.
Second,It is slower to draw non-Alpha RGB data. (34 Ms)
Note: It is the same speed as painting non-Alpha RGB data directly to the view.
But it is faster than argb_8888 (47 Ms)
Finally,The RGB data for Alpha painting is the slowest. (43 ms)
Note: It is the same speed as the RGB data that draws Alpha directly to the view, but it is slower than argb_8888 (39 MS ).
Android fillrect is fast (1 ms)
Android canvas does not provide the fillrect function, but it provides functions with similar functions..
Public voidDrawcolor(INT color)
Fill the entire canvas 'bitmap (restricted to the current clip) with the specified color, using srcover porterduff mode.
It can be encapsulated into a fillrect function to call.
Public void fillrect (int x, int y, int W, int H)
{
G. cliprect (X, Y, W, H );
G. drawcolor (P. getcolor ());
G. cliprect (rect );
}