Android load drawable programmatically and resize it

浪尽此生 提交于 2019-12-12 08:45:42

问题


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

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

How does Android do dynamic resizing of the drawables in /res?


回答1:


This is nice and easy (the other answers weren't working for me), found 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 documentation is available here.




回答2:


Found it:

  /**
   * 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;
  }

Use like this:

loadImageFromFilesystem(context, filename, DisplayMetrics.DENSITY_MEDIUM);



回答3:


If you want to display an image but unfortunately this image is of large size, lets example, you want to display an image in 30 by 30 format, then check its size if it is greater than your require size, then divide it by your amount(30*30 here in this case), and what you got is again take and use for dividing the image area again.

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:


after you load your picture and set it to imageview you can use layoutparamsto size your image to match_parent

like this

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


来源:https://stackoverflow.com/questions/8837810/android-load-drawable-programmatically-and-resize-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!