2

我想创建这样的应用程序。

洪水填充算法

我应用了该代码,它适用于 JPG 或 PNG 文件。但我想将该算法与 Vector drawable imageview 一起使用

洪水填充前的矢量可绘制图像视图 在此处输入图像描述

矢量图像视图的洪水填充后 在此处输入图像描述

预期的结果应该是这样的(当我设置 JPG 或 PNG 文件时,填充工作完美) 在此处输入图像描述

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

检查类:QueueLinearFloodFiller

如何使用矢量绘图?

4

0 回答 0