BlackBerry - Develope Cropit как приложение

я хочу разработать cropit как приложение, я просто хочу знать, как увеличить или уменьшить размер прямоугольника по изображению (мультисенсорные события), которые определяют часть изображения, которая будет обрезана.

Большое спасибо

1
задан Maksym Gontar 9 June 2010 в 15:13
поделиться

1 ответ

Используйте onTouchEvent, ищите события DOWN и MOVE, чтобы правильно рисовать выделение.

Чтобы сделать симулятор мультитач: Alt + T, а затем используйте левую кнопку мыши для пальца 1 и правую кнопку мыши для пальца 2.

Пример кода:

class Scr extends MainScreen {

    XYPoint mLeftUp = null;
    XYPoint mRightBottom = null;
    XYPoint mCoursor = null;
    // Bitmap bitmap = Bitmap.getBitmapResource("wallpaper_bold.jpg");
    Bitmap bitmap = Bitmap.getBitmapResource("wallpaper_storm.jpg");
    BitmapField mBitmapField = new BitmapField(bitmap);

    public Scr() {
        add(mBitmapField);
        mCoursor = new XYPoint(bitmap.getWidth() >> 1,
                bitmap.getHeight() >> 1);
    }

    protected void makeMenu(Menu menu, int instance) {
        super.makeMenu(menu, instance);
        if (mLeftUp != null && mRightBottom != null) {
            menu.add(new MenuItem("crop", 0, 0) {
                public void run() {
                    XYRect rect = null;
                    if (mLeftUp.x > mRightBottom.x
                            && mLeftUp.y > mRightBottom.y) {
                        rect = new XYRect(mRightBottom, mLeftUp);
                    } else {
                        rect = new XYRect(mLeftUp, mRightBottom);
                    }
                    Bitmap crop = cropImage(bitmap, rect.x, rect.y,
                            rect.width, rect.height);
                    mBitmapField.setBitmap(crop);
                    mCoursor = new XYPoint(crop.getWidth() >> 1, crop
                            .getHeight() >> 1);
                    mLeftUp = null;
                    mRightBottom = null;
                    invalidate();
                }
            });

            menu.add(new MenuItem("reset", 0, 0) {
                public void run() {
                    mBitmapField.setBitmap(bitmap);
                    mCoursor = new XYPoint(bitmap.getWidth() >> 1, bitmap
                            .getHeight() >> 1);
                    invalidate();
                }
            });
        }
    }

    protected void paint(Graphics graphics) {
        super.paint(graphics);
        if (mCoursor != null) {
            graphics.setColor(Color.RED);
            graphics.drawLine(mCoursor.x - 4, mCoursor.y - 4,
                    mCoursor.x + 4, mCoursor.y + 4);
            graphics.drawLine(mCoursor.x + 4, mCoursor.y - 4,
                    mCoursor.x - 4, mCoursor.y + 4);
        }
        if (mLeftUp != null && mRightBottom != null) {
            graphics.setColor(Color.RED);
            graphics.drawPoint(mLeftUp.x, mLeftUp.y);
            graphics.drawPoint(mRightBottom.x, mRightBottom.y);
            graphics.drawPoint(mLeftUp.x, mRightBottom.y);
            graphics.drawPoint(mRightBottom.x, mLeftUp.y);
            graphics.setColor(Color.WHITESMOKE);
            XYRect redRect = null;
            if (mLeftUp.x > mRightBottom.x && mLeftUp.y > mRightBottom.y) {
                redRect = new XYRect(mRightBottom, mLeftUp);
            } else {
                redRect = new XYRect(mLeftUp, mRightBottom);
            }
            graphics.drawRect(redRect.x, redRect.y, redRect.width,
                    redRect.height);
        }
    }

    // comment block for Bold
    protected boolean touchEvent(TouchEvent message) {
        int x1 = message.getX(1);
        int y1 = message.getY(1);
        int x2 = message.getX(2);
        int y2 = message.getY(2);
        switch (message.getEvent()) {
        case TouchEvent.DOWN:
            if (x1 != -1) {
                mLeftUp = new XYPoint(x1, y1);
            } else if (x2 != -1) {
                mRightBottom = new XYPoint(x2, y2);
            }
            break;
        case TouchEvent.MOVE:
            if (x1 != -1) {
                mLeftUp = new XYPoint(x1, y1);
            }
            if (x2 != -1) {
                mRightBottom = new XYPoint(x2, y2);
            }
            break;
        case TouchEvent.UNCLICK:
            mLeftUp = null;
            mRightBottom = null;
        default:
            break;
        }
        invalidate();
        return true;
    }

    protected boolean navigationMovement(int dx, int dy, int status,
            int time) {
        moveCoursor(dx, dy);
        return true;
    }

    private void moveCoursor(int dx, int dy) {
        mCoursor.translate(dx, dy);
        if (mLeftUp != null) {
            mRightBottom = new XYPoint(mCoursor);
        }
        invalidate();
    }

    protected boolean navigationUnclick(int status, int time) {
        clickCoursor();
        return true;
    }

    private void clickCoursor() {
        if (mLeftUp != null && mLeftUp.equals(mCoursor)) {
            mLeftUp = null;
            mRightBottom = null;
        } else {
            mLeftUp = new XYPoint(mCoursor);
        }
        invalidate();
    }

    public static Bitmap cropImage(Bitmap image, int x, int y, int width,
            int height) {
        Bitmap result = new Bitmap(width, height);
        Graphics g = Graphics.create(result);
        g.drawBitmap(0, 0, width, height, image, x, y);
        return result;
    }
}
2
ответ дан 3 September 2019 в 00:03
поделиться
Другие вопросы по тегам:

Похожие вопросы: