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 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 BitmapFactory.Options, which is called inSampleSize.

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 these's something a little unobvious about inSampleSize the first time you use it. So the original image you can see up at the top under the words the 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 inSampleSize thing. So we're basically going to iterate values from 2 to 10 and use that as our inSampleSize value and see what effect we get. So we create one of these Bitmap options objects and we set the inSampleSize 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 dose a decode on that original image. But it use the options. It uses this inSampleSize. And what inSampleSize 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 inSampleSize. 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 round down to 4. So basically, inSampleSize 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 lot's of different ways todo that. Today was about using inSampleSize in particular. High performance because it reads less data with the constraint that it will end up with powers of 2 images at the end. So inSampleSize. Use it. Thanks.

嗨,我是Google的Android团队的一名工程师Chet Haase。今天我想谈谈一下关于位图的一点点。特别是,我想谈谈位图缩放。

人们在受限制的设备上运行应用程序时遇到的一个大问题是,他们希望从手机上的相机,网络上的任何地方加载大图像。当他们加载了很多这样的东西的时候,A,他们需要很长时间才能进来.B,他们会一路创造垃圾和性能问题。而C也许是,如果他们试图在同一时间加载太多内存,他们将会耗尽内存。

处理这种情况有很多种方法。首要的想法是,您应该加载和创建和使用您需要的大小的图像。因此,例如,如果您始终要将其显示为缩略图,则不要加载这个大的图像。相反,加载任何你需要的,以你需要的大小来获得它,然后把它作为你保留的图像。所以今天的例子是在它的缩放版本中加载。特别是,我们将在BitmapFactory.Options中使用一个名为inSampleSize的参数。

所以首先让我们看看这个名为Bitmap Scaling的演示。在这里没有任何交互我只是想展示一下这些图像发生了什么。因为在第一次使用inSampleSize时,这些有点不明显。所以原来的图像,你可以看到在顶部的话说原始图像。然后你可以在缩放图像的下面看到这些缩放版本。所以我们看到两个相同的尺寸,然后是四个相同的尺寸,然后在那里两个相同的尺寸。发生什么了?让我们来看看代码。

所以我们有这个叫做Bitmap Scaling的类。我们有这个图像视图,我们要找到原始图像。我们将在那里解码Jelly Bean的雕像。然后我们要添加缩放的图像视图。这将是使用inSampleSize的东西。所以我们基本上将迭代从2到10的值,并使用它作为我们的inSampleSize值,看看我们得到什么效果。所以我们创建这些Bitmap选项对象之一,并且将inSampleSize设置为传入的样本大小,即从2到10的整数值。然后我们创建一个缩放的位图,对该原始图像进行解码。但它使用的选项。它使用inSampleSize。而inSampleSize告诉它,图像中每个原始像素要读取多少个像素。

这样做的好处是读取的数据更少。这样做会更快,因为它基本上可能会根据inSampleSize的值跳过大量数据。这样做的缺点是,它基本上将与你的原始图像的2个比例的权力结束。所以你可以传递一个2的值,这意味着它将跳过其他每一个像素,并且你将会得到一个尺寸是原始尺寸一半的图像。如果你问3,很高兴这样做。除非它没有意义。实际上它会下降到2的下一个最低的功率,它将相当于2,这就是为什么我们看到,前2个缩放图像,他们是完全相同的大小。因为传入3相当于传入2.当我们上升到4时,这是下一个2的能力。好吧,这样做很高兴。当我们到5时,它下降到4.所以基本上,inSampleSize是一个非常粗糙的方式来获得缩放图像。它在你想要原始图像的两个比例的权力的情况下效果很好。也许它适合你阅读,而且阅读时间更短,因为你阅读的数据更少。

但是,一旦你有了这些数据,也许你想要缩放到你想要的大小。而且你总是可以通过各种机制获得你想要的确切大小的位图。正是在你想要的宽高处创建它,或者创建一个缩放的位图。

有一个API从另一个API创建一个缩放的位图。所以有很多不同的方式待办事。今天是特别使用inSampleSize。高性能,因为它读取较少的数据,最终将以2个图像的权力结束。所以inSampleSize。用它。谢谢。

翻译自:DevBytes: Bitmap Scaling

results matching ""

    No results matching ""