5

我正在尝试找到一种方法来实现拖放功能并在我的 Android 应用程序中旋转位图。我希望能够在画布上拖动图像并旋转它。这是我将位图添加到画布的方法:

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

哪里x & y等于event.getX(); / event.getY();

任何想法我怎样才能做到这一点?

提前致谢!:)

4

1 回答 1

0

这是我的解决方案,可能不是很酷,所以如果有人建议与我分享。

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 不是复制和粘贴代码。您需要向其中添加一些实现。

在主类(片段或活动)中,您可以像这样初始化该代码:

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

它只解决拖放问题,不解决旋转问题。但主要思想甚至适用于轮换。

于 2016-08-31T10:51:29.170 回答