Uso de mapa de bits

Bitmap 13 uso:

  01.Dibujo de uso

Recursos recursos = mContext.getResources (); 
Drawable drawable = resources.getDrawable (R.drawable.a); 
imageview.setBackground (dibujable);

  02. Uso de mapa de bits Mapa de bits dibujable

Recursos r = this .getContext (). GetResources (); 
Inputstream es = r.openRawResource (R.drawable.my_background_image); 
BitmapDrawable bmpDraw = nuevo BitmapDrawable (es); 
Mapa de bits bmp = bmpDraw.getBitmap ();

 

  03.Bitmap -> BitmapFactory.decodeResource

Bitmap bmp = BitmapFactory.decodeResource (r, R.drawable.icon); 
Bitmap newb = Bitmap.createBitmap (300, 300, Config.ARGB_8888);

 

  04.Bitmap -> BitmapFactory.decodeStream

InputStream es = getResources (). OpenRawResource (R.drawable.icon);  
Mapa de bits mBitmap = BitmapFactory.decodeStream (es);

 

  05.Dibujable → Mapa de bits

pública  estática Bitmap drawableToBitmap (dibujable Disponibles) { 
mapa de bits de mapa de bits = Bitmap 
.createBitmap ( 
drawable.getIntrinsicWidth (), 
drawable.getIntrinsicHeight (), 
drawable.getOpacity () = PixelFormat.OPAQUE!? Bitmap.Config.ARGB_8888: Bitmap.Config.RGB_565 ); 
Canvas canvas = nuevo Canvas (mapa de bits);
// canvas.setBitmap (bitmap); 
drawable.setBounds (0, 0 , drawable.getIntrinsicWidth (), 
drawable.getIntrinsicHeight ()); 
drawable.draw (lienzo); 
devolver mapa de bits; 
}

 

  06. Obtener mapa de bits de los recursos

Recursos res = getResources (); 
Bitmap bmp = BitmapFactory.decodeResource (res, R.drawable.pic);

 

  07.Bitmap → byte []

 byte privado [] Bitmap2Bytes (Bitmap bm) { 
ByteArrayOutputStream baos = nuevo ByteArrayOutputStream (); 
bm.compress (Bitmap.CompressFormat.PNG, 100 , baos);
return baos.toByteArray (); 
}

 

  08.byte [] → Mapa de bits

Bytes2Bimap privados de mapa de bits ( byte [] b) {
  if (b.length! = 0 ) {
    return BitmapFactory.decodeByteArray (b, 0 , b.length); 
  } 
  else {
    return  null ; 
  } 
}

 

  09. Guardar mapa de bits

static  boolean saveBitmap2file (Bitmap bmp, String filename) { 
  CompressFormat format = Bitmap.CompressFormat.JPEG;
  int calidad = 100 ; 
  OutputStream stream = null ;
  pruebe { 
  stream = new FileOutputStream ("/ sdcard /" + filename); 
  } catch ( 
    FileNotFoundException e) { 
    // TODO Bloque de captura generado automáticamente Generado por Foxit PDF Creator © Foxit Software http://www.foxitsoftware.com Solo para evaluación. 
    e.printStackTrace (); 
  } 
  volverbmp.compress (formato, calidad, transmisión); 
}

 

  10. Amplíe la imagen según sus requisitos.

Bitmap bm = BitmapFactory.decodeStream (getResources () 
.openRawResource (R.drawable.dog)); 
// Obtenga el ancho y el alto de la imagen 
int width = bm.getWidth ();
 int height = bm.getHeight ();
 // Establezca el pensamiento El tamaño requerido 
int newWidth = 320 ;
 int newHeight = 480 ;
 // Calcule la relación de escala 
float scaleWidth = (( float ) newWidth) / width;
 float scaleHeight = (( float ) newHeight) / height;
 // Obtenga la matriz que desea escalar Parámetro 
Matrix matrix = new Matrix ();
matrix.postScale (scaleWidth, scaleHeight);
// Obtenga una nueva imagen 
Bitmap newbm = Bitmap.createBitmap (bm, 0, 0 , width, height, matrix,
 true );
 // Póngalo en el lienzo 
canvas.drawBitmap (newbm, 0, 0, paint);

 

  11.Imagen de archivo a Bitmap

Bitmap bt = BitmapFactory.decodeFile ("/ sdcard / myImage /" + "head.jpg"); // Dirección de imagen

 

  12. Imagen a mapa de bits

mapa de bits público drawableToBitamp ( int drawableResource) {   // 可以 取 raw 里面 的 资源 
        BitmapFactory.Options opt = new BitmapFactory.Options (); 
        opt.inPreferredConfig = Bitmap.Config.RGB_565; 
        opt.inPurgeable = true ; 
        opt.inInputShareable = true ; 
        InputStream es = this .getResources (). OpenRawResource (drawableResource); 
        BitmapFactory.decodeStream (es, nulo , opt);
        return BitmapFactory.decodeStream (es, nulo  , opt);
}

 

  13. Resumen de uso del mapa de bits

BitmapFactory.Options option = new BitmapFactory.Options (); 
option.inSampleSize = 2; // Establezca la imagen en 1/2 del ancho y alto originales para evitar el desbordamiento de la memoria 
Bitmap bm = BitmapFactory.decodeFile ("", opción); // 
URL de secuencia de archivo url = nueva URL ("" ); 
InputStream es = url.openStream (); 
Bitmap bm = BitmapFactory.decodeStream (is); 
android: scaleType: 
android: scaleType // Controla cómo se redimensiona / mueve la imagen para que coincida Tamaño de ImageView. ImageView.ScaleType /

 

Supongo que te gusta

Origin www.cnblogs.com/hugeba/p/12744493.html
Recomendado
Clasificación