Java 修改后,如何将ImageView中包含的图像居中?
感谢stackoverflows用户,我已经编写了一段代码,其中我使用“泛光填充”算法填充了图像中一些不同颜色的区域。问题是,此修改的结果没有放在屏幕的中心。见下文: 我认为发生这种情况是因为我没有正确设置视图的参数,正如您在下面的代码中看到的:Java 修改后,如何将ImageView中包含的图像居中?,java,android,android-layout,android-imageview,android-view,Java,Android,Android Layout,Android Imageview,Android View,感谢stackoverflows用户,我已经编写了一段代码,其中我使用“泛光填充”算法填充了图像中一些不同颜色的区域。问题是,此修改的结果没有放在屏幕的中心。见下文: 我认为发生这种情况是因为我没有正确设置视图的参数,正如您在下面的代码中看到的: import java.util.LinkedList; import java.util.Queue; import android.app.Activity; import android.app.ProgressDialog; import
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);
}
}
}
}
import java.util.LinkedList;
导入java.util.Queue;
导入android.app.Activity;
导入android.app.ProgressDialog;
导入android.content.Context;
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.graphics.Canvas;
导入android.graphics.Color;
导入android.graphics.Paint;
导入android.graphics.Path;
导入android.graphics.Point;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.view.MotionEvent;
导入android.view.view;
公共类主要扩展活动{
//私人相对性图纸布局;
私有MyView-MyView;
//公众形象;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
//this.drawingLayout=(RelativeLayout)findViewById(R.id.drawingLayout);
//image=(ImageView)findviewbyd(R.id.immagine);
this.myView=newmyview(Main.this);
setContentView(myView);
//this.drawingLayout.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.leone).copy(Bitmap.Config.ARGB_8888,true);
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();
}
类任务扩展异步任务{
位图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;
}如果(spanUp&&y>0),则为else
&&image.getPixel(x,y-1)!=目标){
spanUp=false;
}
如果(!spanDown&&y<高度-1
&&image.getPixel(x,y+1)=目标){
添加(新点(x,y+1));
spanDown=true;
}否则,如果(向下和向下<高度-1
&&image.getPixel(x,y+1)!=目标){
spanDown=false;
}
x++;
}
}而((node=queue.poll())!=null);
}
}
}
}
是否可以将ImageView设置为可编辑,以便我可以使用泛光填充算法而不是Main的视图为其着色
您能告诉我如何将这个图像设置到屏幕中央并正确调整大小吗
谢谢你的帮助 一,。数学
您需要使用以下命令获取屏幕尺寸
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth(); // deprecated
int height = display.getHeight(); // deprecated
imgView.setScaleType(ImageView.ScaleType.CENTER);