1. Are you ready for the Galaxy S20? Here is everything we know so far!

Scaling image issue

Discussion in 'Android Development' started by B8787, Aug 14, 2012.

  1. B8787

    B8787 Newbie
    Thread Starter

    Hi all,

    I am having a slight scaling issue. I got some code from the net but I don't really know what it does and if it is the best solution to scale an image, I can't figure it out. The problem is with images with a large height the decoder automatiscally cuts off a part from the top and bottom of the picture and I don't know what causes this. The code is:

    Code (Text):
    1.  //decodes image and scales it to reduce memory consumption
    2.     private Bitmap decodeFile(File f){
    3.         try {
    4.             //decode image size
    5.             BitmapFactory.Options o = new BitmapFactory.Options();
    6.             o.inJustDecodeBounds = true;
    7.             BitmapFactory.decodeStream(new FileInputStream(f),null,o);
    8.            
    9.             //Find the correct scale value. It should be the power of 2.
    10.             final int REQUIRED_SIZE=100;
    11.             int width_tmp=o.outWidth, height_tmp=o.outHeight;
    12.             int scale=1;
    13.             while(true){
    14.                 if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
    15.                     break;
    16.                 width_tmp/=2;
    17.                 height_tmp/=2;
    18.                 scale*=2;
    19.             }
    20.            
    21.             //decode with inSampleSize
    22.             BitmapFactory.Options o2 = new BitmapFactory.Options();
    23.             o2.inSampleSize=scale;
    24.             return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    25.         } catch (FileNotFoundException e) {}
    26.         return null;
    27.     }
    and

    Code (Text):
    1. <ImageView
    2.       android:id="@+id/image"
    3.       android:layout_width="fill_parent"
    4.       android:layout_height="fill_parent"  android:scaleType="centerCrop"
    5.        />
    Thanks for your help!
     



    1. Download the Forums for Android™ app!


      Download

       
Loading...
Similar Threads - Scaling image issue
  1. Toneek
    Replies:
    0
    Views:
    82
  2. happydz
    Replies:
    1
    Views:
    264
  3. ZigZag03
    Replies:
    6
    Views:
    474
  4. xarzu
    Replies:
    1
    Views:
    774
  5. OmTec Web
    Replies:
    0
    Views:
    595
  6. OmTec Web
    Replies:
    0
    Views:
    527
  7. App Update
    Replies:
    0
    Views:
    393
  8. matthewpederson
    Replies:
    0
    Views:
    1,938
  9. App Update
    Replies:
    1
    Views:
    685
  10. Mehak Gul
    Replies:
    1
    Views:
    850

Share This Page

Loading...