Flood fill algorithm on vector drawable image view

2019-06-07 01:51发布

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 enter image description here

After flood fill of vector imageview enter image description here

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

@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?

0条回答
登录 后发表回答