• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.bitmapscaling;
18 
19 import android.app.Activity;
20 import android.graphics.Bitmap;
21 import android.graphics.BitmapFactory;
22 import android.os.Bundle;
23 import android.widget.ImageView;
24 import android.widget.LinearLayout;
25 import android.widget.LinearLayout.LayoutParams;
26 
27 /**
28  * This example shows how the use of BitmapOptions affects the resulting size of a loaded
29  * bitmap. Sub-sampling can speed up load times and reduce the need for large bitmaps
30  * in memory if your target bitmap size is much smaller, although it's good to understand
31  * that you can't get specific Bitmap sizes, but rather power-of-two reductions in sizes.
32  *
33  * Watch the associated video for this demo on the DevBytes channel of developer.android.com
34  * or on YouTube at https://www.youtube.com/watch?v=12cB7gnL6po.
35  */
36 public class BitmapScaling extends Activity {
37 
38     @Override
onCreate(Bundle savedInstanceState)39     public void onCreate(Bundle savedInstanceState) {
40         super.onCreate(savedInstanceState);
41         setContentView(R.layout.activity_bitmap_scaling);
42 
43         LinearLayout container = (LinearLayout) findViewById(R.id.scaledImageContainer);
44         ImageView originalImageView = (ImageView) findViewById(R.id.originalImageHolder);
45 
46         Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
47                 R.drawable.jellybean_statue);
48         originalImageView.setImageBitmap(bitmap);
49 
50         for (int i = 2; i < 10; ++i) {
51             addScaledImageView(bitmap, i, container);
52         }
53     }
54 
addScaledImageView(Bitmap original, int sampleSize, LinearLayout container)55     private void addScaledImageView(Bitmap original, int sampleSize, LinearLayout container) {
56 
57         // inSampleSize tells the loader how much to scale the final image, which it does at
58         // load time by simply reading less pixels for every pixel value in the final bitmap.
59         // Note that it only scales by powers of two, so a value of two results in a bitmap
60         // 1/2 the size of the original and a value of four results in a bitmap 1/4 the original
61         // size. Intermediate values are rounded down, so a value of three results in a bitmap 1/2
62         // the original size.
63 
64         BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
65         bitmapOptions.inSampleSize = sampleSize;
66 
67         Bitmap scaledBitmap = BitmapFactory.decodeResource(getResources(),
68                 R.drawable.jellybean_statue, bitmapOptions);
69         ImageView scaledImageView = new ImageView(this);
70         scaledImageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
71                 LayoutParams.WRAP_CONTENT));
72         scaledImageView.setImageBitmap(scaledBitmap);
73         container.addView(scaledImageView);
74     }
75 }
76