[英]resize and redraw image in imageView in onActivityResult
For some reason I can't for the life of me figure out why this is happening, but I have an imageView where I resize the image pragmatically using a matrix but it won't display the changes until I reapply them in an onTouch event. 由于某种原因,我无法确定为什么会发生这种情况,但是我有一个imageView,其中我使用矩阵实用地调整了图像的大小,但是直到我在onTouch事件中重新应用更改后,它才会显示更改。
So the function of this Activity is to open a gallery picker immediately and then display the result in an imageView where you can pan and resize the image. 因此,此活动的功能是立即打开图库选择器,然后在imageView中显示结果,您可以在其中平移和调整图像的大小。 Then the drawing cache is saved to storage as a bitmap. 然后,将图形缓存作为位图保存到存储中。
I resize the ImageView pragmatically so that it's square so I have to resize the image to match the new dimensions. 我实用地调整了ImageView的大小,以便它是正方形的,因此我必须调整图像的大小以匹配新的尺寸。 But it's always displayed as just center until I tap the screen. 但是在我点击屏幕之前,它始终显示为居中。
here's my code pleas help me 这是我的代码请帮助我
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;
}
}
}
Make some delay when called setValues() method in onActivityResult() like below. 在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);
} }
let me know the result if not working. 让我知道结果,如果不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.