user6735728
user6735728

Reputation:

Android Fit Image into ImageView

I get an image from a socket server using this:

    byte[] decodedString = Base64.decode(values[26], Base64.NO_WRAP);
    master_bitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
    master_frame.setImageBitmap(master_bitmap);

Xml

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

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/textView2"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="27dp"
    android:layout_marginStart="27dp"
    android:layout_marginTop="39dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/textView4"
    android:layout_alignTop="@+id/textView2"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_marginRight="50dp"
    android:layout_marginEnd="50dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/textView6"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="196dp" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/iv_master"
    android:layout_below="@+id/textView2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_toStartOf="@+id/textView6"
    android:layout_above="@+id/textView6"
    android:layout_toLeftOf="@+id/textView6" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/iv_slave"
    android:layout_above="@+id/textView6"
    android:layout_toRightOf="@+id/textView6"
    android:layout_alignTop="@+id/iv_master"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:scaleType="centerInside"
    android:adjustViewBounds="true"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/textView8"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:textSize="12dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/textView9"
    android:layout_alignBottom="@+id/textView8"
    android:layout_toRightOf="@+id/textView8"
    android:layout_toEndOf="@+id/textView8"
    android:textSize="12dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/textView14"
    android:layout_alignBottom="@+id/textView9"
    android:layout_toRightOf="@+id/textView9"
    android:layout_toEndOf="@+id/textView9"
    android:textSize="12dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/textView22"
    android:layout_alignBottom="@+id/textView14"
    android:layout_toRightOf="@+id/textView14"
    android:layout_toEndOf="@+id/textView14"
    android:textSize="12dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/textView23"
    android:layout_alignBottom="@+id/textView22"
    android:layout_toRightOf="@+id/textView22"
    android:layout_toEndOf="@+id/textView22"
    android:textSize="12dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/textView24"
    android:layout_alignParentBottom="true"
    android:layout_toRightOf="@+id/textView23"
    android:layout_toEndOf="@+id/textView23"
    android:textSize="12dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/textView25"
    android:layout_alignParentBottom="true"
    android:layout_toRightOf="@+id/textView6"
    android:layout_toEndOf="@+id/textView6"
    android:textSize="12dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/textView26"
    android:layout_alignBottom="@+id/textView25"
    android:layout_toRightOf="@+id/textView25"
    android:layout_toEndOf="@+id/textView25"
    android:textSize="12dp" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/iv_arena"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_above="@+id/textView14"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

I want to fit the image on (Layout) :

Layout

But it displays like this:

I want to fit all the Bitmaps correctly in the ImageViews. It can be stretched.

PS: It's also very hard to make a layout... Why I cant put the image views near each other?

ScreenShot of My Android ScreenShot of My Android

The fist two images are 640x360 and the last 1159x398

Upvotes: 1

Views: 7869

Answers (3)

Bhaval Patel
Bhaval Patel

Reputation: 409

Step 1: Get device display width & Height dynamically using.

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

int height = metrics.heightPixels;
int width = metrics.widthPixels;

Step 2: Set Layout parameter to image view

imagePhoto.setLayoutParams(getLayout(5, 5, height/2, width/2));




 private FrameLayout.LayoutParams getLayout(int fMarginLeft, int fMarginTop, int fWidth, int fHeight) {
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        params.setMargins(fMarginLeft, fMarginTop, 0, 0);
        params.width = fWidth;
        params.height = fHeight;
        return params;
    }

Upvotes: 0

Nikhil Borad
Nikhil Borad

Reputation: 2085

android:scaleType="fitxy" in xml

this will fill your whole Imageview.

Upvotes: 2

Hobo Joe
Hobo Joe

Reputation: 902

If you want an image of that aspect ratio to fit into that ImageView, use android:scaleType="centerCrop" in its XML attributes.

Upvotes: 2

Related Questions