【问题标题】:My layout is crashed after Picasso loads imagesPicasso 加载图像后我的布局崩溃了
【发布时间】:2015-03-17 06:17:05
【问题描述】:

我正在尝试构建一个自定义的图像布局,如下所示,其中 4 个A 表示一个ImageView

AABC
AADE

当我尝试使用默认 src 属性绘制布局时,或者当我在 Picasso 上放置 placeholder 选项时,布局渲染没有问题。然而,当毕加索逐渐延迟加载每张图片时,布局就这样崩溃了。 (A下面的空格是空格。)

ABC
 DE

如何使用 Picasso 的延迟加载来保持我的原始布局? CustomSquareImageView 是一个自定义类,它扩展了 ImageView 以绘制方形 ImageView。

PartOfLayout.xml

<LinearLayout
    android:id="@+id/set_profile_profile_photos_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal">

        <CustomSquareImageView
            android:id="@+id/set_profile_profile_photos_first"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="centerCrop"
            android:src="@drawable/profile" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <CustomSquareImageView
                android:id="@+id/set_profile_profile_photos_second"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:scaleType="centerCrop"
                android:src="@drawable/profile" />

            <CustomSquareImageView
                android:id="@+id/set_profile_profile_photos_third"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:scaleType="centerCrop"
                android:src="@drawable/profile" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <CustomSquareImageView
                android:id="@+id/set_profile_profile_photos_fourth"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:scaleType="centerCrop"
                android:src="@drawable/profile" />

            <CustomSquareImageView
                android:id="@+id/set_profile_profile_photos_fifth"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:scaleType="centerCrop"
                android:src="@drawable/profile" />

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

PartOfFragment.java

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    try {
        JSONArray profilePhotos = new JSONArray(mProfile.getProfileImages());
        if (!profilePhotos.get(0).toString().isEmpty()) {
            Picasso.with(getActivity()).load(profilePhotos.get(0).toString()).
                    placeholder(R.drawable.profile).into(mProfilePhotoFirst);
        }
    } catch (JSONException e) {
         e.printStackTrace();
    }
}

更新

作者here使用.fit().centerCrop()placeholder的回答有效地解决了“到底”的问题,但是在毕加索加载图片的过程中,由于每张图片的不同,布局暂时崩溃尺寸。 (成功加载所有图像后,布局看起来不错。)

如何加载图像而不会使中间的布局崩溃?我希望加载的图像不会干扰布局,而是直接插入到具有centerCrop 状态的布局中。

【问题讨论】:

  • 尝试使用 resize() 并修复图像大小...
  • @NAVdroid 这就是工作,但我以不同的方式解决了(实际上解决了我的错误)问题。

标签: android android-layout imageview picasso


【解决方案1】:

因为我使用LinearLayouts 作为图像框架,所以我所要做的就是将LinearLayouts 的layout_width 设置为match_parent,而不是Android Studio 建议的wrap_content0dp

我的新 XML 如下所示。

NewPartOfLayout.xml

<LinearLayout
    android:id="@+id/set_profile_profile_photos_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal">

        <CustomSquareImageView
            android:id="@+id/set_profile_profile_photos_first"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="centerCrop"
            android:src="@drawable/profile" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <CustomSquareImageView
                android:id="@+id/set_profile_profile_photos_second"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:scaleType="centerCrop"
                android:src="@drawable/profile" />

            <CustomSquareImageView
                android:id="@+id/set_profile_profile_photos_third"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:scaleType="centerCrop"
                android:src="@drawable/profile" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <CustomSquareImageView
                android:id="@+id/set_profile_profile_photos_fourth"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:scaleType="centerCrop"
                android:src="@drawable/profile" />

            <CustomSquareImageView
                android:id="@+id/set_profile_profile_photos_fifth"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:scaleType="centerCrop"
                android:src="@drawable/profile" />

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

【讨论】:

    猜你喜欢
    • 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
    相关资源
    最近更新 更多