加载不同高度或宽度的毕加索图像时出错

时间:2016-10-13 11:18:10

标签: android picasso

这是我的PhotosAdapter类,用于加载高度或宽度变化的图像。

import com.fivehundredpx.greedolayout.GreedoLayoutSizeCalculator.SizeCalculatorDelegate;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;

 import java.util.ArrayList;

/**
 * Created by Julian Villella on 16-02-24.
 */

public class PhotosAdapter extends RecyclerView.Adapter<PhotosAdapter.PhotoViewHolder> implements SizeCalculatorDelegate {


    private static final int IMAGE_COUNT = 500;

    // private final double[] mImageAspectRatios = new double[Constants.IMAGES.length];

    ArrayList<String> source_title = new ArrayList<String>();

    ArrayList<String> mImageResIds = new ArrayList<String>();


    private Context mContext;

//    @Override
//    public double aspectRatioForIndex(int index) {
//        // Precaution, have better handling for this in greedo-layout
//        if (index >= getItemCount()) return 1.0;
//        return mImageAspectRatios[getLoopedIndex(index)];
//    }

    @Override
    public double aspectRatioForIndex(int index) {
        // Precaution, have better handling for this in greedo-layout
        if (index >= getItemCount()) return 1.0;
        return mImageResIds.size();
    }


    public class PhotoViewHolder extends RecyclerView.ViewHolder {
        private ImageView mImageView;

        public PhotoViewHolder(ImageView imageView) {
            super(imageView);
            mImageView = imageView;
            Log.d("PhotoViewHolderimage", imageView + "");

        }
    }

    public PhotosAdapter(Context context, ArrayList<String> source_title, ArrayList<String> source_filepath) {
        mContext = context;

        this.mImageResIds = source_filepath;
        this.source_title = source_title;

        Log.d("start", "start" + mImageResIds);

//        calculateImageAspectRatios();

    }

    @Override
    public PhotoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        ImageView imageView = new ImageView(mContext);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

        imageView.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
        ));

        Log.d("PhotoViewHoldermake", imageView + "");

        return new PhotoViewHolder(imageView);
    }

    @Override
    public void onBindViewHolder(final PhotoViewHolder holder, int position) {

//        Picasso.with(mContext)
//                .load(mImageResIds.get(position))
//                .into(holder.mImageView);
        Log.d("onBindViewHolder", holder + "");

        Log.d("mImageResIds", mImageResIds.get(position));

        Picasso.with(mContext).load(mImageResIds.get(position)).into(new Target() {
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                int width = bitmap.getWidth();
                int height = bitmap.getHeight();
                holder.mImageView.setImageBitmap(bitmap);


            }

            @Override
            public void onBitmapFailed(Drawable errorDrawable) {

                Log.d("Imageloadingfailed", " " + errorDrawable);

            }

            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {


                Log.d("Imageloading", "placeHolderDrawable " + placeHolderDrawable);

            }
        });


    }

    @Override
    public int getItemCount() {
        return mImageResIds.size();
    }

//    private void calculateImageAspectRatios() {
//        BitmapFactory.Options options = new BitmapFactory.Options();
//        options.inJustDecodeBounds = true;
//
//        for (int i = 0; i < mImageResIds.size(); i++) {
//            BitmapFactory.decodeResource(mContext.getResources(), mImageResIds.indexOf(i), options);
//            mImageAspectRatios[i] = options.outWidth / (double) options.outHeight;
//        }
//    }

    // Index gets wrapped around <code>Constants.IMAGES.length</code> so we can loop content.
//    private int getLoopedIndex(int index) {
//        return index % Constants.IMAGES.length; // wrap around
//    }
}

当这个类运行时,数据存在但没有显示图像,onBindViewHolder方法运行并且存在问题,由于此问题,picasso Override方法onPrepareLoad并显示placeHolderDrawable null异常

我使用此代码作为参考https://github.com/500px/greedo-layout-for-android

如果任何人对此问题有任何解决方案或任何建议,请帮助我解决此问题。 对不起我的英语,并提前致谢。

0 个答案:

没有答案