0

I have an image of height 200 and 400 pixels. The way I want to display all these images is in height of 200 pixels. I mean to say whatever the size of an image, while displaying that image I want to display image up to a height of 200 pixels. The rest portion of the image is hidden. So how that can be done? I have used one code for decoding but here it stretches the bigger size image and then displays it. In my case, I don't want the image to stretch but only display images up to height 200 pixels.

Code I used:

private Bitmap decodeFile(File f)
{
    try 
    {
        // decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        
        // Find the correct scale value.
        final int REQUIRED_SIZE = 200;
        int height_tmp = o.outHeight;
        while(true)
        {
            if(height_tmp/2 < REQUIRED_SIZE)
                break;          
            height_tmp/=2;
        }

        o.inSampleSize = height_tmp;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o);
    } 
    catch (FileNotFoundException e) 
    {}
    return null;
}
iknow
  • 4,796
  • 9
  • 19
  • 37
AndroidDev
  • 4,331
  • 21
  • 73
  • 123

2 Answers2

1

just edit your main.xml

    android:layout_width="200dp"
    android:layout_height="200dp"
jeet.chanchawat
  • 5,228
  • 5
  • 31
  • 57
  • My requirement is different. Actually my images are save in internal storage file. All the images have different height. So i want to read all the images from the internal Storage and display it in list view with height 200 pixel. – AndroidDev Jun 20 '12 at 06:17
  • Relative layout.If i fixed the size the images with height more then 200 pixel stretch while displaying – AndroidDev Jun 20 '12 at 06:22
1

Ok, So try layoutParams for doing the same task programatically.

  public void taskCompleted ()
{

    ImageView iv = new ImageView(this);


    Bitmap completionBitmap = null;
    completionBitmap = BitmapFactory.decodeFile(CommonMessageConstants.BASE_IMAGE_FOLDER_ON_DEVICE +"completionimage.png");


    iv.setImageBitmap(completionBitmap);
    RelativeLayout rl = (RelativeLayout) findViewById(R.id.randomImageView);
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    //lp.setMargins(200, 200, 0, 0);

    lp.addRule(RelativeLayout.CENTER_IN_PARENT);


    rl.addView(iv, lp);

    Toast.makeText (getApplicationContext(), msg, Toast.LENGTH_SHORT).show ();

} 
jeet.chanchawat
  • 5,228
  • 5
  • 31
  • 57