2013-09-26 48 views
0

我目前使用自定義ArrayAdapter作爲列表。列表項目跨越屏幕的整個寬度,所以在一些設備上非常寬。縮放和錨定圖像視圖內的圖像

我在一些列表項上覆蓋了一個ImageView,您可以輕掃一下。 用於ImageView的XML如下:

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_centerInParent="true" 
    android:scaleType="matrix" 
    android:src="@drawable/myImage" /> 

視圖正確地放置,但是,爲了使圖像以適合與真寬屏幕的設備,或在橫向模式下,圖像具有非常寬。

問題是,我想縮放圖像以適應垂直方向(即fitY,它不存在爲scaleType,然後將圖像錨定在視圖的左側,對於不適合的所有內容被裁剪

我已經嘗試了所有的scaleType價值觀,他們都不是完全正確的,因此,我覺得我也許應該使用matrix和定義我自己翻譯的形象,所以我在getView嘗試這樣做。:

//snipped out code before to initiate variables etc. 
Drawable src = imageView.getDrawable(); 

//scale by the ratio of the heights. 
int scaleFactor = imageView.getHeight()/src.getIntrinsicHeight(); 

Matrix imageMatrix = new Matrix(); 

//set the scale factor for the imageMatrix 
imageMatrix.setScale(scaleFactor, scaleFactor); 

//set the translation to be 0,0 (top left); 
imageMatrix.setTranslate(0,0); 

//assign the image matrix to the imageview. 
imageView.setImageMatrix(imageMatrix); 

但是這沒有任何所有的效果(即使我讓這些值真的很瘋狂)。我認爲我在錯誤的地方設置了imageMatrix - 是否有一個onDraw()事件我可以掛鉤定製ArrayAdapter子類?

或者我正在以完全錯誤的方式解決問題?

+0

你有沒有試過@ @維恩/'不同大小的屏幕? – Si8

+0

你能解釋一下如何工作嗎? –

+0

用幾句話解釋有點難,但您可以訪問http://developer.android.com/guide/practices/screens_support.html瞭解更多信息。如果你瞭解它,讓我知道。你想要爲其設計什麼Android版本?如果它是3.0和以上,你可以使用它:) – Si8

回答

1

最後,我必須明確地在代碼中設置imageView.setScaleType(ScaleType.MATRIX);,然後申請我imageMatrix它。使用xml屬性scaleType="matrix"似乎不起作用。

-1

只有根據您正在使用的API級別將layout_width和layout_height設置爲match_parent或fill_parent,fitXY scaleType才起作用。 所以你必須在只有你的XML使用此代碼:

<ImageView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_centerInParent="true" 
    android:scaleType="fitXY" 
    android:src="@drawable/myImage" /> 
+0

但我不想適合X和Y,只是Y. –

+0

嘿@EdHinchliffe ,...我遇到過類似的情況。這裏http://stackoverflow.com/questions/32023108/android-how-to-scaley-to-based-on-other-objects-position你能幫我嗎? – gumuruh