Android loads the rendering programmatically and resizes it

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

The original image is in hdpi format, I only need to resize for mdpi and ldpi.

How does Android dynamically resize items in / res?

+8
android image drawable
source share
4 answers

Found this:

/** * 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); 
+4
source share

It's nice and easy (other answers didn't work 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); 

The documentation for Android is available here .

+8
source share

If you want to display an image, but, unfortunately, this image is large, for example, you want to display an image in 30 by 30 format, then check its size if it is larger than your desired size, and then divide it by your total (30 * 30 here in this case), and what you got is again taken and used to divide 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) 
+1
source share

after loading the image and setting it to image view mode you can use layoutparamsto image size for match_parent

like this

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

All Articles