【发布时间】:2011-01-25 17:28:47
【问题描述】:
我正在尝试实现一个可以容纳横向或纵向图像的 ImageView。 这些图像应该适合图像视图的宽度(如果是横向)或高度(如果是纵向),但在任何情况下,它们都必须与视图的顶部对齐,并且没有边距或填充。
我想要实现类似于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);
}
【问题讨论】:
-
我想做类似的事情,你有没有找到解决方案?
-
到目前为止没有运气,我最终使用了我上面发布的代码......
-
好的,感谢代码!
标签: android imageview drawable