Canvas runtime error!

All your problems with Audio, Video and Images.

Canvas runtime error!

Postby pcm2a » Sat Aug 15, 2009 6:46 am

I am getting a crazy runtime error that I just cannot figure out. I have a image "testimage.png" that I can easily read into a Bitmap like this:
Code: Select all
Bitmap ob = BitmapFactory.decodeResource(getResources(), R.drawable.testimage);

I can display the Bitmap and it works great. However, if I try use a canvas with this I get a runtime error in the emulator and on the phone.
Code: Select all
Canvas canvas = new Canvas(ob);

How else does one work with a canvas if you cannot pass a Bitmap into it?
pcm2a
Developer
Developer
 
Posts: 27
Joined: Tue Aug 11, 2009 4:54 pm

Top

Postby mad.dogg » Sat Aug 15, 2009 2:59 pm

Read careful the documentation which loads the bitmap and check if the bitmap created is mutable.
If it's boring it ain't heavy enough!
mad.dogg
Developer
Developer
 
Posts: 42
Joined: Tue May 20, 2008 2:18 pm
Location: Bulgaria

Postby pcm2a » Sat Aug 15, 2009 7:02 pm

Wow you are 100% right, the method I was using returns a immutable bitmap! Here is the code that I have gotten to work, but it seems extremely long. I have tried to comment it up well for pasting it in here. Please let me know if there is any way to shorten this up.
Code: Select all
//Load in the .png image into a sprite
Drawable sprite = getResources().getDrawable(R.drawable.testimage);

//Create a mutable bitmap with transparency
Bitmap ob = Bitmap.createBitmap(sprite.getIntrinsicWidth(), sprite.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);

//Create a canvas for copying the png image over
Canvas canvas = new Canvas(ob);
   sprite.setBounds(0, 0, sprite.getIntrinsicWidth(), sprite.getIntrinsicHeight());
   sprite.draw(canvas);

/* At this point we have a mutable bitmap 'ob' that has
   our testimage.png loaded into it */

//Now lets change the color of all non-transparent pixels on the canvas
Paint paint = new Paint();
paint.setColorFilter(new PorterDuffColorFilter(0xffffff00,Mode.SRC_ATOP));  //yellow

//Apply those changes to our bitmap 'ob'
canvas.drawBitmap(ob, 0f, 0f, paint);

//Finally lets rotate our bitmap!
Matrix matrix = new Matrix();
matrix.postRotate(45); //rotate by 45 degrees

//Apply the rotation by creating a new bitmap
Bitmap obm = Bitmap.createBitmap(ob, 0, 0, ob.getWidth(), ob.getHeight(), matrix, true);

/* Bitmap obm now contains a copy of testimage.png which
   has had all non-transparent pixels turned yellow and
   has been rotated 45 degrees. */

/* I am not sure why we need this last part here
   but without it my rotation doesn't seem to work very well */
BitmapDrawable bmd = new BitmapDrawable(obm);

return bmd.getBitmap();  //This is the final bitmap that I use


If that is as optimized as possible then so be it. Maybe this will help someone looking for some bitmap code.
pcm2a
Developer
Developer
 
Posts: 27
Joined: Tue Aug 11, 2009 4:54 pm

Postby mad.dogg » Sat Aug 15, 2009 8:55 pm

Look at the Bitmap class there are quite a lot of stuff you can configure about it.

Currently if I understood your code correctly you do something like this.

1) get some drawable.
2) Create a empty bitmap with some configuration.
3) get a canvas, and a paint and draw the drawable onto the bitmap.

You could do something like this
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. Bitmap b = BitmapFactory.decodeResource(Resources res, int id);
  3.  
  4. Bitmap mutableBitmap = b.copy(config, true)// copy(Bitmap.Config config, boolean isMutable)
  5.  
  6.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Look careful at the docs, a few minutes with the docs will save you hours of clicking(cursing) around.
If it's boring it ain't heavy enough!
mad.dogg
Developer
Developer
 
Posts: 42
Joined: Tue May 20, 2008 2:18 pm
Location: Bulgaria

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 10 guests