Android sets any transparency of Bitmap images

Source: Internet
Author: User

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.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.