简体   繁体   中英

Not able to Edit a cropped photo in Android

I'm building an Android app where I have to edit a picture which has been picked from the phone gallery and cropped.

So in my layout, I have an ImageView where I am placing the cropped image

xml file

<ImageView
    android:id="@+id/ivEditPhoto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:layout_weight=".90"
    android:src="@drawable/app_icon_big" />

Placing the cropped image in ImaveView

bitmapPic = (Bitmap) getIntent().getParcelableExtra(
            "CroppedBitmapImage");
picView = (ImageView) findViewById(R.id.ivEditPhoto);
        picView.setImageBitmap(bitmapPic);

The image is getting placed correctly. But the problem is when I try to edit it. I have an edit button and on click of that I do the following which includes registering of On Touch Listener.

DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        int height = displaymetrics.heightPixels;
        int width = displaymetrics.widthPixels;
        Bitmap alteredPastedBitmap = Bitmap.createBitmap(bitmapPic.getWidth(),
                bitmapPic.getHeight(), bitmapPic.getConfig());
        pasteCanvas = new Canvas(alteredPastedBitmap);
        paint = new Paint();
        paint.setColor(Color.GREEN);
        paint.setStrokeWidth(5);
        matrix = new Matrix();
        pasteCanvas.drawBitmap(bitmapPic, matrix, paint);
        picView.setOnTouchListener(this);

Then the following

public boolean onTouch(View v, MotionEvent event) {
    Toast.makeText(this, v.getId(), Toast.LENGTH_SHORT).show();
    if (v.getId() == R.id.ivEditPhoto) {

        Toast.makeText(this, "onTouch", Toast.LENGTH_SHORT).show();
        int action = event.getAction();
        x = event.getX();
        y = event.getY();
        switch (action) {
        case MotionEvent.ACTION_DOWN:
            sX = event.getX();
            sY = event.getY();
            break;
        case MotionEvent.ACTION_UP:
            if (skewedBitmap == null) {
                int resID = 0;
                if (imageId == 0)
                    resID = R.drawable.green_arrow;
                else
                    resID = imageId;
                bitmapToPaste = BitmapFactory.decodeResource(
                        getResources(), resID);
            } else {
                bitmapToPaste = skewedBitmap;
                skewedBitmap = null;
            }
            pasteCanvas.drawBitmap(bitmapToPaste, sX- (bitmapToPaste.getWidth() / 2),sY- (bitmapToPaste.getHeight() / 2), null);
            picView.invalidate();
            break;
        case MotionEvent.ACTION_CANCEL:
            break;
        default:
            break;
        }
    }
    return true;
}

The issue is: the bitmapPic.getWidth() and bitmapPic.getHeight() is 160*160 and the onTouch event.getX(); and event.getY(); is beyond the co-ordinates (For example: 150.33 & 500.89) although the image seems to be occupying the whole screen and the touch is on the image. So onTouch doesn't place the bitmap and returns a false. Could any of you please guide me on this?

I had the same issue in one of my applications. I need to see more of your code but this could be related to the onInterceptTouch() method you need to implement.

If that is not the case, then maybe you can look into this code for help on how to move, modify and rotate bitmaps:

http://adblogcat.com/custom-view-to-draw-rotate-erase-and-convert-images-to-black-and-white/

I wrote this tutorial btw and this is my website and I think is easier for you to follow that tutorial than just paste it here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM