Android drag / rotate bitmap in canvas

I am trying to find a way to implement the Drag And Drop function and rotate a bitmap in an Android app. I want to be able to drag the image onto the canvas and rotate it. Here's how I add a bitmap to my canvas:

Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.dog); mCanvas.drawBitmap(bm,x-100,y-100 , mPaint); 

where x & y are equal to event.getX(); / event.getY(); event.getX(); / event.getY(); .

Any ideas how I can achieve this?

Thanks in advance!:)

+4
source share
1 answer

Here is my solution, maybe this is not very cool, so if someone offers to share it with me.

 public class BitmapDragAndDrop { BitmapDragAndDropListener listener; public BitmapDragAndDrop(BitmapDragAndDropListener listener) { this.listener = listener; } public boolean onTouch(View view, MotionEvent motionEvent) { switch (motionEvent.getAction()) { case MotionEvent.ACTION_DOWN: listener.onDragStart(motionEvent.getX(), motionEvent.getY()); view.invalidate(); return true; case MotionEvent.ACTION_MOVE: listener.onDragging(motionEvent.getX(), motionEvent.getY()); view.invalidate(); return true; case MotionEvent.ACTION_UP: listener.onDragEnd(listener.onDrop(motionEvent.getX(), motionEvent.getY())); view.invalidate(); return true; } return false; } public interface BitmapDragAndDropListener { void onDragStart(float x, float y); void onDragging(float x, float y); boolean onDrop(float x, float y); void onDragEnd(boolean isEntered); } } public class YourDrawer implements BitmapDragAndDrop.BitmapDragAndDropListener { Bitmap originalImage; //for restore initial state Bitmap drawingImage; //bitmap for drawing states. Canvas imageCanvas; Bitmap objectBitmap; public YourDrawer(Bitmap imageBmp, Bitmap objectBmp) { this.originalImage = imageBmp.copy(Bitmap.Config.RGB_565, true); this.drawingImage = imageBmp.copy(Bitmap.Config.ARGB_8888, true); this.imageCanvas = new Canvas(drawingImage); this.objectBitmap = objectBmp; //Draw your object at standard place where you need drawObject(); } private void restoreImageToOriginal() { imageCanvas.drawBitmap(originalImage, 0, 0, null); } @Override public void onDragStart(float x, float y) { //do whatever you want } @Override public void onDragging(float x, float y) { restoreImageToOriginal(); //Draw bitmap object at new coordinates drawMyObject(x, y); } } @Override public boolean onDrop(float x, float y) { if (isRightPlace(x, y)) { return true; } else { return false; } } @Override public void onDragEnd(boolean isEntered) { restoreImageToOriginal(); if (isEntered) { drawMyObjectAtLastCoordinates(); } } public Bitmap getDrawingBitmap() { return drawingImage; } } 

Ofc is not to copy and paste the code. You need to add your implementation to it.

In the main class (fragment or activity), you can run this code as follows:

 YourDrawer yourDrawer = new YourDrawer(originalImage, objectBitmap); onTouchListener = new BitmapDragAndDrop(yourDrawer); imageView.setImageBitmap(yourDrawer.getDrawingBitmap())); imageView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { if (onTouchListener != null) onTouchListener.onTouch(view, motionEvent); return false; } }); 

Solves only the drag problem, not rotation. But the basic idea will work even for rotation.

0
source

Source: https://habr.com/ru/post/1410836/


All Articles