Custom Android Image Crop

后端 未结 3 960
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-07 17:28

i just want to share this piece of code that i wrote. I tried searching for a custom crop activity, but most of them leads to the default \"com.android.camera.action.CROP\"

相关标签:
3条回答
  • 2020-12-07 17:39

    You should try:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.crop_layout);
    
    
        myCropView = new CropView(this);
    
        Uri imageUri = getIntent().getExtras().getParcelable("path");
    
        b = (BitmapDrawable) BitmapDrawable.createFromPath(imageUri.getPath()); 
    
        myCropView.setImageURI(imageUri);
    }
    

    (Taken from an edit to your question.)

    0 讨论(0)
  • 2020-12-07 17:54

    I found a library which supports this: SimpleCropView from https://android-arsenal.com/details/1/2366. In general, I would not recommend it, its performance is far from the native android cropping apps.

    I've tried using it, and my thoughts are:

    • Really simple to implement in your app, took me about 5 minutes to get crop and rotation functionality working with my existing app

    • The re-sizing of the crop area is painfully slow, I wouldn't like my user to see it.

    UPDATE: In fact, I've found a really good solution as jdamcd/android-crop on Github: https://github.com/jdamcd/android-crop Summary:

    • Very simple to use in your app

    • Fast, since it uses the code from native gallery app

    • Customizable, if you want to spend some time playing with it. By default, it provides you with an Activity where you do the cropping. If you want to integrate it into your own activity, it will take a bit mor time. (For my project I would have liked to integrate it, and will do in the future, but for now a separate activity is enough).

    Hope this provides some insight!

    0 讨论(0)
  • 2020-12-07 17:56

    thanks thehippo... but i've solved finding the view by the layout

    Uri imageUri = getIntent().getExtras().getParcelable("path");
        b = (BitmapDrawable) BitmapDrawable.createFromPath(getRealPathFromURI(imageUri));
        myCropView = (CropView) findViewById(R.id.image_preview);
        myCropView.setBackground(b);
    

    but now i can't handle the touch event. The rectangle stay still even if i touch the screen...

    EDIT: ok, i've made it works. But now, the rectangle move only inside a smaller area, not in the entire image. I suppose that there's something wrong here

    private boolean isInImageRange(PointF point) {
        // Get image matrix values and place them in an array
        float[] f = new float[9];
        getImageMatrix().getValues(f);
    
        // Calculate the scaled dimensions
        imageScaledWidth = Math.round(getBackground().getIntrinsicWidth() * f[Matrix.MSCALE_X]);
        imageScaledHeight = Math.round(getBackground().getIntrinsicHeight() * f[Matrix.MSCALE_Y]);
    
        return (point.x>=(center.x-(imageScaledWidth/2))&&point.x<=(center.x+(imageScaledWidth/2))&&point.y>=(center.y-(imageScaledHeight/2))&&point.y<=(center.y+(imageScaledHeight/2)))?true:false;
    }
    

    i've done a little change to make work the code: getBackground() instead of getDrawable

    EDIT 2: OK ive got it, i was doing this in the wrong way. You're code is good. To set the image i was using the view.seBackground()...instead of view.setImageDrawable(). Now everything works. Maybe i will only check if it's possible to create a larger area that fires the scaling of the rectangle

    0 讨论(0)
提交回复
热议问题