Fill a template in Image on Android

Two images give a hit, I call the first image as a frame image, and the second image as a frame image. Here fst is my linear layout, and I set the frame image as the background image. Now I want to fill in the image of the picture in the white area of ​​the image frame. The outer region of the image is transparent, and the inner region is white. How can I fill an image image in my image frame. I tried this code.

private void patternFill(Bitmap tempBitmapColor) { Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pattern_a); BitmapDrawable bitmapDrawable = new BitmapDrawable(bmp); bitmapDrawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT); fst.setBackgroundDrawable(bitmapDrawable); } 

but it gives a square bitmap. and my image looks like this in which I want to fill out a template.

enter image description here

The image of the picture looks like this:

enter image description here

I want to fill out the image only in the white area of ​​this image. I can fill in the color inside this image frame, but not in the case of image templates. can someone help me ...

UPDATE: - I read that if we want to set more than one color, so we must use a shader, I updated the current code

 public void setPattern(String newPattern){ postInvalidate(); //get pattern int patternID = getResources().getIdentifier(newPattern, "drawable", "com.akanksha.partternfillexperiment"); //decode Bitmap patternBMP = BitmapFactory.decodeResource(getResources(), patternID); //create shader BitmapShader patternBMPshader = new BitmapShader(patternBMP, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT); //color and shader // drawPaint.setColor(0xFFFFFFFF); drawPaint.setShader(patternBMPshader); patternset=true; } 

So, here I can successfully install the shader. After this method, the OnDraw method will call

 @Override protected void onDraw(Canvas canvas) { if(patternset){ for (int x = 0; x < canvasBitmap.getWidth(); x++) { drawPath.moveTo(x, 0); for (int y = 0; y < canvasBitmap.getHeight(); y++) { drawPath.lineTo(x, y); } drawCanvas.drawPath(drawPath, drawPaint); drawPath.reset(); invalidate(); canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint); canvas.drawPath(drawPath, drawPaint); } 

What is setXfermode I tried some mode in my setpattern method for example: DST_OVER, DST_IN

 drawPaint.setColorFilter(new PorterDuffColorFilter(Color.YELLOW,Mode.MULTIPLY)); drawPaint.setXfermode(new PorterDuffXfermode(Mode.DST_OVER)); 

but not getting the result according to needs.

Can any mode solve my goal now? I get this conclusion

enter image description here

I think that I am very close to the result. I can draw the template that I now need, the problem with the fix is ​​to set patten only in the Inner opaque area of ​​the frame.

I also tried this

if (canvasBitmap.getPixel (x, y) == Color.TRANSPARENT)

to identify the transparent area so that I can fill the template in another opaque state, but this also does not work.

Here I upload an image for a more detailed explanation. In the stroke image, I fill in the color in the image frame. Now I want to fill in the template instead of pink.

enter image description here

Does anyone have a good opportunity to try and share with you ...

+5
source share
1 answer

You can see the next tutorial for your help.

Sue Smith Pattern Fill Drawing

Using the tutorial above, you can get the following output enter image description here

0
source

All Articles