Android的负载绘制编程并调整其大小 [英] Android load drawable programatically and resize it

查看:101
本文介绍了Android的负载绘制编程并调整其大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何可以加载从绘制的InputStream(资产,文件系统),并动态地调整其大小根据屏幕分辨率华电国际,MDPI和LDPI?

How can I load drawable from InputStream (assets, file system) and resize it dynamically based on the screen resolution hdpi, mdpi or ldpi?

原始图像是华电国际,我只需要调整大小为MDPI和LDPI。

The original image is in hdpi, I only need resizing for mdpi and ldpi.

有谁知道机器人是如何做的可绘制的动态调整中/ RES?

Does anyone know how Android does dynamic resizing of the drawables in /res?

推荐答案

发现:

  /**
   * 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 (Throwable e) {
      // handle
    } finally {
      if (is != null) {
        try {
          is.close();
        } catch (Throwable e1) {
          // ingore
        }
      }
    }
    return drawable;
  }

使用这样的:

loadImageFromFilesystem(context, filename, DisplayMetrics.DENSITY_MEDIUM);

这篇关于Android的负载绘制编程并调整其大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆