【问题标题】:ImageView autoresizing when getting a downloaded drawable获取下载的可绘制对象时 ImageView 自动调整大小
【发布时间】:2012-08-29 01:07:52
【问题描述】:

我正在正确获取我下载的可绘制对象并且它们正确显示。我只想在我的 ImageView 上显示图像的顶部 144x284dip。相反,当下载图像时,ImageView 会将自身调整为可绘制对象的宽度和高度。我尝试修改 layoutparams 和 minimumwidth / minimumheight,但无济于事。

我能做些什么来强制 ImageView 保持在 144x284 吗?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="284dip"
    android:layout_height="144dip" >

    <ImageView
        android:id="@+id/imageViewLogo"
        android:layout_width="284dip"
        android:layout_height="144dip" />

</LinearLayout>

【问题讨论】:

  • ImageView 的布局管理器是什么?能否请您发布布局 XML 文件?

标签: android drawable android-imageview


【解决方案1】:

如果我理解正确,你想做两件事:

  1. 将 ImageView 的大小固定为 284dip X 144dip。
  2. 仅显示图像的左上部分,未缩放。

执行第二部分将需要您使用“矩阵”比例类型并将比例设置为 1.0 并将变换因子设置为 0.0。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="284dip"
        android:layout_height="144dip"
        android:scaleType="matrix"
        android:src="@drawable/ic_launcher" />
</LinearLayout>

默认情况下,比例因子为 1,平移为 0。因此,您无需执行任何其他操作。如果您确实希望进行不同的缩放和转换,您可以编写这样的代码。

void showImage(Bitmap bm) {
    float scaleFactor = ...;
    float transform = ...;

    imageView.setImageBitmap(bm);
    final Matrix matrix = new Matrix();
    matrix.setScale(scaleFactor, scaleFactor);
    matrix.setTranslate(transform, transform);
    imageView.setImageMatrix(matrix);
}

【讨论】:

    【解决方案2】:

    好的,我解决了。

    我刚刚这样做了:

    Bitmap newBitmap = Bitmap.createBitmap(bm, 0, 0, 144, 284);
    

    然后将其设置为 ImageView。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多