2019-04-04 SubsamplingScaleImageView初步使用(仅限使用)

1.修改SubsamplingScaleImageView长图展示位置:

①新增方法
/**添加的代码,在{@link #checkReady()}中调用*/
  private void setNewDefaultScale(){
    int w1 = getWidth();
    int h1 = getHeight();
    int w2 = sWidth;
    int h2 = sHeight;
    //保证控件大小和显示的图片大小都大于0
    if (w1 > 0 && h1 > 0 && w2 > 0 && h2 > 0) {
      //获取控件宽高比
      final float ratioView = w1 * 1f / h1;
      //获取原图宽高比
      final float ratioImg = w2 * 1f / h2;
      //是否显示中间部分,不显示中间就显示顶部
      final boolean isShowCenter = true;
      //需要重新设置的缩放比
      float scaleTemp = -1f;
      float centerX = -1f;
      float centerY = -1f;
      if (ratioImg > ratioView) {//宽图
        scaleTemp = h1 * 1f / h2;
        centerY = w2 / 2f;
        if (isShowCenter) {//显示中间部分,适配高度
          centerX = w2 / 2f;
        } else {//显示左边部分,适配高度
          centerX = w1 / 2f / scaleTemp;
        }
      } else if (ratioImg < ratioView) {//高图
        scaleTemp = w1 * 1f / w2;
        centerX = h2 / 2f;
        if (isShowCenter) {//显示中间部分,适配宽度
          centerY = h2 / 2f;
        } else {//显示上边部分,适配宽度
          centerY = h1 / 2f / scaleTemp;
        }
      } else {//默认显示全部内容,不作处理
      }
      if (scaleTemp >= 0 && centerX >= 0 && centerY >= 0) {
        PointF pointF = new PointF(centerX, centerY);
        this.pendingScale = scaleTemp;
        this.sPendingCenter = pointF;
        this.sRequestedCenter = pointF;
      }
    }
  }
②调用地方
  private boolean checkReady() {
    boolean ready =  getWidth() > 0 && getHeight() > 0 && sWidth > 0 && sHeight > 0 && (bitmap != null || isBaseLayerReady());
    if (!readySent && ready) {
      preDraw();
      readySent = true;
      onReady();
      if (onImageEventListener != null) {
        onImageEventListener.onReady();
      }
      setNewDefaultScale();//第一次准备好的时候调用
    }
    return ready;
  }

2.设置网络加载

//使用Glide下载图片,保存到本地
Glide.with(this)
    .download(url)
    .into(object : SimpleTarget() {
      override fun onLoadFailed(errorDrawable: Drawable?) {
        super.onLoadFailed(errorDrawable)
        main_ssiv.setBackgroundColor(Color.RED)
      }
      override fun onResourceReady(
        resource: File,
        transition: Transition?
      ) {
        main_ssiv.setImage(ImageSource.uri(resource.absolutePath))
      }
    })

你可能感兴趣的:(2019-04-04 SubsamplingScaleImageView初步使用(仅限使用))