有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在这个洪水填充算法代码和自定义视图中调整位图图像的大小

我是编程新手,我正在尝试实现一种泛光填充算法,将图像中选定的部分着色为配色系统应用程序。 我发现这个link为算法提供了完整的工作代码(但速度较慢)。并且还提供了更快的队列线路地板填充器。 问题是,自定义MyView类被添加到Relativelayout中,以显示将着色的位图,但图像不在视图中,如下所示

我想将其包裹以填充视图,并将其大小与包裹内容中的大小相同

enter image description here

我尝试使用此代码调整其大小

dashBoard = (RelativeLayout) findViewById(R.id.dashBoard);
    int width = RelativeLayout.LayoutParams.WRAP_CONTENT;
    int hight = RelativeLayout.LayoutParams.WRAP_CONTENT;
    myView.setLayoutParams(new RelativeLayout.LayoutParams(width,hight));
    dashBoard.addView(myView);

但它不起作用

下面是完整的代码和XML

public class UnicornColorActivity extends AppCompatActivity {

private RelativeLayout dashBoard;
private MyView myView;
public ImageView image;

Button b_red, b_blue, b_green, b_orange, b_clear;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    myView = new MyView(this);

    setContentView(R.layout.activity_unicorn_color);
    findViewById(R.id.dashBoard);

    b_red = (Button) findViewById(R.id.b_red);
    b_blue = (Button) findViewById(R.id.b_blue);
    b_green = (Button) findViewById(R.id.b_green);
    b_orange = (Button) findViewById(R.id.b_orange);

    b_red.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            myView.changePaintColor(0xFFFF0000);
        }
    });

    b_blue.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            myView.changePaintColor(0xFF0000FF);
        }
    });

    b_green.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            myView.changePaintColor(0xFF00FF00);
        }
    });

    b_orange.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            myView.changePaintColor(0xFFFF9900);
        }
    });

    dashBoard = (RelativeLayout) findViewById(R.id.dashBoard);
    int width = RelativeLayout.LayoutParams.WRAP_CONTENT;
    int hight = RelativeLayout.LayoutParams.WRAP_CONTENT;
    myView.setLayoutParams(new RelativeLayout.LayoutParams(width,hight));
    dashBoard.addView(myView);
}

public class MyView extends View {

    private Paint paint;
    private Path path;
    public Bitmap mBitmap;
    public ProgressDialog pd;
    final Point p1 = new Point();
    public Canvas canvas;

    //Bitmap mutableBitmap ;
    public MyView(Context context) {

        super(context);

        this.paint = new Paint();
        this.paint.setAntiAlias(true);
        pd = new ProgressDialog(context);
        this.paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeWidth(5f);
        mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.unicorn_2).copy(Bitmap.Config.ARGB_8888, true);
        //Bitmap.createScaledBitmap(mBitmap, 60, 60 , false);
        this.path = new Path();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        this.canvas = canvas;
        this.paint.setColor(Color.RED);

        canvas.drawBitmap(mBitmap, 0, 0, paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        float x = event.getX();
        float y = event.getY();

        switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:

                p1.x = (int) x;
                p1.y = (int) y;
                final int sourceColor = mBitmap.getPixel((int) x, (int) y);
                final int targetColor = paint.getColor();
                new TheTask(mBitmap, p1, sourceColor, targetColor).execute();
                invalidate();
        }
        return true;
    }

    public void clear() {
        path.reset();
        invalidate();
    }

    public int getCurrentPaintColor() {
        return paint.getColor();
    }

    public void changePaintColor(int color){
        this.paint.setColor(color);
    }

    class TheTask extends AsyncTask<Void, Integer, Void> {

        Bitmap bmp;
        Point pt;
        int replacementColor, targetColor;

        public TheTask(Bitmap bm, Point p, int sc, int tc) {
            this.bmp = bm;
            this.pt = p;
            this.replacementColor = tc;
            this.targetColor = sc;
            pd.setMessage("Filling....");
            pd.show();
        }

        @Override
        protected void onPreExecute() {
            pd.show();

        }

        @Override
        protected void onProgressUpdate(Integer... values) {

        }

        @Override
        protected Void doInBackground(Void... params) {
            FloodFill f = new FloodFill();
            f.floodFill(bmp, pt, targetColor, replacementColor);
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            pd.dismiss();
            invalidate();
        }
    }
}

// flood fill
public class FloodFill {

    public void floodFill(Bitmap image, Point node, int targetColor, int replacementColor) {

        int width = image.getWidth();
        int height = image.getHeight();
        int target = targetColor;
        int replacement = replacementColor;

        if (target != replacement) {
            Queue<Point> queue = new LinkedList<Point>();
            do {

                int x = node.x;
                int y = node.y;
                while (x > 0 && image.getPixel(x - 1, y) == target) {
                    x--;
                }

                boolean spanUp = false;
                boolean spanDown = false;
                while (x < width && image.getPixel(x, y) == target) {
                    image.setPixel(x, y, replacement);
                    if (!spanUp && y > 0 && image.getPixel(x, y - 1) == target) {
                        queue.add(new Point(x, y - 1));
                        spanUp = true;
                    } else if (spanUp && y > 0 && image.getPixel(x, y - 1) != target) {
                        spanUp = false;
                    }
                    if (!spanDown && y < height - 1 && image.getPixel(x, y + 1) == target) {
                        queue.add(new Point(x, y + 1));
                        spanDown = true;
                    } else if (spanDown && y < (height - 1) && image.getPixel(x, y + 1) != target) {
                        spanDown = false;
                    }
                    x++;
                }

            } while ((node = queue.poll()) != null);
        }
    }
}

}

<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:id="@+id/drawingLayout"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
 >
<安卓.support.v7.widget.CardView
    安卓:id="@+id/cardView"
    安卓:layout_width="match_parent"
    安卓:layout_height="542dp"
    安卓:layout_gravity="center"
    安卓:layout_marginTop="8dp"
    安卓:layout_marginRight="8dp"
    安卓:layout_marginLeft="8dp"
    安卓:layout_marginBottom="8dp"
    安卓:background="?安卓:attr/selectableItemBackground"
    app:cardBackgroundColor="#FFFFFF"
    app:cardCornerRadius="16dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

<RelativeLayout
    安卓:id="@+id/dashBoard"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:layout_above="@+id/b_red"
    安卓:layout_alignParentLeft="true"
    安卓:layout_alignParentRight="true"
    安卓:layout_alignParentTop="true"
    安卓:layout_marginBottom="10dp" >

</RelativeLayout>
</安卓.support.v7.widget.CardView>

<Button
    安卓:id="@+id/b_red"
    安卓:layout_width="65dp"
    安卓:layout_height="40dp"
    安卓:layout_alignParentBottom="true"
    安卓:layout_alignParentLeft="true"
    安卓:background="#FF0000" />

<Button
    安卓:id="@+id/b_green"
    安卓:layout_width="65dp"
    安卓:layout_height="40dp"
    安卓:layout_alignParentBottom="true"
    安卓:layout_toRightOf="@+id/b_red"
    安卓:background="#00FF00" />

<Button
    安卓:id="@+id/b_blue"
    安卓:layout_width="65dp"
    安卓:layout_height="40dp"
    安卓:layout_alignParentBottom="true"
    安卓:layout_toRightOf="@+id/b_green"
    安卓:background="#0000FF" />

<Button
    安卓:id="@+id/b_orange"
    安卓:layout_width="65dp"
    安卓:layout_height="40dp"
    安卓:layout_alignParentBottom="true"
    安卓:layout_toRightOf="@+id/b_blue"
    安卓:background="#FF9900" />

<Button
    安卓:id="@+id/button5"
    安卓:layout_width="60dp"
    安卓:layout_height="40dp"
    安卓:layout_alignParentBottom="true"
    安卓:layout_alignParentRight="true"
    安卓:text="Clear" />

有人能帮我修一下吗


共 (0) 个答案