The first approach is to operate Bitmap to get the Bitmap pixel value to an int [] array, because in android, Bitmap is generally in the ARGB8888 format, therefore, the highest bit is the value of Channel A. After the alignment changes, you can create A new Bitmap. The second approach is to change the transparency of the View by setting the paint transparency of the canvas and then using canvas. drawBitmap. The Code is as follows:
First thought:
Public static Bitmap getTransparentBitmap (Bitmap sourceImg, int number) {int [] argb = new int [sourceImg. getWidth () * sourceImg. getHeight ()]; sourceImg. getPixels (argb, 0, sourceImg. getWidth (), 0, 0, sourceImg. getWidth (), sourceImg. getHeight (); // obtain the ARGB value of the Image number = number * 255/100; for (int I = 0; I <argb. length; I ++) {argb [I] = (number <24) | (argb [I] & 0x00FFFFFF);} sourceImg = Bitmap. createBitmap (argb, sourceImg. getWidth (), sourceImg. getHeight (), Config. ARGB_8888); return sourceImg ;}
The above code was tested and available by myself, and two Link errors were corrected. The range of number is 0-. 0 indicates completely transparent and invisible. The most critical step is argb [I] = (number <24) | (argb [I] & 0x00FFFFFF); pass (argb [I] & 0x00FFFFFF) set the channel of pixels at point I to 0, and then perform or operations with (num <24. See the link for shift
The second approach:
Sample Code:
Class drawCanvas extends View {public drawCanvas (Context context) {super (context) ;}@ Override protected void onDraw (Canvas canvas) {super. onDraw (canvas); // obtain the Bitmap of the Resource image. Bitmap vBitmap = BitmapFactory. decodeResource (this. getResources (), R. drawable. icon); // create the Paint object Paint vPaint = new Paint (); vPaint. setStyle (Paint. style. STROKE); // hollow vPaint. setAlpha (75); // canvas. drawBitmap (vBitmap, 50,100, null); // transparent canvas. drawBitmap (vBitmap, 50,200, vPaint); // transparent }}
For more information about canvas. drawBitmap, see the link.
The two methods have their own purposes.