2013-10-24 136 views
0

我是Android新手。我正在寫一個名爲MemoryPractice的例子。在我的應用程序中,我使用ImageView來顯示圖像。我希望他們能夠自動縮放以適應屏幕大小。所以我用TableLayout來填充所有的ImageView s。在設計器中,它正常顯示,但是在運行時,當我觸摸ImageView時,它會改變其大小,其他的會自動縮放。 我不希望大小ImageView規模。它必須按照設計保持其大小。android ImageView自動縮放

我該怎麼辦?

這裏的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> 

+1

您的示例圖像未顯示。並介意顯示您在onTouch中運行的代碼? –

回答

0

您繪製圖像之前,您可以獲取屏幕尺寸。然後你重新調整你的圖像,最後繪製圖像。您可以根據屏幕大小設置圖像大小。

onTouch()方法中實現屏幕尺寸捕獲過程和縮放過程。僅供參考看看這個:

How to dynamically change ImageView Height

1

我認爲這將有助於你:

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

在圖像視圖試試這個。