【问题标题】:Android change imageview size dynamically by image sizeAndroid通过图像大小动态改变imageview大小
【发布时间】:2017-02-20 07:30:56
【问题描述】:

您好,我有一个显示来自 url 的图像的 ImageView。图像大小不同。它们可以是方形的或纵向的或横向的。我需要相应地设置图像视图大小而不进行拉伸或裁剪。

如果图像大小是 512x512,那么 ImageView 应该是正方形并且对齐屏幕的中心。

如果图像大小类似于 1024x800,则 ImageView 大小应为水平矩形并对齐屏幕中心。

在所有情况下,宽度都应与父级匹配,但高度应相应调整。我怎样才能做到这一点?

在我的xml代码下面。

    <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <android.support.v7.widget.CardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:maxHeight="400dp"
        android:maxWidth="300dp"
        android:layout_margin="10dp"
        app:cardBackgroundColor="@color/white">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <android.support.v4.widget.NestedScrollView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scrollbars="none">
                <ImageView
                    android:id="@+id/image"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:adjustViewBounds="true"
                    android:scaleType="fitXY"
                    android:minWidth="260dp"
                    tools:ignore="ContentDescription" />
            </android.support.v4.widget.NestedScrollView>

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@color/grey_light" />

            <TextView
                android:id="@+id/title"
                android:layout_width="match_parent"
                android:layout_height="35dp"
                android:layout_gravity="right"
                android:layout_margin="@dimen/space_small_low"
                android:background="@drawable/transparent_bg"
                android:textColor="@color/orange" />
        </LinearLayout>
    </android.support.v7.widget.CardView>
</FrameLayout>

【问题讨论】:

  • 从 xml 中删除 android:scaleType="fitXY" 并按 (deviceheight * imagewidth)/devicewidth 等纵横比计算图像高度
  • 删除它会在 ImageView 中创建空白空间。

标签: android xml imageview


【解决方案1】:

在 xml 中将 imageView 的宽度设置为 match_parent 并在运行时计算图像的高度以使用以下代码保持纵横比:

Display display = getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int newWidth = size.x;

//Get actual width and height of image
int width = bitmap.getWidth();
int height = bitmap.getHeight();

// Calculate the ratio between height and width of Original Image
float ratio = (float) height / (float) width;
float scale = getApplicationContext().getResources().getDisplayMetrics().density;
int newHeight = (int) (width * ratio)/scale;

通过调用图像处理库的 .resize 函数传递此 newHeight 和 newWidth 来调整图像大小。

【讨论】:

    【解决方案2】:

    android:scaleType="fitXY" 更改为android:scaleType="centerInside"

    并根据屏幕宽度设置ImageView 宽度,这意味着您应该将ImageView 的宽度计算为屏幕宽度的60% 或类似的东西,使用android:scaleType="centerInside" 可以在不改变图像纵横比的情况下自行计算高度。

    【讨论】:

      【解决方案3】:

      Jsut put wrap_content 它会自动取一个大小。 android:layout_height="wrap_cont`在此处输入代码`ent" />

      【讨论】:

      • 请阅读完整的问题。他希望图像的宽度始终为 match_parent。
      【解决方案4】:

      听说过Picasso 图书馆吗?它不仅可以居中裁剪图像,还可以为您管理缓存的复杂性。

      如何设置?在 Gradle 中添加此依赖项并同步。

      compile 'com.squareup.picasso:picasso:2.5.2'
      

      如何使用库?很简单,你只需要调用一个单例代码。

      ImageView imageView  = ....
      Picasso.with(contextHere())
             .load("http://example.com/myimage.jpg)
             .fit()
             .centerCrop()
             .into(imageView);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多