Tip:
Highlight text to annotate it
X
CHET HAASE: Hi.
I'm Chet Haase, an engineer on the Android Team at Google.
Today I wanted to talk a little bit about bitmaps.
In particular, I want to talk about bitmap scaling.
One of the big problems that people run into when running
applications on constrained devices is that they want to
load in, let's say, huge images from the camera on the
phone, from the web, wherever.
And when they're loading in a lot of these things, A,
they're going to take a long time to come in.
B, they're going to create garbage along the way and
performance problems.
And C, maybe they're going to run out of memory if they try
to load too many at the same time.
There are various ways of dealing with this situation.
The overarching idea is that you should load and create and
use the images of the size you need.
So for example, don't load in an image that's this big if
you're always going to display it as a thumbnail.
Instead, load in whatever you need to, get it at the size
you need it, and then have that be the
image that you retain.
So today's example is around loading in a
scaled version of it.
In particular, we're going to use a parameter in Bitmap
Factory Options, which is called inSample Size.
So first let's take a look at the demo, which is called
Bitmap Scaling.
And there's nothing to interact with here.
I just wanted to show a visual representation of what's going
on with these images.
Because there's something a little unobvious about
inSample Size the first time you use it.
So the original image you can see up at the top under the
words that say Original Image.
And then you can see these scaled versions down below
where it says Scaled Images.
So we see two of the same size and then four of the same size
and then two of the same size under there.
So what's going on?
Let's take a look at the code.
So we have this class called Bitmap Scaling.
We have this image view where we're going to find the
original image.
We're going to decode the Jelly Bean
statue image in there.
And then we're going to add scaled image views.
And this is going to be using the inSample Size thing.
So we're basically going to iterate values from 2 to 10
and use that as our inSample Size value and see
what effect we get.
So we create one of these Bitmap options objects and we
set the inSample Size to the sample size that was passed
in, which is that integer value from 2 to 10.
And then we create a scaled bitmap, which does a decode on
that original image.
But it uses the options.
It uses this inSample Size.
And what inSample Size tells it is how many pixels to read
for each of the original pixels in the image.
The advantage of doing it this way is that
it reads less data.
It's going to be faster to do this because it's basically
skipping a lot of data along the way potentially based on
the value of inSample Size.
The disadvantage of doing it is that it's basically going
to wind up with powers of 2 scales of your original image.
So you can pass in a value of 2, which means it's going to
skip every other pixel, and you're going to wind up with
an image that's half the size of the original.
If you ask for 3, it's happy to do that.
Except it doesn't really make sense.
It's actually going to round down to the next lowest power
of 2 and it's going to be equivalent to 2, which is why
we see, for the first 2 scaled images, they're
exactly the same size.
Because passing in 3 was equivalent to passing in 2.
When we go up to 4, that's the next power of 2.
OK, it's happy to do that.
When we go up to 5, it rounds down to 4.
So basically, inSample Size is a very coarse-grained way of
getting a scaled image.
It works great in the cases where you want powers of 2
scales of your original image.
Maybe it works for you to read in at that lower size and you
get a faster reading time, because you're
reading less data.
But then once you have that data, maybe you then want to
scale to the exact size that you want.
And you can always get a bitmap of the exact size that
you want through various mechanisms.
Creating it exactly at the width-height that you want or
create a scaled bitmap.
There's an API to create a scaled bitmap
from another one.
So there's lots of different ways to do that.
Today was about using inSample Size in particular.
Higher performance because it reads less data with the
constraint that it will end up with powers of 2
images at the end.
So inSample Size.
Use it.
Thanks.