BlackBerry - 开发类似 Cropit 的应用程序

发布于 2024-09-05 01:47:41 字数 77 浏览 9 评论 0原文

我想开发类似cropit的应用程序,我只想知道如何增加或减少图像上矩形的大小(多点触控事件),它定义要裁剪的图像部分。

多谢

i want to develop cropit like application, i just want to know how to increase or decrease the size of the rectangle over the image (multitouch events), which define the image portion to be cropped.

Thanks alot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独木成林 2024-09-12 01:47:41

使用 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;
    }
}

Use onTouchEvent, look for DOWN and MOVE events to correctly draw selection.

To make simulator multitouch: Alt+T and then use left mouse button for finger 1 and right mouse button for finger 2.

Code sample:

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文