How to set photo resolution or how to change to hide the entire Imageview

Asked

Viewed 2,136 times

1

I am using the function of the android camera need to change the resolution of the image to occupy my Imageview completely

code used

public void onClickCamera(View v){

    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraIntent, CAMERA_REQUEST);
}


public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
        Bitmap photo = (Bitmap)data.getExtras().get("data");
        imageView.setImageBitmap(photo);
        imageView2.setImageBitmap(photo);       
}

some tip or a better way to use and get the expected result or how instagram does

1 answer

1


If it is only to visually shrink the image, you can change the Imageview Scaletype property to ImageView.ScaleType.CENTER_INSIDE, so that Imageview will resize the image proportionally when drawing it:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
        Bitmap photo = (Bitmap)data.getExtras().get("data");
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        imageView.setImageBitmap(photo);
    }
}

However, this solution ends up consuming a little more processing, because the image is resized every time the image needs to be drawn.

If this is a problem, it is possible to use a little more memory, and create a smaller copy of the bitmap, already with the size of Imageview (by default this operation should be performed in another Thread / Task):

Bitmap reduzido = Bitmap.createScaledBitmap(photo, LARGURA_DESEJADA, ALTURA_DESEJADA, true);
imageView.setImageBitmap(reduzido);

This will save processing, since Imageview will not need to resize the image every time, but it takes a little more memory.

  • as I would to use imageView.setScaleType(Imageview.ScaleType.CENTER_INSIDE); could give an example if possible?

  • I edited the answer with the example from its original code.

  • thanks gave to solve my problem

Browser other questions tagged

You are not signed in. Login or sign up in order to post.