In two ways, the first idea is to get the bitmap pixel value into a int[] array by manipulating the bitmap, because bitmap is usually the ARGB8888 format in Android, so the highest bit is the value of a channel, After the alignment is changed, create a new bitmap. The second idea is to change the transparency of the view by setting the transparency of the canvas's paint and then Canvas.drawbitmap (). The specific code is as follows:
The first way of thinking:
Public StaticBitmap Gettransparentbitmap (Bitmap sourceimg,intNumber ) { int[] Argb =New int[Sourceimg.getwidth () *sourceimg.getheight ()]; Sourceimg.getpixels (ARGB,0, Sourceimg.getwidth (),0,0, sourceimg. GetWidth (), Sourceimg.getheight ());//get the ARGB value of the picture Number= number *255/ -; for(inti =0; i < argb.length; i++) {Argb[i]= (number << -) | (Argb[i] &0x00ffffff); } sourceimg=Bitmap.createbitmap (ARGB, Sourceimg.getwidth (), sourceimg. GetHeight (), config.argb_8888); returnsourceimg; }
The above code by my pro-test available, fixed the link two errors. The range of number is 0-100,0, which means that it is completely transparent and completely invisible. One of the most critical steps you can see is argb[i] = (number << 24) | (Argb[i] & 0X00FFFFFF); By using (Argb[i] & 0X00FFFFFF), the A channel of the pixel of point I is set to 0, and then (NUM<<24) is performed or manipulated. Shift See links
The second way of thinking:
Example code:
classDrawcanvas extends View { PublicDrawcanvas (Context context) {super (context); } @Overrideprotected voidOnDraw (canvas canvas) {super.ondraw (canvas); //get the bitmap of resource picturesBitmap Vbitmap = Bitmapfactory.decoderesource ( This. Getresources (), R.drawable.icon ); //Creating Paint ObjectsPaint Vpaint =NewPaint (); Vpaint. SetStyle (Paint.Style.STROKE); //HollowVpaint. Setalpha ( the);// Canvas.drawbitmap (Vbitmap, -, -,NULL);//No TransparencyCanvas.drawbitmap (Vbitmap, -, $, Vpaint);//have transparent } }
About Canvas.drawbitmap See links
Both methods have their uses and which ones are used.
Android settings picture Bitmap any transparency