Android - 通过拖放调整大小/缩放图像

时间:2014-04-27 07:17:03

标签: java android image

在Android中,是否有任何内置方法可以通过选择图像来调整图像大小,然后将边角/边拖动到所需的尺寸?

说明我希望实现的目标:

enter image description here enter image description here

1 个答案:

答案 0 :(得分:0)

我认为你要找的是裁剪图像。

你可以通过默认的android Crop功能来实现:

private void performCrop(Uri picUri) {
try {

    Intent cropIntent = new Intent("com.android.camera.action.CROP");
    // indicate image type and Uri
    cropIntent.setDataAndType(picUri, "image/*");
    // set crop properties
    cropIntent.putExtra("crop", "true");
    // indicate aspect of desired crop
    cropIntent.putExtra("aspectX", 1);
    cropIntent.putExtra("aspectY", 1);
    // indicate output X and Y
    cropIntent.putExtra("outputX", 128);
    cropIntent.putExtra("outputY", 128);
    // retrieve data on return
    cropIntent.putExtra("return-data", true);
    // start the activity - we handle returning in onActivityResult
    startActivityForResult(cropIntent, PIC_CROP);
}
// respond to users whose devices do not support the crop action
catch (ActivityNotFoundException anfe) {
    // display an error message
    String errorMessage = "Whoops - your device doesn't support the crop action!";
    Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
    toast.show();
}
}

常数声明:

 final int PIC_CROP = 1;

覆盖活动中的onActivity结果方法,编写以下代码:

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

    if (requestCode == PIC_CROP) {
        if (data != null) {
            // get the returned data
            Bundle extras = data.getExtras();
            // get the cropped bitmap
            Bitmap selectedBitmap = extras.getParcelable("data");

            imgView.setImageBitmap(selectedBitmap);
        }
    }

}