【问题标题】:Scale images inside textview using ImageGetter使用 ImageGetter 在 textview 中缩放图像
【发布时间】:2014-04-23 23:20:05
【问题描述】:

我正在尝试缩放文本视图中显示的图像,但我不能。

我正在使用此代码,但无论如何,它都会显示在容器内裁剪的图像或根本不显示。

int width, height;
DisplayMetrics metrics = new DisplayMetrics();
metrics = Resources.getSystem().getDisplayMetrics();

int originalWidthScaled = (int) (result.getIntrinsicWidth() * metrics.density);
            int originalHeightScaled = (int) (result.getIntrinsicHeight() * metrics.density);
            if (originalWidthScaled > metrics.widthPixels) {
                height = result.getIntrinsicHeight() * metrics.widthPixels
                        / result.getIntrinsicWidth();
                width = metrics.widthPixels;
            } else {
                height = originalHeightScaled;
                width = originalWidthScaled;
            }
                urlDrawable.drawable = result; 

                urlDrawable.setBounds(0, 0, 0+width, 0+height);

                // change the reference of the current drawable to the result 
                // from the HTTP call 

                // redraw the image by invalidating the container 

                container.invalidate();

                // For ICS
                container.setHeight(
                        container.getHeight() + 
                        result.getIntrinsicHeight());

                // Pre ICS
                container.setEllipsize(null);

【问题讨论】:

    标签: android textview android-image


    【解决方案1】:

    我回答自己我变了

     if (originalWidthScaled > metrics.widthPixels) {
                    height = result.getIntrinsicHeight() * metrics.widthPixels
                            / result.getIntrinsicWidth();
                    width = metrics.widthPixels;
                }
    

    if (originalWidthScaled > (metrics.widthPixels * 70) / 100) {
                    width = (metrics.widthPixels * 70) / 100;
    
                    height = result.getIntrinsicHeight() * width
                            / result.getIntrinsicWidth();
                }
    

    现在它占据了屏幕空间的 70%,正好是容器的最大尺寸

    【讨论】:

      【解决方案2】:

      对于仍在使用新 API 寻找答案的任何人,ImageGetter 的此自定义实现应该允许您放大将占据设备显示宽度的图像,如果给定图像大于设备显示则缩小宽度或保留其原始尺寸(如果更小)。

      /**
       * Custom ImageGetter for [HtmlCompat.fromHtml] which accept both Url and Base64 from img tag.
       * */
      class HtmlImageGetter(
          private val scope: LifecycleCoroutineScope,
          private val res: Resources,
          private val glide: RequestManager,
          private val htmlTextView: AppCompatTextView,
          @DrawableRes
          private val errorImage: Int = 0,
          private val matchParent: Boolean = true
      ) : ImageGetter {
      
          override fun getDrawable(source: String): Drawable {
              val holder = BitmapDrawablePlaceHolder(res, null)
      
              scope.launch(Dispatchers.IO) {
                  runCatching {
      
                      glide
                          .asBitmap()
                          .load(
                              if (source.matches(Regex("data:image.*base64.*")))
                                  Base64.decode(
                                      source.replace("data:image.*base64".toRegex(), ""),
                                      Base64.DEFAULT
                                  ) // Image tag used Base64
                              else
                                  source // Image tag used URL
                          )
                          .submit()
                          .get()
      
                  }
                      .onSuccess { setDrawable(holder, it) }
                      .onFailure {
      
                          if (errorImage != 0)
                              BitmapFactory.decodeResource(res, errorImage)?.let {
                                  setDrawable(holder, it)
                              }
      
                      }
              }
      
              return holder
          }
      
          private suspend fun setDrawable(holder: BitmapDrawablePlaceHolder, bitmap: Bitmap) {
              val drawable = BitmapDrawable(res, bitmap)
      
              val width: Int
              val height: Int
      
              val metrics = res.displayMetrics
              val displayWidth = metrics.widthPixels - (htmlTextView.paddingStart + htmlTextView.paddingEnd + htmlTextView.marginStart + htmlTextView.marginEnd) * 100 / 100
      
              val imageWidthScaled = (drawable.intrinsicWidth * metrics.density)
              val imageHeightScaled = (drawable.intrinsicHeight * metrics.density)
      
              // Scale up if matchParent is true
              // Scale down if matchParent is false
              if (matchParent || imageWidthScaled > displayWidth) {
                  width = displayWidth
                  height = (drawable.intrinsicHeight * width / drawable.intrinsicWidth)
              }
              else {
                  height = imageHeightScaled.roundToInt()
                  width = imageWidthScaled.roundToInt()
              }
      
              drawable.setBounds(0, 0, width, height)
      
              holder.setDrawable(drawable)
              holder.setBounds(0, 0, width, height)
      
              withContext(Dispatchers.Main) { htmlTextView.text = htmlTextView.text }
          }
      
          internal class BitmapDrawablePlaceHolder(res: Resources, bitmap: Bitmap?) :
              BitmapDrawable(res, bitmap) {
              private var drawable: Drawable? = null
      
              override fun draw(canvas: Canvas) {
                  drawable?.run { draw(canvas) }
              }
      
              fun setDrawable(drawable: Drawable) {
                  this.drawable = drawable
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2016-02-22
        • 1970-01-01
        • 1970-01-01
        • 2021-07-25
        • 2013-01-05
        • 1970-01-01
        • 1970-01-01
        • 2011-12-13
        • 2021-05-08
        相关资源
        最近更新 更多