Flood fill algorithm on vector drawable image view

我的梦境 提交于 2019-12-08 06:05:29

问题


I want to create application like this.

Flood fill algorithm

I applied that code and it is working fine with JPG or PNG file. But I want to use that algorithm with Vector drawable imageview

Vector drawable imageview before flood fill

After flood fill of vector imageview

Expected result should be like this (Flood fill work perfectly when I set JPG or PNG file)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_flood_fill);

    iv_FloodFillActivity_image = findViewById(R.id.iv_FloodFillActivity_image);
    iv_FloodFillActivity_image.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                floodColor(event.getX(), event.getY());
            }
            return true;
        }
    });
}

private void floodColor(float x, float y) {
    final Point p1 = new Point();
    p1.x = (int) x;// X and y are co - ordinates when user clicks on the screen
    p1.y = (int) y;

    Bitmap bitmap = getBitmapFromVectorDrawable(iv_FloodFillActivity_image.getDrawable());
    //bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);

    int pixel = bitmap.getPixel((int) x, (int) y);
    int[] sourceColorRGB = new int[]{
            Color.red(pixel),
            Color.green(pixel),
            Color.blue(pixel)
    };

    final int targetColor = Color.parseColor("#FF2200");

    QueueLinearFloodFiller queueLinearFloodFiller = new QueueLinearFloodFiller(bitmap, sourceColor, targetColor);

    int toleranceHex = Color.parseColor("#545454");
    int[] toleranceRGB = new int[]{
            Color.red(toleranceHex),
            Color.green(toleranceHex),
            Color.blue(toleranceHex)
    };
    queueLinearFloodFiller.setTolerance(toleranceRGB);
    queueLinearFloodFiller.setFillColor(targetColor);
    queueLinearFloodFiller.setTargetColor(sourceColorRGB);
    queueLinearFloodFiller.floodFill(p1.x, p1.y);

    bitmap = queueLinearFloodFiller.getImage();
    iv_FloodFillActivity_image.setImageBitmap(bitmap);
}

private Bitmap getBitmapFromVectorDrawable(Drawable drawable) {
    try {
        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
        return bitmap;
    } catch (OutOfMemoryError e) {
        return null;
    }
}

Check Class : QueueLinearFloodFiller

How can I use vector drawable?

来源:https://stackoverflow.com/questions/50426729/flood-fill-algorithm-on-vector-drawable-image-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!