5
我試圖實現ImageView,可以同時保存橫向或縱向圖像。 這些圖像應該適合imageview的寬度(如果橫向)或高度(如果縱向),但無論如何它們必須與視圖的頂部對齊,無邊距或填充。Android ImageView ScaleType * FIT_TOP *
我想實現的是類似android:scaleType="fitStart"
但集中在縱向圖像的情況下,或對齊到橫向圖像的情況下。
補充:
我現在用這樣一個醜陋的代碼,這似乎是工作,但不知道這是最好的解決辦法:
<com.custom.layout.MyImageView
android:id="@+id/detail_view_image"
android:src="@drawable/logo"
android:background="#fff"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:cropToPadding="false"
android:adjustViewBounds="false"
/>
,然後在我的課,擴展ImageView的我這樣做:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int imgWidth = getDrawable().getIntrinsicWidth();
int imgHeight = getDrawable().getIntrinsicHeight();
if(imgWidth>imgHeight)
setScaleType(ScaleType.FIT_START);
else
setScaleType(ScaleType.FIT_CENTER);
int width = measureWidth(widthMeasureSpec);
int height = measureHeight(heightMeasureSpec);
setMeasuredDimension(width, height);
}
我期待着做類似的事情,你有沒有找到這個解決方案? – 2012-06-14 16:16:22
到目前爲止沒有運氣,我結束了使用我上面發佈的代碼... – 0m4r 2012-06-18 13:55:24