在Android中的图像中填充图案 [英] Fill pattern in Image in Android

查看:247
本文介绍了在Android中的图像中填充图案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定两个图像,我将第一个图像称为框架图像,将第二个图像称为框架图像.这里是我的线性布局,我将框架图像设置为其背景图像.现在,我想在我的框架图像的白色区域中填充图案图像.帧图像的外部区域是透明的,内部区域是白色的. 如何在我的框架图像中填充图案图像.我试过了这段代码.

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);


 }

但是这给出的是正方形位图图像. 我的Imageview就是这样,我要填充图案.

but this is giving a square bitmap Image. and my Imageview is like this in which i want to fill pattern.

图案图像是这样的-

我只想在该图像的白色区域填充图案图像.我能够填充此图像框架内的颜色,但在图案图像的情况下无法完成. 谁能帮我...

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;

}

因此,在这里,我能够成功设置着色器. 在此方法之后,OnDraw方法将调用

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);
        }

什么是setXfermode 我在我的setpattern方法中尝试了一些模式 像:DST_OVER,DST_IN

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.

我也尝试过

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

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 ...

推荐答案

您可以查看以下教程以获取帮助

You can have look on the following tutorial for your help

使用图案填充进行绘制由苏·史密斯(Sue Smith)

Drawing with Pattern Fills by Sue Smith

使用以上教程,您可以获得以下输出

Using the above tutorial you can get following output

这篇关于在Android中的图像中填充图案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆