0

我目前正在使用自定义 ArrayAdapter 作为列表。列表项跨越屏幕的整个宽度,因此在某些设备上非常宽。

我覆盖了ImageView一些列表项,您可以滑动以将其关闭。的xmlImageView如下:

<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不存在作为 a 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子类挂钩?

还是我要以完全错误的方式解决问题?

4

2 回答 2

1

最后,我必须在代码中明确设置imageView.setScaleType(ScaleType.MATRIX);,然后将 myimageMatrix应用于它。使用 xml 属性scaleType="matrix"似乎不起作用。

于 2013-09-26T13:08:36.560 回答
-1

fitXY scaleType 仅在您根据您使用的 API 级别将 layout_width 和 layout_height 设置为 match_parent 或 fill_parent 时才起作用。所以你必须只在你的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" />
于 2013-09-26T11:20:27.907 回答