Java 如何在此泛洪填充算法代码和自定义视图中调整位图图像的大小

Java 如何在此泛洪填充算法代码和自定义视图中调整位图图像的大小,java,android,Java,Android,我是编程新手,我正在尝试实现一种泛光填充算法,将图像中选定的部分着色为配色系统应用程序。 我发现这为算法提供了完整的工作代码(但速度较慢)。并且还提供了更快的队列线路地板填充器。 问题是,自定义MyView类被添加到Relativelayout中,以显示将着色的位图,但图像不在视图中,如下所示 我想将其包裹以填充视图,并将其大小与包裹内容中的大小相同 我尝试使用此代码调整其大小 dashBoard = (RelativeLayout) findViewById(R.id.dashBoard);

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

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

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

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);
        }
    }
}
公共类UnicornColorActivity扩展了AppCompatActivity{
私人相对仪表盘;
私有MyView-MyView;
公众形象;
按钮b_红色、b_蓝色、b_绿色、b_橙色、b_透明;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
myView=新的myView(此);
setContentView(R.layout.activity\u unicorn\u color);
findViewById(R.id.仪表板);
b_red=(按钮)findViewById(R.id.b_red);
b_蓝=(按钮)findViewById(R.id.b_蓝);
b_绿色=(按钮)findviewbyd(R.id.b_绿色);
b_orange=(按钮)findViewById(R.id.b_orange);
b_red.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
myView.changePaintColor(0xFFFF0000);
}
});
b_blue.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
myView.changePaintColor(0xFF0000FF);
}
});
b_green.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
myView.changePaintColor(0xFF00FF00);
}
});
b_orange.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
myView.changePaintColor(0xFFFF9900);
}
});
dashBoard=(RelativeLayout)findViewById(R.id.dashBoard);
int width=RelativeLayout.LayoutParams.WRAP_CONTENT;
int hight=RelativeLayout.LayoutParams.WRAP_CONTENT;
myView.setLayoutParams(新的RelativeLayout.LayoutParams(宽度、高度));
dashBoard.addView(myView);
}
公共类MyView扩展了视图{
私人油漆;
专用路径;
公共位图mBitmap;
公共发展对话;
终点p1=新点();
公共画布;
//位图可变位图;
公共MyView(上下文){
超级(上下文);
this.paint=新油漆();
this.paint.setAntiAlias(true);
pd=新进度对话框(上下文);
this.paint.setStyle(paint.Style.STROKE);
绘制.设置行程连接(绘制.连接.圆形);
油漆。设置行程宽度(5f);
mBitmap=BitmapFactory.decodeResource(getResources(),R.drawable.unicorn_2).copy(Bitmap.Config.ARGB_8888,true);
//createScaledBitmap(mBitmap,60,60,false);
this.path=新路径();
}
@凌驾
受保护的void onDraw(画布){
this.canvas=画布;
这个.paint.setColor(Color.RED);
画布.drawBitmap(mBitmap,0,0,paint);
}
@凌驾
公共布尔onTouchEvent(运动事件){
float x=event.getX();
float y=event.getY();
开关(event.getAction()){
case MotionEvent.ACTION\u DOWN:
p1.x=(int)x;
p1.y=(int)y;
最终int sourceColor=mBitmap.getPixel((int)x,(int)y);
final int targetColor=paint.getColor();
新建任务(mBitmap、p1、sourceColor、targetColor).execute();
使无效();
}
返回true;
}
公共空间清除(){
path.reset();
使无效();
}
public int getCurrentPaintColor(){
返回paint.getColor();
}
公共void changePaintColor(内部颜色){
此.paint.setColor(颜色);
}
类任务扩展异步任务{
位图bmp;
点pt;
int replacementColor,targetColor;
公共任务(位图bm、点p、点sc、点tc){
this.bmp=bm;
这个p.pt=p;
this.replacementColor=tc;
this.targetColor=sc;
pd.setMessage(“填充…”);
pd.show();
}
@凌驾
受保护的void onPreExecute(){
pd.show();
}
@凌驾
受保护的void onProgressUpdate(整型…值){
}
@凌驾
受保护的Void doInBackground(Void…参数){
溢流填料f=新溢流填料();
f、 泛光填充(bmp、pt、targetColor、replacementColor);
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
pd.解散();
使无效();
}
}
}
//填海
公共级填洪{
公共空白泛光填充(位图图像、点节点、int-targetColor、int-replacementColor){
int width=image.getWidth();
int height=image.getHeight();
int target=targetColor;
int replacement=replacementColor;
如果(目标!=更换){
Queue Queue=new LinkedList();
做{
int x=node.x;
int y=node.y;
而(x>0&&image.getPixel(x-1,y)=目标){
x--;
}
布尔spanUp=false;
布尔值=假;
而(x0&&image.getPixel(x,y-1)=目标){
添加(新点(x,y-1));
spanUp=true;
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawingLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
 >
<android.support.v7.widget.CardView
    android:id="@+id/cardView"
    android:layout_width="match_parent"
    android:layout_height="542dp"
    android:layout_gravity="center"
    android:layout_marginTop="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginBottom="8dp"
    android:background="?android:attr/selectableItemBackground"
    app:cardBackgroundColor="#FFFFFF"
    app:cardCornerRadius="16dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

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

</RelativeLayout>
</android.support.v7.widget.CardView>

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

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

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

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

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