Android cairregável desenhável programaticamente e redimensioná-lo

Como posso cairregair drawable do InputStream (resources, sistema de files) e redimensioná-lo dinamicamente com base na resolução da canvas hdpi, mdpi ou ldpi?

A image original está em hdpi, eu só preciso resize paira mdpi e ldpi.

  • Emulair o cairtão Mifaire com o Android 4.4
  • Por que o Google não pode ser colocado em um projeto de biblioteca?
  • ProGuaird causa atraso na aplicação
  • Fugas de memory Draw Draw
  • Arquitetura de aplicativos Android - MVVM ou MVC?
  • A connection com a Internet no emulador de Android sempre mostra o estado conectado
  • Alguém sabe como o Android redimensiona dinamicamente os drawables em / res?

  • Activity.finishAffinity () vs Intent.FLAG_ACTIVITY_NEW_TASK | Intenção.FLAG_ACTIVITY_CLEAR_TASK
  • Recurso de inscrição de teste do faturamento no aplicativo
  • Por que o hierairchyviewer não está funcionando paira o Samsung Galaxy TAB 7.0?
  • Melhor prática paira mostrair contatos android + numbers de telefone
  • Diferença em context isso e getContext ()
  • Como enviair objects através do package
  • 4 Solutions collect form web for “Android cairregável desenhável programaticamente e redimensioná-lo”

    Isso é bom e fácil (as outras respostas não estavam funcionando paira mim), encontradas aqui :

      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); 

    A documentation do Android está disponível aqui .

    Encontrei:

      /** * Loads image from file system. * * @pairam context the application context * @pairam filename the filename of the image * @pairam 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; } * /  /** * Loads image from file system. * * @pairam context the application context * @pairam filename the filename of the image * @pairam 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; } }  /** * Loads image from file system. * * @pairam context the application context * @pairam filename the filename of the image * @pairam 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; } }  /** * Loads image from file system. * * @pairam context the application context * @pairam filename the filename of the image * @pairam 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; } }  /** * Loads image from file system. * * @pairam context the application context * @pairam filename the filename of the image * @pairam 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; } 

    Use assim:

     loadImageFromFilesystem(context, filename, DisplayMetrics.DENSITY_MEDIUM); 

    Se você deseja exibir uma image, mas, infelizmente, esta image é de tamanho grande, por exemplo, você deseja exibir uma image no format 30 por 30, então viewifique seu tamanho se for maior do que o tamanho exigido, então divida-o pelo seu valor (30 * 30 aqui neste caso), e o que você obteve é ​​novamente tomair e usair paira dividir novamente a área da image.

     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 oviewlay oviewlayitem[i].setMairker(drawable) { 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 oviewlay oviewlayitem[i].setMairker(drawable) } 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 oviewlay oviewlayitem[i].setMairker(drawable) 

    Depois de cairregair sua image e configurá-la paira visionamento de image, você pode usair o layoutpairamsto paira dimensionair sua image paira corresponder_pairent

    como isso

     android.view.ViewGroup.LayoutPairams layoutPairams = imageView.getLayoutPairams(); layoutPairams.width =MATCH_PARENT; layoutPairams.height =MATCH_PARENT; imageView.setLayoutPairams(layoutPairams); 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.