简体   繁体   English

对其进行修改后,如何使ImageView中包含的图像居中?

[英]How to center image contained in ImageView after a modify on it?

I've written a little piece of code, Thanks to stackoverflows users, where I fill with some color different regions of an image using the "Flood fill" algorithm. 感谢stackoverflows用户,我编写了一些代码,在这里我使用“洪水填充”算法在图像的不同区域填充了一些颜色。 The problem is that the result of this modify isn't placed in the center of the screen. 问题在于此修改的结果未放在屏幕中央。 See below: 见下文:

青羊

I suppose that this happens because I don't set the View's paramnetres correctly, as you can see in the code below: 我想发生这种情况是因为我没有正确设置View的参数,如下面的代码所示:

import java.util.LinkedList;
import java.util.Queue;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

public class Main extends Activity {

 // private RelativeLayout drawingLayout;
    private MyView myView;
 // public ImageView image;

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

     //     setContentView(R.layout.activity_main);
     //     this.drawingLayout = (RelativeLayout) findViewById(R.id.drawingLayout);

     //     image = (ImageView) findViewById(R.id.immagine);

            this.myView = new MyView(Main.this);
            setContentView(myView);
    //      this.drawingLayout.addView(myView);
 }

public class MyView extends View {

     private Paint paint;
    private Path path;
    Bitmap mBitmap;
    ProgressDialog pd;
    final Point p1 = new Point();
    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.leone).copy(Bitmap.Config.ARGB_8888, true);
         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();
    }

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

Is that possible to set the ImageView as editable, so that I can color it with Flood Fill algorithm, instead of the Main's View? 是否可以将ImageView设置为可编辑状态,以便我可以使用Flood Fill算法(而不是Main视图)对其进行着色?

Can you tell me how to set this image to center of the screen and resize it properly? 您能告诉我如何将此图像设置为屏幕中心并正确调整其大小吗?

Thank you for your help! 谢谢您的帮助!

1. Math 1.数学

You need to get screen dimensions using the following and adjust the onTouchEvent method if I'm not wrong to account for different dimensions. 如果我没错要考虑其他尺寸,则需要使用以下方法获取屏幕尺寸并调整onTouchEvent方法。

If you want the the display dimensions in pixels you can use getSize : 如果要以像素为单位显示尺寸,可以使用getSize

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

If you're not in an Activity you can get the default Display via WINDOW_SERVICE : 如果您不在Activity ,则可以通过WINDOW_SERVICE获取默认的Display

WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();

Before getSize was introduced (in API level 13), you could use the getWidth and getHeight methods that are now deprecated: 在引入getSize之前(在API级别13中),可以使用现已弃用的getWidthgetHeight方法:

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();  // deprecated
int height = display.getHeight();  // deprecated

2. Scaletype. 2.比例类型。

Extend an ImageView and use this attribute. 扩展ImageView并使用此属性。

if imgView is the image view you want to center. 如果imgView是您要居中的图像视图。 Then do: 然后做:

imgView.setScaleType(ImageView.ScaleType.CENTER);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM