2014-05-20 38 views
2

我使用的是使用ImageView的縮放效果,所以我需要使用scaletype = matrix。現在,我想將中心位置設置爲ImageView,但我無法設置它。

請幫我解決這個問題。

layout.xml:ImageView使用ScaleType矩陣的位置

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <ImageView 
     android:id="@+id/imgImage" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:contentDescription="@string/imgdesc" 
     android:scaleType="matrix" 
     android:src="@drawable/ic_answer" /> 

</RelativeLayout> 
+0

沒有你有任何解決方案,請與我們分享 –

回答

1

試試這個你ImageView

android:layout_centerInParent="true" 
+0

我已經嘗試過這一點,但它不力解決? –

+0

@JeetenParmar更改'android:scaleType =「centerCrop」'並嘗試 –

+1

我縮放圖像,所以我不能使用任何人期望矩陣。 –

0

試試下面的代碼:

android:scaleType="centerInside" 

android:scaleType="fitCenter" 

android:scaleType="centerCrop" 
+0

我縮放圖像,以便所有這些將無法通過替換矩陣工作。 –

-1

試試這個,

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <ImageView 
     android:id="@+id/imgImage" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:scaleType="matrix" 
     android:src="@drawable/ic_launcher" /> 

</RelativeLayout> 

這可能會幫助你

+2

這樣做肯定會奏效,但是'ImageView'設置了邊界,因此它不允許用戶將圖像放大到邊界之外。 –

6

把下面的代碼行:

RectF drawableRect = new RectF(0, 0, image_width, image_height); 
RectF viewRect = new RectF(0, 0, screen_width, screen_height); 
matrix.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.CENTER); 

希望這會幫助你。

0

android:scaleType="matrix"情況下,我發現的唯一方法就是使用矩陣來設置ImageView的位置,使用下面的代碼:

Matrix matrix = new Matrix(); 
matrix.postTranslate(screen_width/2, screen_height/2); 
imageView.setImageMatrix(matrix); 
0

我有sameproblem。最後,我放棄了使用矩陣以及與此替代它:

android.view.ViewGroup.LayoutParams params = imageView.getLayoutParams(); 
params.height = desiredHeight; 
params.width = desiredWid; 
imageView.setLayoutParams(params);