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!