Crop and scale the image without switching to Overlay View Instead of moving the image in ImageView

Hi, I want to crop the image without moving.  enter image description here

The image can be moved around. I would like to crop and save the selection as shown below, inside the black border.

I have ImageViewwithandroid:scaleType="matrix"

Here is my layout code

<RelativeLayout
    android:id="@+imageEdit/rl_ScaleImage"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+imageEdit/Topbar"
    android:visibility="visible" >

    <ImageView
        android:id="@+imageEdit/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:scaleType="matrix" />

    <View
        android:id="@+imageEdit/viewTop"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_alignParentTop="true"
        android:background="#99000000" />

    <View
        android:id="@+imageEdit/view_scaledImage"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+imageEdit/viewBottom"
        android:layout_below="@+imageEdit/viewTop"
        android:background="@drawable/rounded_corner_small" />

    <View
        android:id="@+imageEdit/viewBottom"
        android:layout_width="fill_parent"
        android:layout_height="150dp"
        android:layout_alignParentBottom="true"
        android:background="#99000000" />
</RelativeLayout>

ImageViewhas OnTouchListener. The image can be enlarged or reduced, and also moved to ImageView1. A view view_scaledImageis an area that I want to crop and save. How to do it?

+4
source share
4 answers

Finally, I got the solution by combining Cropper libraries and PhotoView libraries.

https://github.com/rameshkec85/AndroidCropMoveScaleImage

+3

cropimage . :

 <activity android:name="eu.janmuller.android.simplecropimage.CropImage" />

:

public void runCropImage(String path) {
    Intent intent = new Intent(this, CropImage.class);
    intent.putExtra(CropImage.IMAGE_PATH, path);
    intent.putExtra(CropImage.SCALE, true);
    intent.putExtra(CropImage.ASPECT_X, 2);//change ration here via intent
    intent.putExtra(CropImage.ASPECT_Y, 2);
    startActivityForResult(intent, REQUEST_CODE_CROP_IMAGE);//final static int 1
}

onActivityResult:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    // gallery and camera ommitted
    case REQUEST_CODE_CROP_IMAGE:
        String path = data.getStringExtra(CropImage.IMAGE_PATH);
        // if nothing received
        if (path == null) {
            return;
        }
        // cropped bitmap
         Bitmap bitmap = BitmapFactory.decodeFile(path);
        imageView.setImageBitmap(bitmap);
        break;
    default:
        break;
    }
}

.

0

:

public Rect getViewPositionRelative(View v) {
   //Locate on screen
   int[] location = new int[2];
   view.getLocationInWindow(location); //change to getLocationOnScreen(location) for an absolute positioning

   Rect rect = new Rect();
   rect.left = location[0];
   rect.top = location[1];
   rect.right = rect.left + v.getWidth();
   rect.bottom = rect.top + v.getHeight();
   return rect;
}

, ImageView, Overlay View, , ;)

0

You need to combine the Cropper library and PhotoView. I created the same for my project, adding functions to select an image from a camera or gallery. Code exchange on Github. Added apk file in the project. Use a real device to test the camera, since the emulator does not work well with the camera. Here is a link to my project.

https://github.com/ozeetee/AndroidImageZoomCrop

0
source

All Articles