在onActivityResult中的imageView中调整大小并重绘图像

时间:2016-12-21 02:59:11

标签: java android matrix

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

因此,此活动的功能是立即打开图库选取器,然后在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;
    }
}
}

1 个答案:

答案 0 :(得分:0)

在onActivityResult()中调用setValues()方法时会有一些延迟。

return db.Host
         .Include(l => l.RootNode)

}

如果不能正常工作,请告诉我。