What's the difference between drawables and bitmaps? + What's the meaning of a BitmapDrawable?
I just don't understand why all pictures could be used as Bitmap, and only as Bitmap (or Drawable).
Also, what are the pros and cons?
Quoting the documentation for Drawable:
A Drawable is a general abstraction for "something that can be drawn." Most often you will deal with Drawable as the type of resource retrieved for drawing things to the screen; the Drawable class provides a generic API for dealing with an underlying visual resource that may take a variety of forms. Unlike a View, a Drawable does not have any facility to receive events or otherwise interact with the user.
In Android's class library, a Bitmap is the data about a raster image, such as one loaded from a PNG or JPEG file.
What's the meaning of a BitmapDrawable?
A Bitmap does not know how to draw itself; the Android class library developers elected to put that logic in a BitmapDrawable.
Related
I would like to know what the pros and cons of using drawable vector shapes (XML files) vs image resources (png files) for icons (Material Design Icons)?
As I see, the question refers to general alternative between raster (e.g. jpeg, png) and vector (e.g. eps image). In one sentence, vector can be easily scaled and required less memory, but is more complicated. In details, see e.g. here.
The difference stands in vector images describing what they want to draw and raster images describing the color of each pixel, due to which these are heavier in size.
Vector images can be resized flawlessly, while attempting it on raster images is always a bad idea, which makes those images lose their quality.
Moreover, vector images are rendered slower than raster ones when there are many elements, due to the computations necessary to parse paths and shapes and draw them (indeed, vector images are more CPU intensive to render). Rather, it is faster if there are just few elements and paths have few points. Though vector images are lighter to load (in RAM; in fact they cannot be loaded in the GPU memory, unlike raster images), which makes them the fastest.
The important is, don't exceed with the number of elements in SVGs and other vector images. Otherwise, they'd end up being slower.
It depends on your needs to use which one. you can change vectors attributes with xml code anywhere you want. but image resources has less Flexibility to control its attrs and use more memory
I would like to draw technical illustrations in my Android app.
I have images with shapes, and I have to change the size of those shapes based on input data. Thought that I could save those images as SVG, then I could manipulate the size of the shapes in it. For example, I would like to take a with a specified ID, and change its width/height.
There're great SVG display libraries for Android, but I haven't found one which can also manipulate the graphics.
Do you know any library which can do this?
If not, how else should I draw my illustrations?
Is it possible in Android to use the Vector images (for example, contours made in Adobe Illustrator) to be drawn on Canvas?
I looked at Vector graphics in Android but I don't want to use any additional third-party libraries.
So, is there any another way to make this idea?
One thought, that came to mind, was to convert to the 9patch images. But still I'm not sure whether it sounds good.
All of fuss just over the aim to make the complex countour from which I want to create android.graphics.Path using public void addPath (Path src, Matrix matrix) function.
You cannot use vector images in Android, at least with the built in SDK.
It may exist libraries to use vector images, but this is not the Android best practice.
What you need to do in order of not pixelating/blurring your images is to use the different drawable folders existing in android.
You should provide different images depending on the screen density of the display, and android will pick the correct one in runtime.
http://developer.android.com/guide/practices/screens_support.html
9 patches are good only if the image is meant to stretch to fit its content, while its borders should repeat following a pattern. The typical use of this are buttons. For images that dont work as the background of a View, 9 patches are not a good option.
Even after eliminating all the Bitmaps in my app, they are still appearing in the memory analyzer. (see screen capture below) Please can you help me understand why they are still listed and help me to finally remove them? I'm certain there are no bitmaps since I also removed the Bitmap import.
In place of creating bitmaps, I now create the images from Uri's as shown in the code below. Does this approach generate bitmaps?
try {
InputStream inputStream = getContentResolver().openInputStream(imageURI);
imageDrawable = Drawable.createFromStream(inputStream, imageURI.toString() );
} catch (FileNotFoundException e) {
imageDrawable = getResources().getDrawable(R.drawable.default_image);
}
FYI, the reason I eliminated the bitmaps is because they appeared to be causing out of memory crashes. Even after carefully following the SO guidance on how to reduce bitmap size (inSampleSize, bitmap.recycle(), etc.) the app would crash with OOM every other time it was launched. I got annoyed and decided to eliminate bitmaps. But, like kids that won't move out, they still appear to be dogging me.
Though usually not visible to the application, Drawables may take a
variety of forms:
Bitmap: the simplest Drawable, a PNG or JPEG image.
Nine Patch: an extension to the PNG format allows it to specify
information about how to stretch it and place things inside of it.
Shape: contains simple drawing commands instead of a raw bitmap,
allowing it to resize better in some cases.
Layers: a compound drawable, which draws multiple underlying drawables on top of each
other. States: a compound drawable that selects one of a set of
drawables based on its state.
Levels: a compound drawable that selects
one of a set of drawables based on its level.
Scale: a compound drawable with a single child drawable, whose overall size is modified
based on the current level.
That's from the Drawable description. So I believe that inside Drawable is just creating bitmaps for you
I would like to read images from InputStreams, and draw them to my canvas. Unfortunately, the images may be very large, and could easily cause out of memory exceptions. BitmapFactory allows me to provide a sample size value, which will down sample the image as it is processed and avoid the memory issues. However, image quality suffers.
Ideally, canvas would provide a paint image method which can paint from an InputStream, as opposed to from a Bitmap, but I haven't found anything of this type. Does this exist, or is there any other way to safely render arbitrarily large images from InputStreams without down sampling?
I am not sure what image format you are using but if you want to send a lossless image (or with less loss than has already occurred), It can't be compressed better than JPEG. so, use JPEG first.
Here is an example for drawing image on Canvas. Override the 'draw' method. http://www.androidsnippets.com/drawing-an-image-as-a-map-overlay2
At the end, hacking it
Probably, pre-splitting the image; compressing each individual one and restoring at the end sounds logical.
Here are few attempts,
http://kalanir.blogspot.com/2010/02/how-to-split-image-into-chunks-java.html
And
http://www.anddev.org/multimedia-problems-f28/chunk-of-a-big-big-image-t6211.html
The answer appears to be: no, there isn't.