newebook
newebook

Reputation: 1

android ImageView auto scale

I'm new to Android. I'm writing an example that's called MemoryPractice. In my app I use ImageView to display an image. I want them to auto scale to fit in size of screen. So I use TableLayout to fill all ImageViews. In designer, it displays normally, but in run-time when I touch on ImageViews that changes its size and the others auto scale. I don't want size of ImageView to scale. It must keep its size as designed.

What should I do?

Here's the layout.xml:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black_overlay"
tools:context=".TableLayout" >

<TableRow
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="1dp" >

    <ImageView
        android:id="@+id/img1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_margin="1dp"
        android:layout_weight="1"
        android:contentDescription="@string/contentDescription"
        android:scaleType="fitXY"
        android:src="@drawable/image1" />

    <ImageView
        android:id="@+id/img2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="1dp"
        android:layout_weight="1"
        android:contentDescription="@string/contentDescription"
        android:scaleType="fitXY"
        android:src="@drawable/image1" />

    <ImageView
        android:id="@+id/img3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="1dp"
        android:layout_weight="1"
        android:contentDescription="@string/contentDescription"
        android:scaleType="fitXY"
        android:src="@drawable/image1" />

    <ImageView
        android:id="@+id/img4"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_margin="1dp"
        android:layout_weight="1"
        android:contentDescription="@string/contentDescription"
        android:scaleType="fitXY"
        android:src="@drawable/image1" />

    <ImageView
        android:id="@+id/img5"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_margin="1dp"
        android:layout_weight="1"
        android:contentDescription="@string/contentDescription"
        android:scaleType="fitXY"
        android:src="@drawable/image1" />

    <ImageView
        android:id="@+id/img6"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="1dp"
        android:layout_weight="1"
        android:contentDescription="@string/contentDescription"
        android:scaleType="fitXY"
        android:src="@drawable/image1" />
</TableRow>

<TableRow
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="1dp" >

    <ImageView
        android:id="@+id/img7"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:layout_margin="1dp"
        android:layout_weight="1"
        android:contentDescription="@string/contentDescription"
        android:scaleType="fitXY"
        android:src="@drawable/image1" />

    <ImageView
        android:id="@+id/img8"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:layout_margin="1dp"
        android:layout_weight="1"
        android:contentDescription="@string/contentDescription"
        android:scaleType="fitXY"
        android:src="@drawable/image1" />

    <ImageView
        android:id="@+id/img9"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:layout_margin="1dp"
        android:layout_weight="1"
        android:contentDescription="@string/contentDescription"
        android:scaleType="fitXY"
        android:src="@drawable/image1" />

    <ImageView
        android:id="@+id/img10"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:layout_margin="1dp"
        android:layout_weight="1"
        android:contentDescription="@string/contentDescription"
        android:scaleType="fitXY"
        android:src="@drawable/image1" />

    <ImageView
        android:id="@+id/img11"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:layout_margin="1dp"
        android:layout_weight="1"
        android:contentDescription="@string/contentDescription"
        android:scaleType="fitXY"
        android:src="@drawable/image1" />

    <ImageView
        android:id="@+id/img12"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:layout_margin="1dp"
        android:layout_weight="1"
        android:contentDescription="@string/contentDescription"
        android:scaleType="fitXY"
        android:src="@drawable/image1" />
</TableRow>

<TableRow
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="1dp" >

    <ImageView
        android:id="@+id/img13"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:layout_margin="1dp"
        android:layout_weight="1"
        android:contentDescription="@string/contentDescription"
        android:scaleType="fitXY"
        android:src="@drawable/image1" />

    <ImageView
        android:id="@+id/img14"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:layout_margin="1dp"
        android:layout_weight="1"
        android:contentDescription="@string/contentDescription"
        android:scaleType="fitXY"
        android:src="@drawable/image1" />

    <ImageView
        android:id="@+id/img15"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:layout_margin="1dp"
        android:layout_weight="1"
        android:contentDescription="@string/contentDescription"
        android:scaleType="fitXY"
        android:src="@drawable/image1" />

    <ImageView
        android:id="@+id/img16"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:layout_margin="1dp"
        android:layout_weight="1"
        android:contentDescription="@string/contentDescription"
        android:scaleType="fitXY"
        android:src="@drawable/image1" />

    <ImageView
        android:id="@+id/img17"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:layout_margin="1dp"
        android:layout_weight="1"
        android:contentDescription="@string/contentDescription"
        android:scaleType="fitXY"
        android:src="@drawable/image1" />

    <ImageView
        android:id="@+id/img18"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:layout_margin="1dp"
        android:layout_weight="1"
        android:contentDescription="@string/contentDescription"
        android:scaleType="fitXY"
        android:src="@drawable/image1" />
</TableRow>

Upvotes: 0

Views: 4636

Answers (2)

Obsth&#228;ndler
Obsth&#228;ndler

Reputation: 313

I think this will help you:

android:scaleType="centerCrop"
android:adjustViewBounds="true"

Try this in your image view.

Upvotes: 3

Ram
Ram

Reputation: 2530

You can fetch the screen size before you draw the image. Then you re-scale your image and finally draw the image. You can set image size depending on screen size.

Implement the screen size capture process and scaling process in the onTouch() method. For reference look at this:

How to dynamically change ImageView Height

Upvotes: 0

Related Questions