如何获取当前画布? [英] How to get current canvas?

查看:23
本文介绍了如何获取当前画布?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 DrawView.如果我触摸此视图,它会绘制小圆圈.我不会画圆圈但不会触摸视图 - 使用帮助功能setPoints".我做什么:

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

请告诉我,如何从画布中取出 setPoints 函数?

Please tell me, how get canvas out setPoints function?

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

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.

推荐答案

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

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)

这篇关于如何获取当前画布?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆