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:
- create a bitmap,
- create my canvas (that is larger than the screen resolution)
- grab the cached bitmap and
- 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?