图片的Scaling:
下面这个example展示了怎样使用BitmapOptions,影响图片的加载的效果。Sub-sampling 能够加快图片加载的时间和 减少图片所占用的内存。它不能得到图片的大小,而只是在2的幂下的图片的大小 1/2 1/4 1/8。
源码:
*
* This example shows how the use of BitmapOptions affects the resulting size of a loaded
* bitmap. Sub-sampling can speed up load times and reduce the need for large bitmaps
* in memory if your target bitmap size is much smaller, although it's good to understand
* that you can't get specific Bitmap sizes, but rather power-of-two reductions in sizes.
*
* 这个example展示了怎样使用BitmapOptions,影响图片的加载的效果。Sub-sampling 能够加快图片加载的时间和
* 减少图片所占用的内存。它不能得到图片的大小,而只是在2的幂下的图片的大小 1/2 1/4 1/8
*
* Watch the associated video for this demo on the DevBytes channel of developer.android.com
* or on YouTube at https://www.youtube.com/watch?v=12cB7gnL6po.
*/
public class BitmapScaling extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bitmap_scaling);
LinearLayout container = (LinearLayout) findViewById(R.id.scaledImageContainer);
ImageView originalImageView = (ImageView) findViewById(R.id.originalImageHolder);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.jellybean_statue);
originalImageView.setImageBitmap(bitmap);
for (int i = 2; i < 10; ++i) {
addScaledImageView(bitmap, i, container);
}
}
private void addScaledImageView(Bitmap original, int sampleSize, LinearLayout container) {
// inSampleSize tells the loader how much to scale the final image, which it does at
// load time by simply reading less pixels for every pixel value in the final bitmap.
// Note that it only scales by powers of two, so a value of two results in a bitmap
// 1/2 the size of the original and a value of four results in a bitmap 1/4 the original
// size. Intermediate values are rounded down, so a value of three results in a bitmap 1/2
// the original size.
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inSampleSize = sampleSize;
Bitmap scaledBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.jellybean_statue, bitmapOptions);
ImageView scaledImageView = new ImageView(this);
scaledImageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
scaledImageView.setImageBitmap(scaledBitmap);
container.addView(scaledImageView);
}
}
截图:
图片的尺寸:
我们知道用 BitmapFactory.decodeFile之类的直接加载图片的方法,当图片过大的时候会发生OOM。怎么避免它呢?这就用到了我们上面提到的BitmapFactory.Options这个类。
BitmapFactory.Options这个类,有一个字段叫做 inJustDecodeBounds 。SDK中对这个成员的说明是这样的:
If set to true, the decoder will return null (no bitmap), but the out…
也就是说,如果我们把它设为true,那么BitmapFactory.decodeFile(String path, Options opt)并不会真的返回一个Bitmap给你,它仅仅会把它的宽,高取回来给你,这样就不会占用太多的内存,也就不会那么频繁的发生OOM了。
示例代码如下:
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inJustDecodeBounds=true;
Bitmap scaledBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.jellybean_statue, bitmapOptions);
int outHeight = bitmapOptions.outHeight;
int outWidth = bitmapOptions.outWidth;
这段代码之后,options.outWidth 和 options.outHeight就是我们想要的宽和高了
参考:
http://blog.csdn.net/hjm4702192/article/details/7821519
http://my.eoe.cn/isnull/archive/564.html
http://www.youtube.com/watch?v=12cB7gnL6po&list=PLWz5rJ2EKKc_XOgcRukSoKKjewFJZrKV0&index=51
android BitmapFactory.Options 翻译