在android listview中使用picasso加载自定义位图图像

时间:2016-09-07 05:39:22

标签: android listview adapter picasso

我需要在listview的每一行上创建和设置自定义位图图像。我是用毕加索做的。我在getView的适配器中的代码如下所示,

 @Override
public View getView(final int position, View convertView, ViewGroup parent) {


    ViewHolder viewHolder = null;
    View rowView = convertView;


    if (rowView == null){

        LayoutInflater inflater = context.getLayoutInflater();
        rowView = inflater.inflate(R.layout.horizontal_listview_list_sample, parent, false);

        viewHolder = new ViewHolder();

        viewHolder.imgDot = (ImageView) rowView.findViewById(R.id.rowIcon);
        viewHolder.rowIconBackround = (ImageView) rowView.findViewById(R.id.rowIconBackground);


        rowView.setTag(viewHolder);

    }else {

        viewHolder = (ViewHolder) rowView.getTag();

    }


    //Start Custom Image View///////

    String photoUrl1 = arrayList.get(position).getPhotoUrl();

    if (photoUrl1.length()<8){
        photoUrl1 = "SOME_URL";
    }

    final String photoUrl = photoUrl1;

    final ImageView imgDot = viewHolder.imgDot;

//        final viewHolder.imgDot = (ImageView) rowView.findViewById(R.id.rowIcon);


    final View finalRowView = rowView;
    final ViewHolder finalViewHolder = viewHolder;
    final Target target = new Target() {
        @Override
        public void onBitmapLoaded(Bitmap bitmap2, Picasso.LoadedFrom from) {

            Log.d("PICASO", " called: " + "onBitmapLoaded called");

            Bitmap original;


            original = new MyProfile().getResizedBitmap(bitmap2, (int) new MainActivity().dipToPixels(MainActivity.getInstance(), 63), (int) new MainActivity().dipToPixels(MainActivity.getInstance(), 63));

            BitmapDrawable bitmap = (BitmapDrawable) finalRowView.getResources().getDrawable(R.drawable.drawer_pro_pic_placeholder);
            int bitmapHeight= bitmap .getBitmap().getHeight();
            int bitmapWidth = bitmap .getBitmap().getWidth();


            Bitmap mask = BitmapFactory.decodeResource(finalRowView.getResources(), R.drawable.drawer_pro_pic_placeholder);
            Bitmap result = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas mCanvas = new Canvas(result);
            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));


            int width = mask.getWidth();
            int height = mask.getHeight();
            float centerX = (width  - original.getWidth()) * 0.5f;
            float centerY = (height- original.getHeight()) * 0.5f;


            mCanvas.drawBitmap(original, centerX, centerY, null);
            mCanvas.drawBitmap(mask, 0, 0, paint);
            paint.setXfermode(null);

            imgDot.getLayoutParams().height = bitmapHeight;
            imgDot.getLayoutParams().width = bitmapWidth;

            imgDot.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imgDot.setImageBitmap(result);

            imgDot.setAdjustViewBounds(true);


            Picasso.with(context).cancelRequest(finalViewHolder.imgDot);
            targets.remove(this);


        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {
            Log.d("PICASO", " called: " + "onBitmapFailed called");
            Picasso.with(context).cancelRequest(finalViewHolder.imgDot);
            targets.remove(this);

        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {
            Log.d("PICASO", "responseForRegistration called: " + "onPrepareLoad called");

        }
    };

    targets.add(target);

//        imgDot.setTag(target);
    Picasso.with(context).load(photoUrl).into(target);

//End Custom Image View////////////



    return rowView;
}

假设有两行,那么将创建两个自定义位图。当第一个创建时它显示不错,但是当第二个创建时,最后一个图像显示在两行的imageview中。 我需要在代码中更改什么内容?

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:3)

您可以通过创建处理程序作为适配器的子类并传递视图持有者和位置的特定引用来尝试某些操作,这将有助于我们避免覆盖目标侦听器的问题

  // construct the handler inside your adapter's constructor so you know it is on the UI thread
Handler myHandler = new Handler(); 

 class LoadImage implements Runnable {
    ImageView iv;
    int position;

public LoadImage(ImageView iv, int position) {
    this.iv = iv;
    this.position = position;
}

@Override
public void run() {

   // keep your target listener stuffs here
    Target target = new Target() {
    @Override
    public void onBitmapLoaded(Bitmap bitmap2, Picasso.LoadedFrom from) {

        Log.d("PICASO", " called: " + "onBitmapLoaded called");

        Bitmap original;


        original = new MyProfile().getResizedBitmap(bitmap2, (int) new MainActivity().dipToPixels(MainActivity.getInstance(), 63), (int) new MainActivity().dipToPixels(MainActivity.getInstance(), 63));

        BitmapDrawable bitmap = (BitmapDrawable) finalRowView.getResources().getDrawable(R.drawable.drawer_pro_pic_placeholder);
        int bitmapHeight= bitmap .getBitmap().getHeight();
        int bitmapWidth = bitmap .getBitmap().getWidth();


        Bitmap mask = BitmapFactory.decodeResource(finalRowView.getResources(), R.drawable.drawer_pro_pic_placeholder);
        Bitmap result = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas mCanvas = new Canvas(result);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));


        int width = mask.getWidth();
        int height = mask.getHeight();
        float centerX = (width  - original.getWidth()) * 0.5f;
        float centerY = (height- original.getHeight()) * 0.5f;


        mCanvas.drawBitmap(original, centerX, centerY, null);
        mCanvas.drawBitmap(mask, 0, 0, paint);
        paint.setXfermode(null);

        iv.getLayoutParams().height = bitmapHeight;
        iv.getLayoutParams().width = bitmapWidth;

        iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
        iv.setImageBitmap(result);

        iv.setAdjustViewBounds(true);

        Picasso.with(context).cancelRequest(iv);


    }

    @Override
    public void onBitmapFailed(Drawable errorDrawable) {
        Log.d("PICASO", " called: " + "onBitmapFailed called");
        Picasso.with(context).cancelRequest(iv);

    }

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {
        Log.d("PICASO", "responseForRegistration called: " + "onPrepareLoad called");

    }
};

    Picasso.with(context).load(photoList.get(position)).into(target);
}
}

您将通过以下方式从getView中调用它:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {


ViewHolder viewHolder = null;
View rowView = convertView;


if (rowView == null){

    LayoutInflater inflater = context.getLayoutInflater();
    rowView = inflater.inflate(R.layout.horizontal_listview_list_sample, parent, false);

    viewHolder = new ViewHolder();

    viewHolder.imgDot = (ImageView) rowView.findViewById(R.id.rowIcon);
    viewHolder.rowIconBackround = (ImageView) rowView.findViewById(R.id.rowIconBackground);

    rowView.setTag(viewHolder);

  //Change is here
  viewHolder.mRunnable = new LoadImage(viewHolder.imgDot, position);

}else {

    viewHolder = (ViewHolder) rowView.getTag();

   //Change is here
   myHandler.removeCallbacks(viewHolder.mRunnable);

}

//Start Custom Image View///////

String photoUrl1 = arrayList.get(position).getPhotoUrl();

if (photoUrl1.length()<8){
    photoUrl1 = "SOME_URL";
}

final String photoUrl = photoUrl1;

final ImageView imgDot = viewHolder.imgDot;

  //        final viewHolder.imgDot = (ImageView) rowView.findViewById(R.id.rowIcon);

 final View finalRowView = rowView;
 final ViewHolder finalViewHolder = viewHolder;

 //Change is here
 myHandler.postDelayed(viewHolder.mRunnable, 3000);

 return rowView;
}