Picasso和PhotoView Library将图像加载到ImageView中

时间:2014-02-13 09:00:42

标签: android image picasso

我正在使用Picasso库将图像加载到ImageView中,然后使用PhotoView库向ImageView添加缩放和平移等。

但是当毕加索第一次加载图像时,它会显示如下图像:

enter image description here

但是,一旦我触摸它正确放置的图像

enter image description here

但如果我关闭我的应用程序,它突然不再显示图像而且不会。

我的主要活动: http://pastebin.com/5H4zAgH

我正在使用的库:

2 个答案:

答案 0 :(得分:26)

在使用Picasso和Photoview时,我对错位的图像也有同样的问题。

要解决这个问题,我所做的就是在使用into(view, callback)而不是into(view)加载Picasso图像时使用回调。成功加载图像后,我实例化PhotoViewAttacher对象或调用方法update()

这里有一个代码示例:

Callback imageLoadedCallback = new Callback() {

    @Override
    public void onSuccess() {
        if(mAttacher!=null){
            mAttacher.update();
        }else{
            mAttacher = new PhotoViewAttacher(mImageView);
        }
    }

    @Override
    public void onError() {
        // TODO Auto-generated method stub

    }
};

Picasso.with(mContext)
.load(mImageUrl)
.into(mImageView,imageLoadedCallback);

我希望这会有所帮助。问候。

答案 1 :(得分:19)

我也有同样的问题。我使用PhotoView代替ImageView解决了问题,并从我的代码中删除了PhotoViewAttacher

在布局文件中(如果使用布局):

<uk.co.senab.photoview.PhotoView
    android:id="@+id/your_photo_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    ... />

在您的代码中:

PhotoView photoView = (PhotoView) findViewById(R.id.your_photo_view);
Picasso.with(context)
        .load(file)
        ...
        .into(photoView);

现在一切都必须正确(至少对我来说是这样!);