【问题标题】:Picasso with new Target loads bitmap from second time带有新目标的毕加索第二次加载位图
【发布时间】:2015-04-29 11:34:43
【问题描述】:

下面是我的代码。在第一次导航到屏幕期间 onPrepareLoad() 被调用。但它不会出现在 onBitmapLoaded() 或 onBitmapFailed() 中。有没有人能轻点。它总是只从缓存加载吗?如何处理。我只使用默认的缓存机制。不涉及外部存储器。就好像我在同一屏幕/片段上第二次导航一样,位图通过 onBitmapLoaded() 加载

    picasso.load(url)
            .noFade().into(new Target() {

        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                relativeLayout.setBackgroundDrawable(new BitmapDrawable(
                        bitmap));
                relativeLayout.invalidate();
            } else {

                relativeLayout.setBackground(new BitmapDrawable(
                        bitmap));
                relativeLayout.invalidate();
            }
            if (true)
                Log.d("onBitmap", "onBitmapLoaded");
            //
        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {
            //
        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {
            //
        }
    });

【问题讨论】:

    标签: android android-volley retrofit picasso


    【解决方案1】:

    或者,我通过在布局内放置一个 ImageView 来实现要求,并传递我的布局宽度和高度来缩放我的 ImageView。原因是我的 ImageView 的 getMeasuredWidth 始终为 0,因为我的布局给了我测量宽度和测量高度(ImageView 的父级)

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:id="@+id/c_details"
                android:layout_weight="1"
                android:layout_marginBottom="@dimen/spacing_small">
    
            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/c_details_view"
                android:layout_alignParentTop="true"
                android:layout_weight="1"
                android:layout_alignParentLeft="true"
                android:layout_marginBottom="@dimen/spacing_small">
                </ImageView>
    
               <!--   Other Views goes here      -->
    
            </RelativeLayout>
    

                In my Fragement java file
                RelativeLayout cDetailsLayout = (RelativeLayout)                                                 rootView.findViewById(R.id.c_details);
    
                // passing following into Picasso call
                cDetailsLayout.getMeasuredWidth(),                 cDetailsLayout.getMeasuredHeight()
    

    【讨论】:

      【解决方案2】:

      您没有在 Target 中收到回调的原因是因为它正在被垃圾收集。毕加索对它的引用很弱。在文档中对 into() 方法进行了说明:“此方法保持对目标的弱引用......”。所以你必须将 Target 自己存储在一个字段中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-01
        • 2016-07-07
        • 2017-04-23
        • 2014-09-26
        相关资源
        最近更新 更多