0

I am trying to click a photo using the camera, and then display it in imageView. Initially, the image looked extremely small, and did not fit in the imageView. However, after reading a couple of blogs, I made the following change to the code:

photo = android.provider.MediaStore.Images.Media.getBitmap(cr,Uri.fromFile(tempPhoto));
//image.setImageBitmap(photo);
Drawable drawable = new BitmapDrawable(getResources(), photo);
image.setBackground(drawable);
image.setScaleType(ScaleType.FIT_CENTER);

My Layout file is as shown:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

      <AutoCompleteTextView
            android:id="@+id/location"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="1dip"
            android:background="@color/translucent_grey"
            android:ems="10"
            android:hint="@string/hint_location"
            android:inputType="text"
            android:textSize="14sp" >

            <requestFocus />
    </AutoCompleteTextView>

<RelativeLayout
     android:layout_width="fill_parent"
     android:layout_height="200dp" >
     <ImageButton
         android:id="@+id/snap"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignBottom="@+id/image"
         android:layout_alignParentRight="true"
         android:layout_marginBottom="108dp"
         android:layout_marginRight="160dp"
         android:background="@color/white"
         android:src="@drawable/camera" />

     <ImageView
         android:id="@+id/image"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:layout_alignParentLeft="true"
         android:orientation="horizontal" />
</RelativeLayout>     

<FrameLayout
        android:id="@+id/datetime_container"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_marginBottom="2dp" />
<FrameLayout 
    android:id="@+id/eventDetails_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="2dp" />

</LinearLayout>
</ScrollView>

After adding this, the image stretches too much, and looks quite elongated. I am aiming to fit the image on the screen width, and then have flexible image height so that the scale can be maintained (just the way fb modile displays portrait and landscape images fitting along the width of the screen)

Does anyone know how?

gazubi
  • 561
  • 8
  • 32

6 Answers6

1

use this parameter for ImageView

android:scaleType="fitXY"
RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
1

use:

image.setScaleType(ScaleType.FIT_XY);

And then try to set height of imageView programmatically.

Avijit
  • 3,834
  • 4
  • 33
  • 45
Waqar Ahmed
  • 5,005
  • 2
  • 23
  • 45
1

use parameter

  android:adjustViewBounds="true"
        android:scaleType="fitXY"
Shyam
  • 6,376
  • 1
  • 24
  • 38
0

After seeing your code you should not use fill parent in spite use wrap content.

<ImageView
     android:id="@+id/image"
     android:layout_width="wrap_content"//fill_parent
     android:layout_height="wrap_content"//fill_parent
     android:layout_alignParentLeft="true"
     android:orientation="horizontal" />
Daud Arfin
  • 2,499
  • 1
  • 18
  • 37
0

Generally for the ImageView use setImageDrawable(drawable); because the ImageView it self take cares about the scaling and streching and you can use scaleType according to your requirement

Jagadesh Seeram
  • 2,630
  • 1
  • 16
  • 29
  • Could you tell me what is the code for it..how is it different from what i have done already – gazubi Dec 27 '13 at 07:26
  • you used **setBackground()** this resembles to BackGround, Where as **setImageDrawable** resembles to the src of the imageview..check this http://stackoverflow.com/questions/5454491/what-is-the-difference-between-src-and-background-of-imageview – Jagadesh Seeram Dec 27 '13 at 07:32
0

This is my code:

 photo = android.provider.MediaStore.Images.Media.getBitmap(cr, Uri.fromFile(tempPhoto));
 //image.setImageBitmap(photo);
 Drawable drawable = new BitmapDrawable(getResources(), photo);
 image.setBackground(drawable);
 image.setScaleType(ScaleType.FIT_XY);

Layout file

<RelativeLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content" >
     <ImageButton
         android:id="@+id/snap"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignBottom="@+id/image"
         android:layout_alignParentRight="true"
         android:layout_marginBottom="108dp"
         android:layout_marginRight="160dp"
         android:background="@color/white"
         android:src="@drawable/camera" />

     <ImageView
         android:id="@+id/image"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentLeft="true"
         android:adjustViewBounds="true"
         android:orientation="horizontal" />
</RelativeLayout>  

This is my output: enter image description here

gazubi
  • 561
  • 8
  • 32