Fill pattern in Image in Android

2019-06-21 08:49发布

问题:

Two images are given blow i call first image as frame image and second image as frame image.Here fst is my Linear Layout and i set the frame-image as background image of it. Now i want to fill the pattern image in my frame image's white area. Outer area of the frame image is transparent and inner area is white. How can i fill pattern image in my frame-Image. I tryied 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 this is giving a square bitmap Image. and my Imageview is like this in which i want to fill pattern.

pattern image is like this-

I want to fill pattern image in the white area of this image only. I am able to fill the color inside this image frame but not done in case of pattern Images. can any one help me please ...

UPDATE :- I read that if we want to set more then one color so we should use 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 am able to set the shader successfully . After this method 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 like: DST_OVER,DST_IN

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

        drawPaint.setXfermode(new PorterDuffXfermode(Mode.DST_OVER));

but not getting result according to need.

Is any mode can solve my purpose currently I am getting this output

I think I am very close to the result. I can draw the pattern what ever i need now the remaning problem is to set patten only in Inner non transparent area of frame.

I also tried this

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

to identify transparent area so that i can fill pattern in another nontransparent are but this also not working.

Here I am uploading the Image for more clarification In the blow Image I fill the color in frame-image Now I want to fill pattern in place of Pink color.

Can anyone have any good point to try please share ...

回答1:

You can have look on the following tutorial for your help

Drawing with Pattern Fills by Sue Smith

Using the above tutorial you can get following output