DevBytes: Bitmap Scaling

圖片的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);
    }
}

截圖:


ps:我們可以看到圖片的大小的尺寸都是1/2 1/4 1/8...的比例漸變的



圖片的尺寸:

我們知道用 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 翻譯


發佈了120 篇原創文章 · 獲贊 1 · 訪問量 27萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章