毕加索与适配器

时间:2015-10-08 10:01:09

标签: android

我想显示progressBar,用于在ListView的Adapter中下载Image。当下载图像时,progressBar不会消失,图像也不会显示。当我向上和向下滚动它完美地工作。而且我不知道该怎么做。对不起我的英语不好。为了更好地理解,我放了截图。

以下是适配器的方法:

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

        ViewHolder viewHolder =null;


        if(convertView==null){

            viewHolder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.programs_item, null);
            viewHolder.programName = (TextView) convertView.findViewById(R.id.programName);
            viewHolder.programDuration = (TextView) convertView.findViewById(R.id.programDuration);
            viewHolder.programImg = (ImageView) convertView.findViewById(R.id.programImg);
            viewHolder.programImgLoading = (ProgressBar) convertView.findViewById(R.id.programImgLoading);
            convertView.setTag(viewHolder);

        }
        else
        {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        String name = getItem(position).getName();
        Log.d("NAMELOG", "" + getItem(position).getName());
        long duration1 = (getItem(position).getDuration());
        long duration = TimeUnit.SECONDS.toMinutes(duration1);
        Log.d("TIMEEEE",""+duration+" "+duration1);
        String duration2 = String.valueOf(duration)+ " min";
        viewHolder.programName.setText(name);
        viewHolder.programDuration.setText(duration2);

        File dir = new File(FILE_PATH);

        File imgFile = new File(dir, getItemId(position) + ".png");

        if(imgFile.exists()){

            Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

            viewHolder.setImg(myBitmap);


        }
        else if (isNetworkConnected()) {

            Log.d("MYLOGGGGGGGG", MYurl.BASE_URL + "/" + String.valueOf(getItem(position).getImgLarge()) + "");
            final ViewHolder finalViewHolder = viewHolder;
            Picasso.with(mContext)
                    .load(MYurl.BASE_URL + "/" + getItem(position).getImgMedium())
                    .into(new Target() {
                        @Override
                        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {

                            finalViewHolder.programImgLoading.setVisibility(View.INVISIBLE);
                            finalViewHolder.setImg(bitmap);


                            File dir = new File(FILE_PATH);
                            if (!dir.exists())
                                dir.mkdirs();
                            File file = new File(dir, getItemId(position) + ".png");
                            FileOutputStream fOut = null;
                            try {
                                Log.d("MYLOGSFILE", FILE_PATH + " " + file.getName());
                                fOut = new FileOutputStream(file);
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            }

                            bitmap.compress(Bitmap.CompressFormat.PNG, 80, fOut);
                            try {
                                fOut.flush();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            try {
                                fOut.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }



                        }


                        @Override
                        public void onBitmapFailed(Drawable errorDrawable) {
                            finalViewHolder.programImg.setImageResource(R.drawable.noimage);
                        }

                        @Override
                        public void onPrepareLoad(Drawable placeHolderDrawable) {
                            finalViewHolder.programImg.setImageBitmap(null);
                            finalViewHolder.programImgLoading.setVisibility(View.VISIBLE);
                        }
                    });

        }
        else{

            viewHolder.programImg.setImageResource(R.drawable.noimage);




        }


        return convertView;
    } 

before scrolling

1 个答案:

答案 0 :(得分:0)

对不起,你想在你的adpter做什么?

    if(imgFile.exists()){

        Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

        viewHolder.setImg(myBitmap);


    } Your decodnig image in UI thread. its bad pratis.

为什么你不使用毕加索?

接下来,您将使用Picasso<但没有设定它。你知道在设备上保存文件吗?

如果你只是显示图像,请使用picasso:

calback=new Callback() {
                    @Override
                    public void onSuccess() {
                        if (progress != null)
                            progress.setVisibility(View.GONE);
                    }

                    @Override
                    public void onError() {

                        if (progress != null)
                            progress.setVisibility(View.GONE);
                    }
                });
   if(imgFile.exists()){

        Picasso.with(mContext)
                .load(imgFile)
                .into(viewHolder.programImg,calback);

    }
    else if (isNetworkConnected()) {
    Picasso.with(mContext)
            .load(MYurl.BASE_URL + "/" + getItem(position).getImgMedium())
            .into(viewHolder.programImg,calback);

或者如果您保存图像将图像加载到目标中并将其设置为imageView:

 Picasso.with(mContext)
                .load(imgFile)
                .into(tagget,calback);

Target target= new Target() {
        @Override
        public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
            Runnable runnable= new Runnable() {
                @Override
                public void run() {
                    OutputStream imageOut = new FileOutputStream(new File("file"));
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 50, imageOut);   
                }
            };

            myThreadExecutor.execut(runnable);
        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {

        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {

        }
    }

如果您愿意,可以使用AsycTask。但是不要在UI terad中保存图像。也可以通过intentService和BroadcastReceiver提供下载图像