简体   繁体   English

Android中的Android拖放/旋转位图

[英]Android drag and drop / rotate bitmap in Canvas

I'm trying to find a way to implement Drag And Drop functionality and rotating a Bitmap in my Android application. 我正在尝试找到一种方法来实现拖放功能并在我的Android应用程序中旋转位图。 I want to be able to drag an image on canvas and rotate it. 我希望能够在画布上拖动图像并旋转它。 Here is how I'm adding bitmap to my canvas : 这是我在我的画布上添加位图的方法:

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

where x & y are equals to event.getX(); / event.getY(); 其中x & y等于event.getX(); / event.getY(); event.getX(); / event.getY(); .

Any ideas how can I achieve this? 任何想法我怎样才能做到这一点?

Thanks in advance! 提前致谢! : ) :)

Here is my solution, maybe it is not very cool, so if somebody have suggests 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 it is not copy and paste code. Ofc它不是复制和粘贴代码。 You need to add some your implementation to it. 您需要向其添加一些实现。

In main class (fragment or activity) you can init that code like this: 在主类(片段或活动)中,您可以像这样初始化该代码:

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;
        }
    });

It solves only drag and drop problem, not rotating. 它只解决拖放问题,而不是旋转。 But the main idea would work even for rotation. 但主要思想甚至可以用于轮换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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