【问题标题】:Android load drawable programmatically and resize itAndroid以编程方式加载drawable并调整它的大小
【发布时间】:2012-02-08 21:40:39
【问题描述】:

如何从 InputStream(资产、文件系统)加载可绘制对象并根据屏幕分辨率 hdpi、mdpi 或 ldpi 动态调整其大小?

原图是hdpi,我只需要调整mdpi和ldpi的大小。

Android 如何动态调整 /res 中的可绘制对象的大小?

【问题讨论】:

  • 出于好奇,有什么理由不预先调整它的大小(对于mdpi 和ldpi)并在res 目录中链接到它?
  • 网络下载的图片。我可以在服务器上进行预处理,但下载会比较慢。

标签: android image drawable


【解决方案1】:

这很好也很简单(其他答案对我不起作用),找到here:

  ImageView iv = (ImageView) findViewById(R.id.imageView);
  Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.picture);
  Bitmap bMapScaled = Bitmap.createScaledBitmap(bMap, newWidth, newHeight, true);
  iv.setImageBitmap(bMapScaled);

Android 文档可用here。

【讨论】:

    【解决方案2】:

    找到了:

      /**
       * Loads image from file system.
       * 
       * @param context the application context
       * @param filename the filename of the image
       * @param originalDensity the density of the image, it will be automatically
       * resized to the device density
       * @return image drawable or null if the image is not found or IO error occurs
       */
      public static Drawable loadImageFromFilesystem(Context context, String filename, int originalDensity) {
        Drawable drawable = null;
        InputStream is = null;
    
        // set options to resize the image
        Options opts = new BitmapFactory.Options();
        opts.inDensity = originalDensity;
    
        try {
          is = context.openFileInput(filename);
          drawable = Drawable.createFromResourceStream(context.getResources(), null, is, filename, opts);         
        } catch (Exception e) {
          // handle
        } finally {
          if (is != null) {
            try {
              is.close();
            } catch (Exception e1) {
              // log
            }
          }
        }
        return drawable;
      }
    

    这样使用:

    loadImageFromFilesystem(context, filename, DisplayMetrics.DENSITY_MEDIUM);
    

    【讨论】:

    【解决方案3】:

    如果您想显示一张图片,但不幸的是这张图片尺寸很大,例如,您想以 30 x 30 格式显示一张图片,然后检查其尺寸是否大于您需要的尺寸,然后将其分割按你的数量(在这种情况下是 30*30),你得到的是再次拿来并用于再次划分图像区域。

    drawable = this.getResources().getDrawable(R.drawable.pirImg);
    int width = drawable.getIntrinsicWidth();
    int height = drawable.getIntrinsicHeight();
    if (width > 30)//means if the size of an image is greater than 30*30
    {
      width = drawable.getIntrinsicWidth() / 30;
      height = drawable.getIntrinsicWidth() / 30;
    }
    
    drawable.setBounds(
        0, 0, 
        drawable.getIntrinsicWidth() / width, 
        drawable.getIntrinsicHeight() / height);
    
    //and now add the modified image in your overlay
    overlayitem[i].setMarker(drawable)
    

    【讨论】:

      【解决方案4】:

      加载图片并将其设置为 imageview 后 您可以使用 layoutparams 来调整您的图像大小以匹配父项

      喜欢这个

      android.view.ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
      layoutParams.width =MATCH_PARENT;
      layoutParams.height =MATCH_PARENT;
      imageView.setLayoutParams(layoutParams);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-07-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多