【问题标题】:Picasso doesn't load drawable image into ImageView毕加索不会将可绘制图像加载到 ImageView
【发布时间】:2019-07-15 07:47:44
【问题描述】:

Picasso 无法将图像drawable 加载到ImageView,我尝试更改ImageView 的宽度和高度、调整图像大小、加载或不加载fit()resize()center() 并且图像仍然不可见。我在整个项目中使用Picasso,并且到处都可以正常工作,但是在这个项目中没有结果。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/mainFullFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/mainThemeBlack"
    tools:context=".MainTabsActivity">



    <RelativeLayout
        android:id="@+id/rlSynchroView"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_alignParentBottom="true"
        android:background="@color/bottom_sheet_blue">
        <TextView
            android:id="@+id/tvSynchroLast"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="end"
            android:layout_marginEnd="10dp"
            android:textColor="@color/mdtp_white"
            android:text="Last synchro: 10:38"/>



        <ImageView
            android:id="@+id/ivSynchroIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tvSynchroLast"
            android:elevation="5dp"/>
    </RelativeLayout>


    <android.support.v7.widget.RecyclerView
        android:id="@+id/listFullView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:dividerHeight="10.0sp"
        android:layout_above="@id/rlSynchroView"
        android:background="?android:attr/selectableItemBackground"
        android:divider="@color/mainThemeBlack"
        android:paddingBottom="70dp"
        android:clipToPadding="false">

    </android.support.v7.widget.RecyclerView>
</RelativeLayout>

方法调用:

Picasso.get().load(R.drawable.refresh_icon)
                .resize(10, 10)
                .into(refreshIV);

我在 onViewCreated() 内部调用它,我也尝试用不同的值调整它的大小。

我想实现带有刷新图标的可点击底部栏(以及有关上次同步时间的信息),以与onClick方法内的服务同步数据

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        mView = inflater.inflate(R.layout.list_full, container, false);

        refreshIV = mView.findViewById(R.id.ivSynchroIcon);

        mobileArray = new ArrayList<IssuePOJO>(); 
        dbStats.open();

        generateList();

        mView = view;
        return mView;

    }

【问题讨论】:

  • refreshIV 在哪里?
  • 我已经编辑了我的问题,mView 对于这个 Fragment 是全局的,所以我可以在任何地方将它用于 findViewById()

标签: android android-layout picasso


【解决方案1】:

您将其加载到错误的 Imageview 中。像这样更改您的代码

Picasso.get().load(R.drawable.refresh_icon).into(ivSynchroIcon);

如果你想看看哪里出了问题,只需像这样添加 PIcasso 回调

 Picasso.get()
            .load(R.drawable.refresh_icon)
            .into(ivSynchroIcon, new Callback() {
                @Override
                public void onSuccess() {
                }

                @Override
                public void onError(Exception e) {
                }
            })

【讨论】:

  • 我已编辑,refreshIV = mView.findViewById(R.id.ivSynchroIcon);
  • @SkypeDogg 。查看更新的答案。现在打印你的异常和成功。看看会发生什么
  • 我已将其设置为在成功时打印“成功”,在错误时打印 e.getMessage(),这就是我所得到的:02-21 13:12:27.677 32724-32724/com.example.damianadamski.Service D/PICASSO: success。看起来它工作正常,但仍然没有图像已加载
  • 布局背景为蓝色,图标为白色透明背景。不知道有什么问题。
  • @SkypeDogg 尝试使用不同的图像进行测试
【解决方案2】:

Picasso 语法看起来正确,但看起来 onCreateView 方法逻辑不正确

请尝试以下代码:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    mView = inflater.inflate(R.layout.list_full, container, false);

    refreshIV = mView.findViewById(R.id.ivSynchroIcon);

    mobileArray = new ArrayList<IssuePOJO>(); 
    dbStats.open();

    generateList();

    //mView = view;//please comment this one and try
    return mView;

}

【讨论】:

  • @SkypeDogg 请检查我的回答
  • 不客气,感谢您接受答案,乐于助人:)
【解决方案3】:

由于你的资源中已经有了drawable (refresh_icon),你可以直接把它放在布局中。

<ImageView
    android:id="@+id/ivSynchroIcon"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:padding="10dp"
    android:layout_below="@+id/tvSynchroLast"
    android:src="@drawable/refresh_icon"
    android:elevation="5dp"/>

如果您仍然想使用 Picasso,请尝试检查 Layout Inspector 中的 ivSynchroIcon 元素。 因此,您可以看到 imageview 的放置位置以及测量值。 在 Android Studio 中Tools -&gt; Layout Inspector

【讨论】:

  • refresh_icon 是 128x128,所以我不确定它是否会从 XML 调整大小
  • 您的 rlSynchroView 布局的高度是 70dp。所以只要给宽度=70dp,高度=70dp。如果你愿意,你可以通过添加填充来缩小图标。我编辑了我的答案。
【解决方案4】:

试试这条线

Picasso.with(context).load(url).resize(10, 10).into(refreshIV);

【讨论】:

    【解决方案5】:

    毕加索遇到了这个问题,你应该尝试使用占位符来绘制

    Picasso.get().load(R.drawable.refresh_icon)
                .resize(10, 10)
                .placeholder(R.drawable.refresh_icon)
                .into(refreshIV);
    

    【讨论】:

    • 为什么要使用占位符?
    猜你喜欢
    • 1970-01-01
    • 2021-08-09
    • 2020-05-02
    • 2016-03-12
    • 2017-05-09
    • 2014-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多