在毕加索加载图片时如何使用进度条?

时间:2014-07-22 16:27:11

标签: android-progressbar picasso

我希望onStart()方法使用毕加索从服务器加载图片,我想显示进度条直到照片完全下载 这是我的代码:

@Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();

        Picasso.with(context).load(imageLoad)
                .placeholder(R.id.progressBarDetails)
                .error(R.drawable.friend_request).noFade().resize(200, 200)
                .into(avatarImage, new Callback() {
                    @Override
                    public void onError() {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void onSuccess() {
                        // TODO Auto-generated method stub
                        progressbar.setVisibility(View.GONE);
                    }

                });

        Picasso.with(this).load(imageLoad).into(target);

    }

    OnFinished a = new OnFinished() {

        @Override
        public void onSendFinished(IntentSender IntentSender, Intent intent,
                int resultCode, String resultData, Bundle resultExtras) {
            // TODO Auto-generated method stub
            intent = new Intent(getApplicationContext(), Map.class);
        }
    };

    private Target target = new Target() {
        @Override
        public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    File file = new File(Environment
                            .getExternalStorageDirectory().getPath()
                            + "/actress_wallpaper.jpg");
                    try {
                        file.createNewFile();
                        FileOutputStream ostream = new FileOutputStream(file);
                        bitmap.compress(CompressFormat.JPEG, 75, ostream);
                        ostream.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            }).start();
        }

3 个答案:

答案 0 :(得分:18)

我还没有测试过你的代码,但即使这样可行,文件actress_wallpaper.jpg也没有加载到ImageView中。在文档中,它说

  

实现此类的对象必须具有Object.equals(Object)Object.hashCode()的有效实现,才能在内部进行适当的存储。

试试这个:

File file = new File(pathToFile);

Picasso.with(context)
       .load(file)
       .into(imageView, new Callback() {
           @Override
           public void onSuccess() {
               progressbar.setVisibility(View.GONE);
           }
       });

警告我没有测试我的代码。

<强>更新

我已经尝试过版本2.3.2和2.3.3,好像有问题https://github.com/square/picasso/issues/539

答案 1 :(得分:2)

这是一个老问题,但可能这个答案可以帮助其他人,因为我在从服务器加载图像时显示进度条时也遇到了问题。

我使用的是Picasso 2.4.0。我正在使用Picasso Target界面在imageview中加载图像。以下是经过测试和运行的代码:

首先添加以下行:

ImageView ivPhoto = (ImageView) findViewById(R.id.iv_photo);
ProgressBar pbLoadingBar = (ProgressBar) findViewById(R.id.pb_loading_bar);

//get image url
String imageUrl = getImageUrl();

//ImageViewTarget is the implementation of Target interface.
//code for this ImageViewTarget is in the end
Target target = new ImageViewTarget(ivPhoto, pbLoadingBar);
Picasso.with(mContext)
            .load(imageUrl)
            .placeholder(R.drawable.place_holder)
            .error(R.drawable.error_drawable)
            .into(target);

以下是

上面使用的Target接口的实现
private static class ImageViewTarget implements Target {

    private WeakReference<ImageView> mImageViewReference;
    private WeakReference<ProgressBar> mProgressBarReference;

    public ImageViewTarget(ImageView imageView, ProgressBar progressBar) {
        this.mImageViewReference = new WeakReference<>(imageView);
        this.mProgressBarReference = new WeakReference<>(progressBar);
    }

    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {

        //you can use this bitmap to load image in image view or save it in image file like the one in the above question.
        ImageView imageView = mImageViewReference.get();
        if (imageView != null) {
            imageView.setImageBitmap(bitmap);
        }

        ProgressBar progressBar = mProgressBarReference.get();
        if (progressBar != null) {
            progressBar.setVisibility(View.GONE);
        }
    }

    @Override
    public void onBitmapFailed(Drawable errorDrawable) {
        ImageView imageView = mImageViewReference.get();
        if (imageView != null) {
            imageView.setImageDrawable(errorDrawable);
        }

        ProgressBar progressBar = mProgressBarReference.get();
        if (progressBar != null) {
            progressBar.setVisibility(View.GONE);
        }
    }

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {
        ImageView imageView = mImageViewReference.get();
        if (imageView != null) {
            imageView.setImageDrawable(placeHolderDrawable);
        }

        ProgressBar progressBar = mProgressBarReference.get();
        if (progressBar != null) {
            progressBar.setVisibility(View.VISIBLE);
        }
    }
}

如果用于在活动中加载图像,上面的代码可以正常工作。但是如果你想在gridview / recyclerview中加载图像或者在使用相同视图持有者的情况下查看寻呼机等,你可能会得到an issue,其中没有调用onBitmapLoaded()(因为视图被回收而毕加索只保持弱势)对Target对象的引用)。这是解决此问题的a link

答案 2 :(得分:0)

更改为此

function doRound (ctx, game, loc, move){ // move is selected human move
    let go = move;
    let translate = {0:-1,1:-8,2:1,3:8}
    for (var i = 0; i < 2; i++) {
        loc = loc+translate[go]; // calculate move
        if (illegal(loc)){
            let text = (i === 0) ? "You lose!" : "You win!";
            document.getElementById("result").innerHTML = text;
            break;  // game over
        }       
        game[loc] = true; // place move on board
        board(ctx);  // refresh canvas
        if (i === 0){go = compMove()};  // calc computer move
    }
}