iielse / imageviewer

A simple and customizable Android full-screen image viewer 一个简单且可自定义的Android全屏图像浏览器
MIT License
2.23k stars 310 forks source link

P02,”唐僧还是厉害啊,为什么。。“那条下的图片,有bug #7

Closed LeonXtp closed 6 years ago

LeonXtp commented 7 years ago

没有按照预期的方式显示,点击之后仅仅按列表里的大小显示,并且位置水平靠左垂直居中的。 效果图如下: http://upload-images.jianshu.io/upload_images/2018741-f22add5f1676a879.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240

iielse commented 7 years ago

http://img.my.csdn.net/uploads/201707/17/1500281250_7285.gif 兄弟 帮个忙 希望描述详细一点啊,我这里就没翻车。 你是跑的我的demo吗?

iielse commented 7 years ago

@LeonXtp 发个demo工程给我调吧,感谢啊

LeonXtp commented 7 years ago

@iielse 我是Nexus6P 7.1.2系统直接下载apk先体验的,然后出现了上面的问题。然后我拉代码跑工程还是一样,但是在小米Mi Max 7.0系统中运行跑起来又是正常的

g19980115 commented 6 years ago

@iielse e神,一加五也翻车了,运行demo出现一样的问题http://upload-images.jianshu.io/upload_images/2018741-f22add5f1676a879.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240

qq171161636 commented 6 years ago

我也遇到这个问题,刚好也是一加5,图片靠左垂直居中了,而没有整个垂直居中,希望帮忙解决下

dstweihao commented 6 years ago

我也遇到了这个问题。

hu670014125 commented 6 years ago

这个问题我也遇到过,是由于首次创建的时候mWidth和mHeight没有初始值的原因,设置初始值就可以了

iielse commented 6 years ago

今天,我自己也遇到这个问题了。 原因就是 @hu670014125 所说的, 首次创建的时候mWidth和mHeight没有初始值。

如果显示的时候 动态创建imageWatcher
这样调用的API->ImageWatcher.Helper.with(this@BaseActivity).create() 这个时候直接show,mWidth,mHeight就都是0,这样查看大图的第一个平移效果计算的终点坐标就是错误的。

这里有几种方法: 第一种就是 imageWatcher的初始化 在 xml 布局里面写好 ,这样 调用的时候 mWidth, mHeight都会有值。 第二种就是 业务逻辑这里展示大图一般都是全屏展示的。直接 把 mWidth 赋值屏幕宽度,mHeight 赋值屏幕高度就可以。

第三种就是 在show的时候,假如宽高是0,把入参参数临时保存为成员变量,等 onSizeChanged被调用拿到宽高后, 再内部重新调用 show方法就可以了。

 private boolean isInitLayout = false;
 private ImageView initI;
 private SparseArray<ImageView> initImageGroupList;
 private List<Uri> initUrlList;
/**
     * @param i              被点击的ImageView
     * @param imageGroupList
     * @param urlList        被加载的图片url列表,数量必须大于等于 imageGroupList.size。 且顺序应当和imageGroupList保持一致
     */
    public void show(ImageView i, SparseArray<ImageView> imageGroupList, final List<Uri> urlList) {
        if (i == null || imageGroupList == null || urlList == null || imageGroupList.size() < 1 ||
                urlList.size() < imageGroupList.size()) {
            String info = "i[" + i + "]";
            info += "#imageGroupList " + (imageGroupList == null ? "null" : "size : " + imageGroupList.size());
            info += "#urlList " + (urlList == null ? "null" : "size :" + urlList.size());
            throw new IllegalArgumentException("error params \n" + info);
        }

        if (!isInitLayout) {
            initI = i;   //临时存储用户设置入参,onSizeChanged的时候使用
            initImageGroupList = imageGroupList; 
            initUrlList = urlList;
            return;
        }
}

 @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
        maxTranslateX = mWidth / 2;
        maxTranslateY = mHeight / 2;

        if (!isInitLayout) {
            isInitLayout = true;   // 拿到了正确的宽高,重新继续 show的逻辑
            new Handler().post(new Runnable() {
                @Override
                public void run() {
                    show(initI, initImageGroupList, initUrlList);
                }
            });
        }
    }