如何使下载的图像适合图像按钮?

时间:2017-12-29 23:24:22

标签: android imagebutton android-image android-imagebutton android-crop

我正在使用Android-Image-Cropper library。它似乎与使用相机本身拍摄的照片很好地工作,但是当我从画廊中选择从互联网下载并裁剪它们的图像时,图像并不完全覆盖ImageButton。

Here is a picture of it not working (downloaded)

Here is one that is working (taken with camera)

以下是我的活动代码:

public void setProfilePicture(View view) {
CropImage.startPickImageActivity(this);
}

@Override
@SuppressLint("NewApi")
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // handle result of pick image chooser
    if (requestCode == CropImage.PICK_IMAGE_CHOOSER_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
        Uri imageUri = CropImage.getPickImageResultUri(this, data);

        // For API >= 23 we need to check specifically that we have permissions to read external storage.
        if (CropImage.isReadExternalStoragePermissionsRequired(this, imageUri)) {
            // request permissions and handle the result in onRequestPermissionsResult()
            mCropImageUri = imageUri;
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},   CropImage.PICK_IMAGE_PERMISSIONS_REQUEST_CODE);
        } else {
            // no permissions required or already granted, can start crop image activity
            startCropImageActivity(imageUri);
        }
    }

    if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
        CropImage.ActivityResult result = CropImage.getActivityResult(data);
        if (resultCode == RESULT_OK) {
            Uri resultUri = result.getUri();
            profilepicture.setImageURI(resultUri);
        } else if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
            Exception e = result.getError();
        }
    }
}

public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    if (requestCode == CropImage.PICK_IMAGE_PERMISSIONS_REQUEST_CODE) {
        if (mCropImageUri != null && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // required permissions granted, start crop image activity
            startCropImageActivity(mCropImageUri);
        } else {
            Toast.makeText(this, "Cancelling, required permissions are not granted", Toast.LENGTH_LONG).show();
        }
    }
}

private void startCropImageActivity(Uri imageUri) {
    CropImage.activity(imageUri)
            .setGuidelines(CropImageView.Guidelines.ON)
            .start(this);

}

}

1 个答案:

答案 0 :(得分:0)

哦等着它搞清楚了。

我必须在我的XML和

中设置android:scaleType="fitCenter"
 .setAspectRatio(1, 1) 
 .setFixAspectRatio(true)

到我的startCropImageActivity方法。