I'm in the works of a game and I'm loading a large image that is 5120 wide and 116 high. When I try to draw it to the screen using
canvas.drawBitmap(land, 0, 0, paint);
nothing gets drawn. However when I use a smaller file (512x116) with the same method call, it gets drawn. Is there a image size limit? My image is 750 KB. Also the max canvas width and height is 8192. Thanks in advance!
EDIT:
I noticed this in the log:
Bitmap too large to be uploaded into a texture (20480x464, max=8192x8192)
as if my bitmap is 4 times larger than its actual size. Why would that be?
EDIT 2: Found the issue. Bitmapfactory is returning a larger file because is multiplies the dimensions by my screen density. So the file ends up being too large.
I haven't used Canvas layout but try using largeHeap = "true" in your Manifest
Related
I am trying to work on a small requirement which provides users with an option to resize an image to a given percentage. Lets say 75% is the option. Does that mean I should resize the image size to 75% or the resolution of the image to 75%?
Any thoughts on this?
I wanted to use
Bitmap yourBitmap; resized =
Bitmap.createScaledBitmap(yourBitmap,(int)(yourBitmap.getWidth()*0.75),
(int)(yourBitmap.getHeight()*0.75), true);
For the newWidth and newHeight should I blindly pass calculate like above?
I think
image resolution is 100% like 100x100 or 200x200 or 1000x1000
user want to resize it to 75%
I think scaleX, scaleY
so on the end image must be 75% like 75x75 or 150x150 or 750x750
Within the help of Bitmap.createScaledBitmap you can resize your image even without losing your image quality
Bitmap scaledBitmap = Bitmap.createScaledBitmap(unscaledBitmap, wantedWidth, wantedHeight, true);
I hope it will work for you
You can implement as following way.
/* sample code */
Bitmap srcBitmap;
Bitmap destBitmap = Bitmap.createScaledBitmap(srcBitmap, (int)(srcBitmap.getWidth()*0.75), (int)(srcBitmap.getHeight()*0.75), true);
Hopefully...
What you consider to be 75% of an image is up to you. There are several posibilities. A nice variant is to resize to a predetermined size like 1024 for width if landscape and for height if portrait.
All the here suggested techniques can often not be used as they construct a full blown original bitmap first. Often memory for that will not be available.
So scale it down while loading. BitmapFactory will do it for you.
When I create a Canvas from a bitmap that is generated with the BitmapFactory.decodeResources from a drawable resource that is a png image file of 400 x 400 pixels, the Canvas height and width are not 400 but 600!
In the code below, the drawable resource wind_scale is a png file that I generated with PHP as a 400 x 400 image.
Bitmap workingBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.wind_scale, options);
Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas mCanvas = new Canvas(mutableBitmap);
int mHeight = mCanvas.getHeight(); int mWidth = mCanvas.getWidth(); both 600!!!!
Is this behavior expected? If so, why is there a 50% increase?
I am not 100% sure of the problem here but it's similar to another SO question (Getting weird font size after setting it programmatically). The problem there was also a 50% difference in values, and it was due to a comparison of sp units to pixels on the OP's device with a screen density ratio of 1.5.
You might check into BitmapFactory.Options; there are several scale and density flags that may help. My best guess is that the image is being scaled for the device and this is causing a measurement mismatch.
Canvas it's not related to the bitmap size but to the device available screen size. If you try that code on different devices (or virtual devices) you will find different results.
Canvas and Drawables
Hi so I need a user to be able to upload a profile picture that I have to re-size and put in their profile. The "area" for the profile picture is a 100x100 square since its on an android device. Lets assum someone posted a 300 x 600 or 600 x 300 how would I convert / crop an image to fit a 100 x 100 square and look good as a profile image? I feel like this is more of a conventional question.
I do know how to resize and crop bitmaps just can't figure out how would I go about it.
for instance
android.graphics.Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
Well if you are not against wasting some of the space, I recommend just doing basic scaling. Divide the larger size by 100 gives the scale factor. Then divide by that on each dimension and you have a picture with the right aspect ratio that fits your area.
I just can't figure out how to draw on a bitmap (with the help of a canvas) so that the result won't be device density dependent.
Here's the code that makes the drawing:
ImageView iv = (ImageView)findViewById(R.id.container);
Bitmap result = BitmapFactory.decodeResource(getResources(), R.drawable.big_picture).copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(result);
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.small_picture), actualX, actualY, null);
iv.setImageBitmap(result);
I load a big_picture in the Bitmap result and i want to draw a small_picture on that but at a specified position. If i set actualX and actualY it's ok on a device with the same density. However devices with different densities "scale" the canvas.
The interesting part is that only the small_picture is got "scaled" and goes out of the screen but the pig_picture behind it just fits the screen well on any density screen.
Why is that? How can i draw the small_picture on the big_picture densitiy independently?
Thank you!
I've found out what's the case after debugging the Bitmap. Unless you're putting images to drawable-nodpi the image will be re-sized to match the nominal device density. (Which are 120, 160, 240, 320) If you "load" an image to a Bitmap and Canvas they will have one of these densities regardless of the original image's.
So one solution is to put the images to drawable-nodpi. After that they will behave the same on all densities.
Another solution is to multiply the coordinates based on the ratios of the densities above.
...or you can just make custom images and coordinates for all qualifiers.
I have a PNG image of 168.2KB and 1991x1756 and tried to import it into a Bitmap using BitmapFactory.decodeStream(). The problem is that I run into OutOfMemoryError where the Bitmap size ends up being 13,657KB.
What I don't understand is why is the file so big after the import and how to mitigate this.
Seems that this OutOfMemoryError issue is very common with decoded Bitmap images but can't find a way to fix it. Any ideas?
Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, bitmapOptions);
E/AndroidRuntime(10744): java.lang.OutOfMemoryError: bitmap size exceeds VM budget(Heap Size=7815KB, Allocated=3279KB, Bitmap Size=13657KB)
A png-file is highly compressed, meaning that the file size on disk is much smaller than it would be when all the data it contains is loaded into memory.
A bitmap is a very simple representation of an image, where every pixel is represented by a value. The size of this value depends on the current settings (you may have heard of 32-bit images, 16 bit images or 8 bit images which are some common sizes).
To calculate the true size of an image represented as a bitmap you have to calculate the number of pixels, and multiply this with how many bits you have for each pixel.
Your image is 1991x1756 pixels (3 496 196 pixels in total). If you load the bitmap using ARGB_8888 format (one byte for each color/alpha channel = 4 bytes per pixel) you end up with 3 496 196 * 4 = 13 984 784 bytes.
You can reduce the total size by using fewer bytes/channel (RGB_565 for example) although this also reduces the quality of your image. A better option would most likely be to reduce the size of your images, or import them using the inSampleSize parameter in your BitmapFactory.Options to load a smaller image.
I believe the PNG image is heavily compressed, but bitmaps in android are not compressed as there are 4 bytes per pixel (ARGB)
so 1991 * 1756 * (4 bytes) is about 13657 KB
So try using a smaller image or insample size Options for bitmap options to reduce the size.
Take a look at this post:
Decoding bitmaps in Android with the right size