I am trying to composite a background bitmap from multiple smaller bitmaps. I am using Eclipse with the Android SDK.
I have loaded a (mostly) blank background bitmap (pixel dimensions are 320x480) like so:
Bitmap mBackground = BitmapFactory.decodeResource( res, R.drawable.background );
Then I load a bitmap 'tile' (128x128) the same way.
I have tried multiple ways of drawing the tile onto the bitmap but every single method does not work the way I would like/expect. I want to draw the tile bitmap scaled down to 64x64 and at a specific location (pixel offset) on the background bitmap.
1) Using a Canvas...
Canvas c = new Canvas( mBackground );
c.drawBitmap( mTile, null, new Rect( 10, 10, 73, 73 ), null );
This draws the tile way too big (not fullsize, not 64x64, but somewhere in between, about 66% of original)
2) Using drawables (which basically seems to be the same):
Canvas c = new Canvas( mBackground );
c.translate( 10, 10 );
d.setBounds( 0, 0, 63, 63 );
d.draw( c );
Same result as experiment #1...
The documentation mentions in numerous places (without a concise explanation anywhere) that this has to do with the device independent 'densities' thus I tried the next approach...
3) Using fixed scale resources. I created the /res/drawable-nodpi (I also tried just /res/drawable) folder and moved my resources (background and tile png images) into it. That causes an exception when creating the Canvas from the background Bitmap because an unscaled bitmap apparently becomes immutable and therefore cannot be assigned to a Canvas!
4) I tried using BitmapFactory.Options and setting inScaled to false, and passing those options to both of the decodeResource calls for the background and the tile. This has the exact same effect as #3 (exception thrown).
Nothing has worked and its becoming quite frustrating. I am sure I am just missing some detail that is peculiar to bitmaps when it comes to the different coordinate spaces but I cannot find it anywhere in the documentation or elsewhere (I've tried multiple android development sites to no avail).
Hopefully someone who has done this will see this plea!
Thanks, jason
Canvas.drawBitmap() does not perform scaling afaik.
Since your tile image is originally 128x128 and you want to draw it at 64x64, the simplest trick would be to call decodeResource() with the option to reduce it in size:
BitmapFactory.Options options = new BitmapFactory.Options()
options.inSampleSize = 2; // downscale by 50%
Bitmap mTile = BitmapFactory.decodeResource( res, R.drawable.tile, options );
To do more powerful scaling (e.g. drawing your image at any size) you should wrap it in a BitmapDrawable.
It looks like the densities of your bitmaps are different... There is a similar issue here. You should check the density of each Bitmap you load to see if it's the same problem.
Related
Is there a way to directly scale a bitmap (bigBmp) into another bitmap (smallBmp) whithout creating a new one?
This creates a new one:
Bitmap smallBmp = Bitmap.createScaledBitmap(bigBmp, 20, 20, false);
I need to scale it always to the same size so I'd rather create smallBmp in advance (with size 20x20) and reuse this one every time.
I know, creating a 20x20 bitmap is probably not worth the optimization but it's inside a heavy loop where I just don't want any allocations.
I have some SVGs in my assets folder and I need to dynamically set them in my widget (on an ImageView).
I am using this library: http://code.google.com/p/svg-android/
This library returns a Picture or a PictureDrawable.
The only methods I can see to use on RemoteViews are setImageViewBitmap which obviously takes a bitmap.
I tried looking for code to convert a Drawable to a Bitmap like this:
PictureDrawable pictureDrawable = svg.createPictureDrawable();
Bitmap bitmap = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(), pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawPicture(pictureDrawable.getPicture());
currentBitmap = bitmap;
But the bitmap is too small. When I create the bitmap in Illustrator I set the artboard size to 65 which is what comes through on the intrinsic width/height.
My widgets can be resized so the ImageView sizes are variable. Even if I set the width and height statically to some large number like this...
Bitmap bitmap = Bitmap.createBitmap(300, 300, Config.ARGB_8888);
then the resulting bitmap just has a bunch of whitespace below and to the right of a tiny image.
I guess I need to somehow draw the picture at a scaled up value as well as creating the Bitmap at size 300. Ideally I could figure out the size of the ImageView at runtime and set the proper sized Bitmap if I knew that. Is this the best approach and how would I do this? Perhaps there is a better approach I don't even know about?
I've not used android-svg but if it's using vanilla PictureDrawables, then it should be just a matter of not using the intrinsic bounds.
Try the following:
PictureDrawable pictureDrawable = svg.createPictureDrawable();
Bitmap bitmap = Bitmap.createBitmap(300, 300, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
pictureDrawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
pictureDrawable.draw(canvas); // do not access the Picture directly, that defeats the purpose
currentBitmap = bitmap;
In short, use the Drawable, not its Picture and set the Drawable's bounds to be the full canvas.
Have you tried createScaledBitmap?
createScaledBitmap()
I have tried svg-android and it has not worked for me for this very reason. Not to mention its severely limited feature set.
The point of using vector graphics is that I can generate images of any appropriate size to fit the UI View size. Which means the generation method must accept the size requirements at run-time, and not always use width,height declared in <svg> tag.
Hence I used the native implementation: libsvg-android, which exactly does that.
It directly renders to a canvas with given size:
long objId = SvgRaster.svgAndroidCreate();
SvgRaster.svgAndroidParseBuffer(objId, readString(mInputStream, "UTF-8"));
SvgRaster.svgAndroidSetAntialiasing(objId, true);
SvgRaster.svgAndroidRenderToArea(objId, mCanvas, 0, 0, mWidth, mHeight);
I ended up modifying the underlying Artboard size to be 300. None of the scaling up methods worked. So the final code I used was that which I originally posted:
PictureDrawable pictureDrawable = svg.createPictureDrawable();
Bitmap bitmap = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(), pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawPicture(pictureDrawable.getPicture());
currentBitmap = bitmap;
Once I had a 300x300 Bitmap I was able to set it into the RemoteViews using setImageViewBitmap .
This kind of defeated the purpose of using SVGs in the first place (at least as far as using them in widgets was concerned).
My problem was not the same as User117. Perhaps he did not name the outer layer 'bounds' as was required in the library. Either way, I managed to get the library working, albeit by having to modify the SVG Artboard size.
Hopefully with the increase in screen resolution Android will introduce SVGs as part of the platform soon.
I'm sorry if this topic has been brought before, but all my searches on the web and google groups did not help me.
I'm currently developing a little game with the Android SDK, and use hi-res bitmaps that I resize accordingly to match the device's resolution (letting the system do it for me is
not "crisp" enough).
I use a SurfaceView, on which I paint in one pass a canvas filling the whole surface. The paint uses setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)) to allow masking.
Beforehand, I retrieve various bitmaps -- which are resized at initialization with createScaledBitmap() and put in a cache -- and I apply the bitmaps with a paint on this canvas, before drawing this canvas on the SurfaceView.
My problem is, whatever I try, whatever paint settings I use (dithering, antialias, etc..), the resized bitmaps are not antialiased and the drawing present jagged edges. I tried everything.
The only little success I had was using inSampleSize to approach the desired scaled size and force a first pass of antialiasing, before invoking createScaledBitmap on the retrieved
hi-res bitmap, but it is not beautiful enough. I just can't allow to create multitudes of pre-sized bitmaps for every combination of resolution. What did I miss ?
Thanks a lot in advance
First when you load your bitmap you make sure that you don't lose any image quality by settings options to argb_8888:
Options options = new Options();
options.inScaled = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap pic = BitmapFactory.decodeResource(getResources(), R.id.pic, options);
When you scale the bitmap turn on the filter:
pic = Bitmap.createScaledBitmap(pic, screenW, screenH, true);
However if one streaches the image too much inevitably it degrades in quality.
When you use paint you can improve quality but lose on speed with turning on ditherig and filtering:
Paint paint = new Paint();
paint.setFlags(Paint.DITHER_FLAG);
paint.setFilterBitmap(true);
Finally the entire activity window could be set on argb_4444 instead on argb_8888 (OS < 2.3). You can chage this if you instert this line before setContentView:
getWindow().setFormat(PixelFormat.RGBA_8888);
If it comes down to it, you can manually antialias without too much trouble. Just apply a simple lowpass filter (something like an NxN average) to the pixel data before asking the bitmap object to rescale itself.
you may clear canvas buffer by youself! such as follows:
canvas.drawColor(Color.TRANSPARENT, android.graphics.PorterDuff.Mode.CLEAR);
I'm having memory problems and think it might have to do with creating large bitmaps.
The task at hand is to get a fairly small tile image and create a larger tiled image and set this as the phone wallpaper. The way I'm doing this is:
1) Create a view that is 2 * screen width, 1 * screen height
2) Set the view background to a BitmapDrawable with the tile mode set to repeat
3) Create a bitmap with the views dimensions
4) draw the view to the bitmap by: view.draw(new Canvas(bitmap))
5) set wallpper: getApplicationContext().setWallpaper(bitmap)
This works fine on my phone (HTC Magic) and other phones that I have tried. But I am getting bug reports relating to this issue. I tried to recreate the problem by doubling the required dimensions and the problem seems to be happening in the 4th step when the view is being drawn to the bitmap:
ERROR/dalvikvm-heap(124): Heap Massage needed (7372800-byte external allocation too big)
I'm not sure how to go about solving this. Please help!
Thanks
I'm sure you thought of it, but nevertheless: Have you included
<uses-permission android:name="android.permission.SET_WALLPAPER" />
in your manifest-file?
You're sure there is no exception thrown? It could possibly be a problem with showing the Toast.
Not exactly sure if this is your solution, but have you looked at ? BitmapFactory.Options.inTempStorage
The way you use it is:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inTempStorage = new byte[16*1024];
Bitmap bitmap_origin = BitmapFactory.decodeFile(path, options);
Unfortunately, I don't think you can do much... We are on a mobile phone here ; Android limis the process memory to 16MB.
Here are a couple of tips and tricks I can give you (because I have the sames issues in my application)
Are you sure you need 32 bits pixels? that's three 8bit color channels plus a 8bit alpha channel. You can use RGB_565 for a visually acceptable result.
Recycle the image that you don't need when you create your bitmap (and that you won't need to draw your bitmap)
null any other object that you don't need
Run System.gc() to force a garbage collection just before you create the Bitmap
Hope this helps!
Actually you could refactor your code. You'll have better performance and probably use less memory if you don't use a View
Create a Bitmap of the desired size bitmap = Bitmap.createBitmap(width,height,Bitamp.Config.RGV_565) (or ARGB_8888, that might work too)
Create a canvas = new Canvas(bitmap)
Create the tiled image yourself, from your src
simplified Code:
// set another matrix if you want rotation/scaling of the input
Matrix identity=new Matrix();
for (int i=0; i<maxLines; i++) {
for (int j=0; j<maxCol; j++) {
canvas.draw(src, identity,anyPaint);
}
}
Keep the end set wallpaper getApplicationContext().setWallpaper(bitmap)
Say I have a somewhat large (i.e. not fit in most phones' memory) bitmap on disk. I want to draw only parts of it on the screen in a way that isn't scaled (i.e. inSampleSize == 1)
Is there a way to load/draw just the part I want given a Rect specifying the area without loading the entire bitmap content?
I'm quite confident this is possible since you can load a really large bitmap file into an ImageView without problems so there must be some sort of a built-in way to handle large bitmaps... and after a few attempts, I've found a solution:
Instead of loading the entire bitmap and manually draw it yourself, load it as a Drawable instead:
InputStream mapInput = getResources().openRawResource(
R.drawable.transit_map);
_map = Drawable.createFromStream(mapInput, "transit_map");
_map.setBounds(0, 0, _mapDimension.width(), _mapDimension.height());
I'm using a resource file but since you can use Drawable.createFromStream to load image from any InputStream, it should works with arbitrary bitmap.
Then, use the Drawable.draw method to draw it onto the desired canvas like so:
int left = -(int) contentOffset.x;
int top = -(int) contentOffset.y;
int right = (int) (zoom * _mapDimension.width() - contentOffset.x);
int bottom = (int) (zoom * _mapDimension.height() - contentOffset.y);
_map.setBounds(left, top, right, bottom);
_map.draw(canvas);
As in the above case, You can also scale and translate the bitmap as well by manipulating the drawable's bounds and only the relevant parts of the bitmap will be loaded and drawn onto the Canvas.
The result is a pinch-zoomable view from just one single 200KB bitmap file. I've also tested this with a 22MB PNG file and it still works without any OutOfMemoryError including when screen orientation changes.
Now it's very relevant: BitmapRegionDecoder.
Note: available since Android SDK 10
It can easily be done by using RapidDecoder.
import rapid.decoder.BitmapDecoder;
Rect bounds = new Rect(10, 20, 30, 40);
Bitmap bitmap = BitmapDecoder.from("your-file.png")
.region(bounds)
.decode();
imageView.setImageBitmap(bitmap);
It supports down to Android 2.2 (API Level 8).
Generally speaking, that isn't possible, particularly since most image formats are compressed, so you don't even know which bytes to read until you've extracted the uncompressed form.
Break your image up into small tiles and load just the tiles you need to cover the region you want to display at runtime. To avoid jittery scrolling, you might also want to preload tiles that are just out of sight (the ones that border the visible tiles) on a background thread.