0

I am using imageview in android and when I put image in imageview using attributes wrap_content for both height and width. Images appear to be so much big as they are of real size or if i took small images they got blur.

code for it


         <LinearLayout android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/showhide"
        android:layout_marginRight="20sp"
        android:layout_marginLeft="20sp"
        android:layout_marginTop="10sp"
        android:padding="5sp"
        android:weightSum="4"
        >

        <!-- delivery intimation-->
        <LinearLayout android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/layout_d_i"
            android:orientation="vertical"
            android:gravity="center_horizontal"
            android:layout_weight="1"
            android:weightSum="2"


            >
            <ImageView android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="0.5"
                android:src="@drawable/delivintimation"
                android:id="@+id/delivery_intimation"
                android:layout_centerInParent="true"
                android:layout_gravity="center"
                android:layout_marginBottom="10sp"

                />

            <TextView android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Delivery Intimation"
                android:gravity="center"
                android:maxLines="100"
                android:singleLine="false"
                android:layout_below="@id/delivery_intimation"
                android:textSize="12sp"
                android:textColor="@android:color/black"
                />

        </LinearLayout>
          </LinearLayout>

Mukesh Mishra
  • 241
  • 1
  • 3
  • 12
  • Give it custom height and width according to your need. –  Dec 26 '16 at 09:54
  • give your xml code. – Rushi Ayyappa Dec 26 '16 at 09:59
  • if you want to show image in list or recyclerview then just use 75dp * 75dp – SANAT Dec 26 '16 at 10:00
  • Possible duplicate of [What's the difference between fill\_parent and wrap\_content?](http://stackoverflow.com/questions/432763/whats-the-difference-between-fill-parent-and-wrap-content) – Charuක Dec 26 '16 at 10:01
  • check the image size before adding to the project, use small images for small size, use all the drawable folders to add images, so that you won't get blur images. – Mohd Asif Ahmed Dec 26 '16 at 10:02
  • read read -> wrap_content --> Setting a View's size to wrap_content will force it to expand only far enough to contain the values (or child controls) it contains. For controls -- like text boxes (TextView) or images (ImageView) -- this will wrap the text or image being shown. For layout elements it will resize the layout to fit the controls / layouts added as its children. It's roughly the equivalent of setting a Windows Form Control's Autosize property to True. – Charuක Dec 26 '16 at 10:03
  • posted code please check @RushiAyyappa – Mukesh Mishra Dec 26 '16 at 10:54
  • try to adjust ScaleType attribute – Rohan Surve Dec 26 '16 at 10:08

7 Answers7

6

Use android:scaleType="centerInside"

Mohammad Misbah
  • 870
  • 8
  • 19
1

Did you try using android:scaleType attribute?

Example:

android:scaleType="centerCrop"

This attribute uses many other values like, 'fitXY', 'center', 'centerInside' etc. Try this

Charuක
  • 12,953
  • 5
  • 50
  • 88
Abhi
  • 2,115
  • 2
  • 18
  • 29
0

Use fixed size width and height on imageview.

<ImageView
    android:layout_width="200dp"
    android:layout_height="180dp"
    android:id="@+id/imageView"
    android:src="@drawable/image"
    android:scaleType="fitXY"/>
jobair ahmed
  • 141
  • 1
  • 9
0

Use LinearyLayout to wrap your ImageView and another invisible View, and set layout_weight both to 0.3 or 0.5 etc, then the ImageView will be according to your size.

Here is an example for you showing how to set the image half the screen size

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5"
        android:background="#ff0000"
        >
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/your_image"
            android:adjustViewBounds="true"
            />
    </RelativeLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5"
        android:background="#00ff00"
        />
</LinearLayout>

If you want to put your image in center of the screen, you can use the same idea and set proper weight to achieve your goal

Anchal Singh
  • 329
  • 3
  • 13
0

You can manually give width and height programatically

<ImageView   android:layout_width="100dp"
android:layout_height="200dp"
android:src="@drawable/notes"
android:id="@+id/iv_notes"
android:scaleType="fitXY"/>

scaleType-- fitXY will compress and fit all image inside imageview scaleType-- centerCrop will crop the image to the image view size

If you dont want to hard code the Width and height you have to create different drawable folders and place your different resolution images in different folder , Examples of drawable folders are

 drawable-ldpi
 drawable-mdpi
 drawable-hdpi
 drawable-xhdpi
 drawable-xxhdpi
 drawable-xxxhdpi
Manohar
  • 22,116
  • 9
  • 108
  • 144
0

use src instead of background in xml please give the xml to see how you implemented it

iDeveloper
  • 1,699
  • 22
  • 47
0

The code which you have used gave you the correct result: you have used LinearLayout and used weights. weights will divide according to the screen resolution. just remove LinearLayout and use Relativelayout instead. Next remove all the weights for images and set its attributes to wrap_content. If you want to give images that fits exactly to your screen width do it programmatically.

 DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
int desiredWidth=width/2;
yourView.setHeight(desiredWidth);

if this is not your requirement then in your RelativeLayout where you have these images. give attributes . android:alignParentRight="true" for one and android:alignParentLeft="true" for another image and set margins.

Rushi Ayyappa
  • 2,708
  • 2
  • 16
  • 32