Slow graphic performance with ImageView

Problems with Canvas, OpenGL, etc...

Slow graphic performance with ImageView

Postby eoJ » Thu Jul 28, 2011 12:20 pm

Hi all,

I'm writing an application which draws a graph. I am doing this by extending ImageView and drawing my graph in the onDraw() method. My graph is also inside a HorizontalScrollView. Everything works fine except the application takes a while to load up and runs *really* slowly when I scroll the image. I believe this is because it is being redrawn when scrolled so my solution was to draw my entire image to a bitmap and simply draw this bitmap in the onDraw() method. However, I cannot get this to work. I'm probably doing something really stupid so see my code below for mistakes (Also if anyone can think of a better way to optimise the drawing I'm all ears!).

Code: Select all
   public void drawBitmap() {
      drawGraph = Bitmap.createBitmap(graphLength, graphHeight, Bitmap.Config.ARGB_8888);
      drawBitmap = new Canvas(drawGraph);
                drawPath = new Path();

                           for (int i=0; i < newData.size(); i++){            
               drawPath.moveTo(xStart, yStart);
               drawPath.lineTo(xStart+10, newData.get(i));
                       //lots more drawing  but done similarly to this.........

   public void onDraw(Canvas canvas) {
      canvas.drawBitmap(drawGraph, 0, 0, null);      

Any help appreciated!
Posts: 3
Joined: Thu Jul 28, 2011 9:45 am


Re: Slow graphic performance with ImageView

Postby MichaelEGR » Thu Jul 28, 2011 7:43 pm

For starters create the bitmap once outside of the onDraw / drawBitmap methods and you'll have a lot better performance without doing much else different. Likewise create one Path instance outside the onDraw / drawBitmap methods and reuse it by calling reset() after you are done in the drawBitmap method. You always want to avoid object creation in rendering loops.
Founder & Principal Architect; EGR Software LLC
User avatar
Senior Developer
Senior Developer
Posts: 147
Joined: Thu Jan 21, 2010 5:30 am
Location: San Francisco, CA


Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 1 guest