【问题标题】:Scaling and anchoring an image within an imageview在图像视图中缩放和锚定图像
【发布时间】:2013-10-02 07:48:49
【问题描述】:

我目前正在为列表使用自定义 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 子类挂钩?

或者我打算以完全错误的方式解决问题?

【问题讨论】:

  • 你试过@dimen/不同尺寸的屏幕吗?
  • 你能解释一下它是如何工作的吗?
  • 这有点难以用几句话来解释,但您可以访问developer.android.com/guide/practices/screens_support.html 了解更多信息。让我知道你是否理解。您要为哪个 Android 版本设计它?如果它是 3.0 及更高版本,您可以使用它:)
  • 哦,我明白了,您的意思是根据屏幕大小使用不同的可绘制对象。这是一种选择,但仍然不理想。鉴于没有一组有限的屏幕宽度(即屏幕实际上可以是 any 宽度),那么您仍然需要稍微缩放或拉伸图像。
  • 您是对的,但请记住,如果您有一个“大”图像,将其缩小并不总是会使图像模糊。您可以在 Play Store 中查看我使用 @dimen/ 设计的几个应用程序,并且我在多个屏幕尺寸的模拟器中进行了测试,它们运行良好。 (这是其中之一:play.google.com/store/apps/details?id=com.sikni8.colorfinders

标签: android listview layout imageview


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    仅当您根据您使用的 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" />
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-17
    • 1970-01-01
    • 2015-06-28
    • 1970-01-01
    • 2012-05-24
    相关资源
    最近更新 更多