下面这个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); } }
截图:
ps:我们可以看到图片的大小的尺寸都是1/2 1/4 1/8...的比例渐变的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;
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 翻译