【问题标题】:Using Picasso library with ImageSwitcher is causing Object out of Memory errors将 Picasso 库与 ImageSwitcher 一起使用会导致 Object out of Memory 错误
【发布时间】:2014-06-12 17:37:32
【问题描述】:

我想在我的图像切换器中有效地加载大型位图,为此我一直在使用毕加索,但现在我被困在这一点上。如果没有 Picasso 很多 OOMs 和其他讨厌的异常,请告诉我是否也可以将这个库与 Image Switcher 一起使用。 如果是,请提供示例代码。

谢谢!

imswitch.setFactory(new ViewFactory() {

        @Override
        public View makeView() {
              ImageView imageView = new ImageView(getApplicationContext());
              imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
              imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
              return imageView;
        }
    } );

和点击:

     btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
               currentIndex++;
               // If index reaches maximum reset it
                if(currentIndex==messageCount)
                    currentIndex=0;
                ImageView imageView = (ImageView)findViewById(R.id.imswitch);
                Picasso.with(getApplicationContext()).load(imageIds[currentIndex]).into(imageView);
                Toast.makeText(getApplicationContext(), "Pressed "+currentIndex,Toast.LENGTH_LONG).show();
        }

【问题讨论】:

  • 你有什么问题?
  • 我有一堆大的位图,我想在图像切换器上显示它们,想知道毕加索在这里是否有帮助。
  • 您的图片是否来自互联网?如果是真的,那么你可以使用 picasso 来获取这些图像,
  • 不,我已将图像存储在可绘制目录中,我想在图像切换器中显示它们。我知道如何使用 Picasso 在 ImageView 上设置图像,但我不知道对 Image Switcher 做同样的事情

标签: android picasso imageswitcher


【解决方案1】:

一种可能的方法是使用 Target 接口创建自己的实现。

https://square.github.io/picasso/javadoc/com/squareup/picasso/Target.html

public class ImageSwitcherPicasso implements Target {

        private ImageSwitcher mImageSwitcher;
        private Context mContext;

        public ImageSwitcherPicasso(Context context, ImageSwitcher imageSwitcher){
            mImageSwitcher = imageSwitcher;
            mContext = context;
        }

        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) {
            mImageSwitcher.setImageDrawable(new BitmapDrawable(mContext.getResources(), bitmap));
        }

        @Override
        public void onBitmapFailed(Drawable drawable) {

        }

        @Override
        public void onPrepareLoad(Drawable drawable) {

        }

    }

不仅仅是如下使用

ImageSwitcherPicasso mImageSwitcherPicasso = new ImageSwitcherPicasso(getActivity(), playerBackground); Picasso.with(getActivity()).load(new File(path)).into(mImageSwitcherPicasso);

其中 playerBackground 是对 ImageSwitcher 的引用

【讨论】:

  • 感谢回复..反正问题已经解决了。
猜你喜欢
  • 1970-01-01
  • 2018-08-06
  • 1970-01-01
  • 1970-01-01
  • 2016-12-05
  • 2011-06-30
  • 2011-03-03
  • 2018-08-01
  • 1970-01-01
相关资源
最近更新 更多