Android cut the image and set the background color to transparent

Source: Internet
Author: User

Recently, we have cut the image and set the background color to transparent in developing spliced images. Here we will share the implementation method.

First, to ensure that the color is transparent after being removed, the image quality must be config. argb_4444 or config. argb_8888. Use the following methods to convert the resource map:

bmp = bmp.copy(Bitmap.Config.ARGB_8888, true);

Then you can filter out the Code as follows:

Canvas c = new Canvas(bmp);Paint p = new Paint();p.setAlpha(0);p.setXfermode(new AvoidXfermode(removeColor, 0, AvoidXfermode.Mode.TARGET));c.drawPaint(p);return bmp;

For the usage of setxfermode () method, refer to the http://yueguc.iteye.com/blog/782109

After conversion, you can cut the resource map using the following method to obtain the required graph block.

Bitmap tileImg = Bitmap.createBitmap(sourceImg, xIndex, yIndex,  set.tileWidth, set.tileHeight);

However, after the image block is cut out, we find that this is not the effect we want, and the background color turns black rather than transparent.

Test found

Bitmap.createBitmap(sourceImg, xIndex, yIndex,  set.tileWidth, set.tileHeight);

The created image is converted to rgb_565 regardless of the number of resource charts. Therefore, transparent channels are not supported.

Another cutting method is found in online search, which can retain the quality of the source image.

public static Bitmap cutBitmap(Bitmap mBitmap, Rect r, Bitmap.Config config) {          int width = r.width();          int height = r.height();                  Bitmap croppedImage = Bitmap.createBitmap(width, height, config);                  Canvas canvas = new Canvas(croppedImage);        Rect dr = new Rect(0, 0, width, height);          canvas.drawBitmap(mBitmap, r, dr, null);        return croppedImage;}

Finally, the method is verified to be feasible. If you have other implementation methods, feel free to share your thoughts!

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.