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;
}
1

2 Answers 2

1

just edit your main.xml

    android:layout_width="200dp"
    android:layout_height="200dp"
Sign up to request clarification or add additional context in comments.

2 Comments

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.
Relative layout.If i fixed the size the images with height more then 200 pixel stretch while displaying
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 ();

} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.