android get Drawable image after picasso loaded

我怕爱的太早我们不能终老 提交于 2020-06-24 08:19:20

问题


I am using Picasso library to load image from url. The code I used is below.

Picasso.with(getContext()).load(url).placeholder(R.drawable.placeholder)
                .error(R.drawable.placeholder).into(imageView);

What I wanna do is to get the image that loaded from url. I used

Drawable image = imageView.getDrawable();

However, this will always return placeholder image instead of the image load from url. Do you guys have any idea? How should I access the drawable image that it's just loaded from url.

Thanks in advance.


回答1:


This is because the image is loading asynchronously. You need to get the drawable when it is finished loading into the view:

   Target target = new Target() {
          @Override
          public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
              imageView.setImageBitmap(bitmap);
              Drawable image = imageView.getDrawable();
          }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {}

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

   Picasso.with(this).load("url").into(target);



回答2:


        mImageView.post(new Runnable() {
            @Override
            public void run() {
                mPicasso = Picasso.with(mImageView.getContext());
                mPicasso.load(IMAGE_URL)
                        .resize(mImageView.getWidth(), mImageView.getHeight())
                        .centerCrop()
                        .into(mImageView, new com.squareup.picasso.Callback() {
                            @Override
                            public void onSuccess() {
                                Drawable drawable = mImageView.getDrawable();
                                // ...
                            }

                            @Override
                            public void onError() {
                                // ...
                            }
                        });
            }
        });


来源:https://stackoverflow.com/questions/25799967/android-get-drawable-image-after-picasso-loaded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!