0

Currently I have a class that draws a bunch of squares dynamically based on the contents of a sqlite file. As the data is quite large the drawing is many times larger than the screen. I've read a lot of proposed methods to do this and am unsure of the best approach. As I will not change the image after I render it (only need to dynamically render it upon restart) the first answer listed here seems to be the best but I'm not fully understanding how to do so.

If I understand correctly I:

  1. create a bitmap,
  2. create my canvas (that is larger than the screen resolution)
  3. grab the cached bitmap and
  4. render the bitmap versus the canvas.

But I'm lacking the knowledge to make this happen within my existing code. My existing code simply creates a very large canvas (I do not define the bounds, I just start drawing). My main class looks like this:

package com.example.drawdemo;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;

public class DrawDemo extends Activity {
    DrawView drawView;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    drawView = new DrawView(this);
    drawView.setBackgroundColor(Color.WHITE);
    setContentView(drawView);
    }
}

And the DrawView class something like this:

public class DrawView extends View {

    public DrawView(Context context) {
        super(context);            
    } 

    @Override
    public void onDraw(Canvas canvas) {

        //LOTS OF CODE HERE to figure out how to render rectangles through loops
        //  similar to lines like this
        canvas.drawRect(xStart, rectYPointer, xEnd, rectYPointer+rectSize, paint);
        // or this
        canvas.drawText(String.valueOf(rectSize), 130, rectYPointer*2, paint);

    }

}

Is the approach I am taking the correct approach to allow me to scroll down and see this large image that is bigger than the android screen? If so, how do I migrate my code to conform to the approach listed in the answer I believe is the correct method?

Community
  • 1
  • 1
WildBill
  • 9,143
  • 15
  • 63
  • 87

1 Answers1

0

Well I'm affraid you didn't get the point of the linked topic. It's quite different as what you try to achieve.

this topic is about drawing on a bitmap to be used later.

you already have a constructed bitmap do you ? ( or at least you can load it from resources using :

BitmapFactory.decode... (resource or file according to your input, see the documentation here)

Your canvas doesn't have to be huge in order to scroll it inside the screen. The size of the canvas is the size of your view (here, the size of the screen) but you will be drawing the bitmap according to the scroll position you want to apply.

see : canvas.drawBitmap() 's documentation and canvas.translate can help too.

Basically what you want is : get a reference on a loading bitmap (using bitmapdecode) in your view. then in the onDraw, draw the bitmap on the view's canvas to show it on screen. then, to add the scroll effect, try to change the y translation of the canvas when drawing on it.

hope that helps.

For more information (and a good example) about drawing on a canvas I suggest you open the API demo project source code :

API Demos -> Graphics -> TouchPaint
Guian
  • 4,563
  • 4
  • 34
  • 54