如何制作一个打印触摸点并绘制它们的Android应用程序?

发布于 2024-12-27 00:19:54 字数 182 浏览 4 评论 0原文

我想做一个小应用程序。您将触摸屏幕并绘制一些内容,它会列出您经过的点,并为每第五个点绘制绿色的 3x3 小矩形。我使用 onTouchEvent 使用 TextView 列出点并将其发送到 setContentView。但是,我在绘画方面遇到了问题。我检查了绘图(onDraw)的示例,但我无法让它同时适用于打印点和绘制绿点。任何帮助都会很棒,谢谢。

I want to make a small app. You will touch the screen and draw something and it will list points you pass and draw small green 3x3 rectangles for each fifth point. I use onTouchEvent for listing points using TextView and send it to setContentView. However, I have problem in drawing. I checked examples for drawing (onDraw) but I am not able to get it working for both printing point plus drawing green dots. Any help would be great, thanks.

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

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

发布评论

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

评论(2

娇纵 2025-01-03 00:19:54

这是在 SurfaceView 上绘图的快速示例。

public class FunPanel extends SurfaceView {

    class Point {
        int X;
        int Y;

        public Point() {
            X = Y = -1;
        }

    }

    private ArrayList<Point> mPoints = new ArrayList<Point>();
    private Point mCurPoint = new Point();
    private Bitmap mBitmap = ....// your desired image


    @Override
    public void doDraw(Canvas canvas) {
        if( !(mPoints.size() % 5) ) {
            canvas.drawBitmap(mBitmap, mCurPoint.X, mCurPoint.Y, null);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mCurPoint.X = (int) event.getX() - mBitmap.getWidth() / 2;
        mCurPoint.Y = (int) event.getY() - mBitmap.getHeight() / 2;

        mPoints.add(mCurPoint);
        return super.onTouchEvent(event);
    }

}

Here you are, a quick sample of drawing on SurfaceView.

public class FunPanel extends SurfaceView {

    class Point {
        int X;
        int Y;

        public Point() {
            X = Y = -1;
        }

    }

    private ArrayList<Point> mPoints = new ArrayList<Point>();
    private Point mCurPoint = new Point();
    private Bitmap mBitmap = ....// your desired image


    @Override
    public void doDraw(Canvas canvas) {
        if( !(mPoints.size() % 5) ) {
            canvas.drawBitmap(mBitmap, mCurPoint.X, mCurPoint.Y, null);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mCurPoint.X = (int) event.getX() - mBitmap.getWidth() / 2;
        mCurPoint.Y = (int) event.getY() - mBitmap.getHeight() / 2;

        mPoints.add(mCurPoint);
        return super.onTouchEvent(event);
    }

}
黯然 2025-01-03 00:19:54

It's not entirely clear what you're trying to do, but have a look at this It should get you started in the right direction. Basically extend a View and override the onDraw(Canvas) to draw the Rectangles and override the onTouchEvent(MotionEvent) to grab the touch points from the screen.

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