1

hi everyone I am new to blackebrry. I want to use low level graphics for drawing a bitmapfield. On this bitmapfield i need to take three action-

  1. pick the bitmapfield,
  2. drag it in the screen in any direction and
  3. finally release it at any point on the screen.

how can I do this. if 'Canvas' can be a solution... can any one help me with some sample code for how to use 'Canvas'.

Also is there any other method for making a draggable bitmapfield..??

Please help

Swati
  • 1,179
  • 9
  • 28

1 Answers1

0

Firstly, you can create a bitmap and get a graphics object from it to do custom drawing on it.

Bitmap myBitmap = new Bitmap(widht, height);
Graphics graphics = Graphics.create(myBitmap);

// now you can draw anything on graphics supported by API

[Edited]

I have added a code snippet for drag an image on a screen. You can check this code if it works on your side.

class MyScreen extends MainScreen {
    private final Bitmap myBm;
    private int wBm;
    private int hBm;
    private int xBm;
    private int yBm;
    private boolean dragStarted;

    public MyScreen() {
        myBm = Bitmap.getBitmapResource("img/myBitmap.png");
        if (myBm != null) {
            wBm = myBm.getWidth();
            hBm = myBm.getHeight();
        }
    }

    protected void paint(Graphics graphics) {
        // need to adjust z-index of the items added on screen.
        super.paint(graphics);
        if (myBm != null) {
            graphics.drawBitmap(xBm, yBm, wBm, hBm, myBm, 0, 0);
        }
    }

    protected boolean touchEvent(TouchEvent event) {
        int x = event.getX(1);
        int y = event.getY(1);
        int eventCode = event.getEvent();

        if (dragStarted == false & (eventCode == TouchEvent.DOWN)) {
            if (isDragActivated(x, y)) {
                // need to adjust offset.
                dragStarted = true;
                updateBitmapLocation(x, y);
            }
        }

        if (eventCode == TouchEvent.MOVE && dragStarted) {
            updateBitmapLocation(x, y);
        }

        if (eventCode == TouchEvent.UP && dragStarted) {
            dragStarted = false;
            updateBitmapLocation(x, y);
        }

        // need to decide what to return
        return true;
    }

    private void updateBitmapLocation(int xTouch, int yTouch) {
        if (xTouch != xBm && yTouch != yBm) {
            xBm = xTouch;
            yBm = yTouch;
            invalidate();
        }
    }

    private boolean isDragActivated(int x, int y) {
        return (x >= xBm && x <= xBm + wBm && y >= yBm && y <= yBm + hBm);
    }
}
Rupak
  • 3,674
  • 15
  • 23
  • 1
    i have tried this way..its making the whole screen to be draggable.. i want to drag tmage only without moving the screen – Swati Feb 01 '12 at 12:50
  • Actually Screen doesn't move ever and you can control everything if you have the touch event. I'm not sure what you have tried, you can paste your code and what you tried with your question. – Rupak Feb 01 '12 at 14:10