SoFunction
Updated on 2025-03-11

Android ImageView implements image cropping and display functions

First set the button and an ImageView in the layout layout

<Button
  android:
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:text="Select Picture" />
 <Button
  android:
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:text="Select an image to crop" />
 <!-- Information used to display pictures -->
 <ImageView
  android:
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" />

Write code on Activity

public class MainActivity extends AppCompatActivity implements  {
 private Button selectImageBtn, cutImageBtn;
 private ImageView imageView;
 // Declare two static integer variables, mainly used for the return flag of the intention private static final int IMAGE_SELECT = 1;// Select a picture private static final int IMAGE_CUT = 2;// Crop the picture  @Override
 protected void onCreate(Bundle savedInstanceState) {
  (savedInstanceState);
  setContentView(.activity_main);
  selectImageBtn = (Button) findViewById();
  cutImageBtn = (Button) findViewById();
  imageView = (ImageView) findViewById();
  // Register to listen to events  (this);
  (this);
  }

Implement the OnClickListener method and set the cropping method

@Override
 public void onClick(View v) {
  switch (()) {
   case :
    //How to extract the picture library of your mobile phone and select pictures    Intent intent = new Intent(Intent.ACTION_PICK, .EXTERNAL_CONTENT_URI);//Open the picture library of your phone    startActivityForResult(intent, IMAGE_SELECT);
    break;
   case :
    Intent intent2 = getImageClipIntent();
    startActivityForResult(intent2, IMAGE_CUT);
  }
 }
private Intent getImageClipIntent() {
  Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);//Do not specify the URL  //To achieve cropping of the picture, the attributes and size of the picture must be set.  ("image/*");//Get any image type Set an explicit MIME data type. Each MIME type consists of two parts. The first is the major category of data, such as sound audio, image image, etc., and the specific type is defined later.  ("crop", "true");//Slide to select the picture area  ("aspectX", 1);//Denotes the effect of 1:1 ratio of the cut box  ("aspectY", 1);
  ("outputX", 80);//Specify the size of the output image  ("outputY", 80);
  ("return-data", true);//There is a return value  return intent;
 }

If you want to get the data returned after the newly opened Activity is closed in the Activity, you need to use the startActivityForResult(Intent intent,int requestCode) method provided by the system to open the new Activity. After the new Activity is closed, data will be returned to the previous Activity. In order to get the returned data, you must rewrite the onActivityResult(int requestCode, int resultCode,Intent data) method in the previous Activity

When the new Activity is closed, the data returned by the new Activity is passed through the Intent. The Android platform will call the onActivityResult() method of the previous Activity, and pass the Intent that stores the returned data as the third input parameter. In the onActivityResult() method, the data returned by the new Activity can be retrieved.

If you need to return data or results, usestartActivityForResult (Intent intent, intrequestCode), the value of requestCode is customized to identify the target activity of the jump.

Override the onActivityResult method

@Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  (requestCode, resultCode, data);
  if (resultCode == RESULT_OK) {
   //Processing pictures are displayed according to the screen size of the phone   if (requestCode == IMAGE_SELECT) {
    Uri uri = ();//The path to get the picture    Display display = getWindowManager().getDefaultDisplay();
    Point point = new Point();
    (point);
    int width = ;//Get the width of the screen    int height =  ;//Screen height    try {
     //The class that implements the cropping of the image is an anonymous internal class      options = new ();
      = false;
     //Match the width and height of the picture corresponding to the screen of the phone according to the phone     Bitmap bitmap = (getContentResolver().openInputStream(uri), null, options);
     //If it is greater than 1, it means that the height of the picture is greater than the height of the mobile phone screen     int hRatio = (int) ( / (float) height);//(int) is to get the round     //If it is greater than 1, it means that the width of the picture is greater than the width of the mobile phone screen     int wRatio = (int) ( / (float) width);
     //If hRatio or wRatio is greater than 1, scale the image to the size of 1/radio and the pixels of 1/radio^2     if (hRatio > 1 || wRatio > 1) {
      if (hRatio > wRatio) {
        = hRatio;
      } else {
        = wRatio;
      }
      bitmap = (getContentResolver().openInputStream(uri), null, options);
      (bitmap);
     }else{
      //If hRatio and wRatio are 0, output directly      (bitmap);
     }
    } catch (Exception e) {
    }
    // means cropped picture   } else if (requestCode == IMAGE_CUT) {
    Bitmap bitmap = ("data");
    (bitmap);
   }
  }
 }
 = false/true;

Let's parse an image. If it is too large, it will be OOM. We can set the compression ratio inSampleSize, but the amount of this compression ratio is a problem, so we can analyze the image into two steps. The first step is to obtain the width and height of the image. Here we need to set it=true, At this time, the bitmap of the decode is null, just put the width and height of the picture in Options.

Then the second step is to set the appropriate compression ratio inSampleSize, and inSampleSize is the original 1/ratio, and then you can get the appropriate Bitmap.

Set = false; read out the picture againbitmap = (getContentResolver().openInputStream(uri), null, options);

The above is the Android ImageView that the editor introduced to you to realize the image cropping and display functions. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!