Android中的Android拖放/旋转位图

时间:2012-05-05 10:21:56

标签: android drag-and-drop bitmap android-canvas

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

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

其中x & y等于event.getX(); / event.getY();

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

提前致谢! :)

1 个答案:

答案 0 :(得分: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;
        }
    });

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