Custom Android Image Crop

拜拜、爱过 提交于 2019-11-28 03:52:03

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.)

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

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!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!