如何获取当前画布?

发布于 2025-01-04 15:20:01 字数 1637 浏览 0 评论 0原文

我有绘图视图。如果我触摸这个视图,它会画出小圆圈。我不会画圆圈,但不会触摸视图 - 使用帮助功能“setPoints”。我做什么:

package com.samples;
import ...

public class DrawView extends View {
    ArrayList<Point> points = new ArrayList<Point>();

    Paint paint = new Paint();

    private int pSize = 5;
    private int pColor = Color.BLACK;

    public DrawView(Context context, AttributeSet attrs) {

        super(context, attrs);

        setFocusable(true);
        setFocusableInTouchMode(true);

        this.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                v.setOnTouchListener(this);
                    Point point = new Point();
                    point.x = event.getX();
                    point.y = event.getY();
                    points.add(point); 
                    invalidate();
                }
                return true;
            }
        });
        requestFocus();
    }

    @Override
    public void onDraw(Canvas canvas) { 
        for (Point point : points) {
            canvas.drawCircle(point.x, point.y, pSize, paint);
        }
    }

    public void setPoints(Float xP, Float yP)
    {
        Point point = new Point();
        point.x = xP;
        point.y = yP;
        points.add(point);
        postInvalidate();
    }
}

class Point {
    float x, y;

    @Override
    public String toString() {
        return x + ", " + y;
    }
}

请告诉我,如何获取canvas的setPoints函数?

更新: 哇,这真是一个有趣的问题。我的 DrawView 包含在 Horizo​​ntalScrollView 中。因为如果我在这个 DrawView 中设置右坐标,没有人知道可绘制的圆圈在哪里。

I have DrawView. If I touch this view it draws small circles. I wont to draw circles but not to touch view - with help function "setPoints". What I do:

package com.samples;
import ...

public class DrawView extends View {
    ArrayList<Point> points = new ArrayList<Point>();

    Paint paint = new Paint();

    private int pSize = 5;
    private int pColor = Color.BLACK;

    public DrawView(Context context, AttributeSet attrs) {

        super(context, attrs);

        setFocusable(true);
        setFocusableInTouchMode(true);

        this.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                v.setOnTouchListener(this);
                    Point point = new Point();
                    point.x = event.getX();
                    point.y = event.getY();
                    points.add(point); 
                    invalidate();
                }
                return true;
            }
        });
        requestFocus();
    }

    @Override
    public void onDraw(Canvas canvas) { 
        for (Point point : points) {
            canvas.drawCircle(point.x, point.y, pSize, paint);
        }
    }

    public void setPoints(Float xP, Float yP)
    {
        Point point = new Point();
        point.x = xP;
        point.y = yP;
        points.add(point);
        postInvalidate();
    }
}

class Point {
    float x, y;

    @Override
    public String toString() {
        return x + ", " + y;
    }
}

Please tell me, how get canvas out setPoints function?

Update:
Wow, it's really interesting problem. My DrawView contains in HorizontalScrollView. Because if I set in this DrawView right coordinates, no one knows where are drawable circles.

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

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

发布评论

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

评论(3

胡大本事 2025-01-11 15:20:01

你不能。画布由系统管理并传递给您的onDraw()。我不明白为什么你需要它在外面。只需像这样重新声明 setPoints

public void setPoints(Canvas canvas, Float xP, Float Yp)

您就可以保留以前绘图的缓存(或存储以前的点)

You can't. The canvas is managed by the system and is passed to your onDraw(). I don't understand why you'd need it outside of there. Just redeclare setPoints like this

public void setPoints(Canvas canvas, Float xP, Float Yp)

You can keep a cache of the previous drawings (or store the previous points)

三月梨花 2025-01-11 15:20:01

尝试将 canvas2 声明为 DrawView 类中的公共变量。

Try declaring canvas2 as a public variable in the DrawView class.

睫毛溺水了 2025-01-11 15:20:01

您可以在 onDraw() 中绘制圆圈。这就是 View 应该工作的方式(从技术上讲,它实际上是在 draw() 方法中,但我们会忽略这一点)。在setPoints()中,在类范围内的变量中设置圆的点,调用invalidate(),然后像在onDraw()中那样绘制圆。如果您遵循此方法,您就遵循了视图设计的类流程。

You draw your circles in onDraw(). That's the way View is supposed to work (technically it's actually in the draw() method but we'll overlook that). In setPoints(), set the points of the circle in variables within the class scope, call invalidate(), then draw the circle like that in onDraw(). If you follow this method, you're following the class flow that the view was designed for.

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