0

I use the following code to initialize my bitmap variable:

Bitmap bitmap = ((BitmapDrawable)this.model.gameView.context.getResources().getDrawable(R.drawable.pic1)).getBitmap();

When I try to log the width of that bitmap, the log does not even output anything for that call.

I know it's making it to that line, because I traced the code.

Also, when I try to do canvas.draw for the bitmap, nothing is drawn on the screen.

Everything I draw with rectangles works fine.

My picture resource is a PNG.

Any help is appreciated!

jruizaranguren
  • 12,679
  • 7
  • 55
  • 73
a_schimpf
  • 745
  • 1
  • 5
  • 16

3 Answers3

2

Try something like this for your bitmap class.

public class DrawBitmap extends View
{
    Bitmap bitmap;

    public DrawBitmap(Context content)
    {
        super(content);

        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pic1);
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        canvas.drawColor(Color.BLACK);//whatever color you want, make sure it's not the same as your image
        canvas.drawBitmap(bitmap, (canvas.getWidth()), 0, null);
    }
}

Main Class

public class Main extends Activity 
{

    DrawBitmap myView;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        myView = new DrawBitmap(this);
        setContentView(myView);
    }
}
corecase
  • 1,278
  • 5
  • 18
  • 29
1

Try using BitmapFactory.decodeResource Have a look at the answer in this topic: How to convert a Drawable to a Bitmap?

Community
  • 1
  • 1
  • Yes, I tried this at first, and just did it again, and it is behaving the same way. – a_schimpf Aug 09 '12 at 20:00
  • Did you also try handling exceptions on that code? Maybe your png image is not in the right place in resource folder. I suggest check if it throws a Resource Not Found exception. – Serdar Samancıoğlu Aug 09 '12 at 20:05
0

Just figured it out. It had nothing to do with the method of bitmap loading I used. It was a logical error on my part. My code accidentally reached a case where my bitmap became null, and it tried to draw the null resource on the canvas.

a_schimpf
  • 745
  • 1
  • 5
  • 16