gpt4 book ai didi

java - 在 onActivityResult 中调整 imageView 中的图像大小并重新绘制图像

转载 作者:行者123 更新时间:2023-12-01 09:06:18 25 4
gpt4 key购买 nike

出于某种原因,我一生都无法弄清楚为什么会发生这种情况,但我有一个 imageView,我可以在其中使用矩阵实用地调整图像大小,但它不会显示更改,直到我将它们重新应用到onTouch 事件。

因此,此 Activity 的功能是立即打开图库选择器,然后将结果显示在 imageView 中,您可以在其中平移图像并调整图像大小。然后绘图缓存以位图的形式保存到存储中。

我实用地调整了 ImageView 的大小,使其成为正方形,因此我必须调整图像的大小以匹配新的尺寸。但它始终显示为中心,直到我点击屏幕。

这是我的代码,请帮助我

public class PickPic extends Activity {

private static final String D = "PickPic";
float scaleAmount = 1;
//this view determines what part of the image will be kept
ImageView cropView;
Bitmap b;
ScaleGestureDetector scaleD;
//for touch event handling
float initX,initY;
//cropview image matrix
Matrix matC;
int resultCode;

float scale;
//cropview image matrix translation values
float cropTransX, cropTransY;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pick_pic);

resultCode = getIntent().getIntExtra("result code", MainActivity.PICK_PIC_4_PIC_ONE);

scaleD = new ScaleGestureDetector(this, new ScaleListener());

Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(intent, 1);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

cropView = (ImageView) findViewById(R.id.editor);
cropView.setDrawingCacheEnabled(true);
//make crop view square
cropView.getLayoutParams().height = cropView.getWidth();
cropView.requestLayout();

if (resultCode == RESULT_OK) {
try {
b = MediaStore.Images.Media.getBitmap(this.getContentResolver(), data.getData());
cropView.setImageBitmap(b);
} catch (IOException e) {
e.printStackTrace();
}

}
setValues();
}

private void setValues() {
matC = cropView.getImageMatrix();
//scale crop to fit
matC = scale(matC,b.getWidth(),b.getHeight(),cropView.getWidth(),cropView.getWidth());
float[] matCValues = new float[9];
matC.getValues(matCValues);
//transformation values to be kept for onTouch methods
scale = matCValues[Matrix.MSCALE_X];
cropTransX = matCValues[Matrix.MTRANS_X];
cropTransY = matCValues[Matrix.MTRANS_Y];
cropView.setImageMatrix(matC);
cropView.invalidate();
//here the values get set properly but the display doesn't change
}

private Matrix scale(Matrix mat, float bwidth, float bheight, float vwidth, float vheight) {

float scale;
float dx = 0, dy = 0;

if (bwidth * vheight > vwidth * bheight) {
scale = vheight / bheight;
dx = (vwidth - bwidth * scale) * 0.5f;
} else {
scale = vwidth / bwidth;
dy = (vheight - bheight * scale) * 0.5f;
}

mat.setTranslate(Math.round(dx), Math.round(dy));
mat.preScale(scale, scale);
//mat.setScale(scale, scale);
//mat.postTranslate(Math.round(dx), Math.round(dy));

return mat;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
scaleD.onTouchEvent(event);

switch (MotionEventCompat.getActionMasked(event)){
case (MotionEvent.ACTION_DOWN):
initX = event.getX();
initY = event.getY();
return true;
case (MotionEvent.ACTION_MOVE):
//this is where the display gets updated
matC.setTranslate(cropTransX + event.getX() - initX, cropTransY + event.getY() - initY);
matC.preScale(scale*scaleAmount, scale*scaleAmount);
cropView.setImageMatrix(matC);
cropView.invalidate();
matB.setTranslate(backTransX + event.getX() - initX, backTransY + event.getY() - initY);
matB.preScale(scale*scaleAmount, scale*scaleAmount);
return true;
case (MotionEvent.ACTION_UP):
//commit transformations
cropTransX += event.getX() - initX;
cropTransY += event.getY() - initY;

backTransX += event.getX() - initX;
backTransY += event.getY() - initY;
return true;
default:
return super.onTouchEvent(event);
}
}

private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
scaleAmount *= detector.getScaleFactor();
return true;
}
}
}

最佳答案

在 onActivityResult() 中调用 setValues() 方法时进行一些延迟,如下所示。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

cropView = (ImageView) findViewById(R.id.editor);
cropView.setDrawingCacheEnabled(true);
//make crop view square
cropView.getLayoutParams().height = cropView.getWidth();
cropView.requestLayout();

if (resultCode == RESULT_OK) {
try {
b = MediaStore.Images.Media.getBitmap(this.getContentResolver(), data.getData());
cropView.setImageBitmap(b);
} catch (IOException e) {
e.printStackTrace();
}

}

new Handler().postDelayed(new Runnable() {
@Override
public void run() {
setValues();
}
},1500);

}

如果不起作用,请告诉我结果。

关于java - 在 onActivityResult 中调整 imageView 中的图像大小并重新绘制图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41254408/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com