0

我想在我的图像切换器中有效地加载大型位图,为此我一直在使用毕加索,但现在我被困在这一点上。如果没有 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();
        }
4

1 回答 1

4

一种可能的方法是使用 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 的引用

于 2014-07-23T20:55:08.763 回答