Android draws and saves the actual implementation code for the image

  • 2020-05-17 06:19:58
  • OfStack

Canvas is a canvas, you can create a blank canvas, just new1 Canvas object, no parameters required.
You can also use BitmapFactory to create an Bitmap object as a parameter to the new Canvas object, which means that the canvas is not blank,
If you want to save the image, it is better to have Bitmap as a new one, rather than as an Drawable object read in from a file.

Then use Canvas to draw the first image, then the second image, and finally use Canvas.save (int flag) to save. Note that the parameters in save can save a single layer.
Use save(Canvas.ALL_SAVE_FLAG) if you want to save all layers.

Finally, all the information is saved in the first Bitmap created. The code is as follows:
Java code

/** 
    * create the bitmap from a byte array 
    * 
    * @param src the bitmap object you want proecss 
    * @param watermark the water mark above the src 
    * @return return a bitmap object ,if paramter's length is 0,return null 
    */  
   private Bitmap createBitmap( Bitmap src, Bitmap watermark )  
   {  
       String tag = "createBitmap";  
       Log.d( tag, "create a new bitmap" );  
       if( src == null )  
       {  
           return null;  
       }  

       int w = src.getWidth();  
       int h = src.getHeight();  
       int ww = watermark.getWidth();  
       int wh = watermark.getHeight();  
       //create the new blank bitmap  
       Bitmap newb = Bitmap.createBitmap( w, h, Config.ARGB_8888 );// create 1 A new and SRC The length of the width 1 The sample of the bitmap   
       Canvas cv = new Canvas( newb );  
       //draw src into  
       cv.drawBitmap( src, 0, 0, null );// in  0 . 0 Let me draw the coordinates src  
       //draw watermark into  
       cv.drawBitmap( watermark, w - ww + 5, h - wh + 5, null );// in src Draw the watermark in the lower right corner   
       //save all clip  
       cv.save( Canvas.ALL_SAVE_FLAG );// save   
       //store  
       cv.restore();// storage   
       return newb;  
   }  

Methods of image reduction:
Java code

/** 
    * lessen the bitmap 
    * 
    * @param src bitmap 
    * @param destWidth the dest bitmap width 
    * @param destHeigth 
    * @return new bitmap if successful ,oherwise null 
    */  
   private Bitmap lessenBitmap( Bitmap src, int destWidth, int destHeigth )  
   {  
       String tag = "lessenBitmap";  
       if( src == null )  
       {  
           return null;  
       }  
       int w = src.getWidth();// The size of the source file   
       int h = src.getHeight();  
       // calculate the scale - in this case = 0.4f  
       float scaleWidth = ( ( float ) destWidth ) / w;// Width reduction ratio   
       float scaleHeight = ( ( float ) destHeigth ) / h;// Height reduction ratio   
       Log.d( tag, "bitmap width is :" + w );  
       Log.d( tag, "bitmap height is :" + h );  
       Log.d( tag, "new width is :" + destWidth );  
       Log.d( tag, "new height is :" + destHeigth );  
       Log.d( tag, "scale width is  :" + scaleWidth );  
       Log.d( tag, "scale height is  :" + scaleHeight );  
       Matrix m = new Matrix();// matrix   
       m.postScale( scaleWidth, scaleHeight );// Set the matrix scale   
       Bitmap resizedBitmap = Bitmap.createBitmap( src, 0, 0, w, h, m, true );// Draw the source file directly to the scale of the matrix   
       return resizedBitmap;  
   } 

Related articles: