0

我正在尝试了解 Tizen 中的多点触控示例。我在以下部分遇到问题(每次我将手指在画布上移动一个像素或更多时都会调用此方法):

result
MainForm::OnDraw(void)
{
    __pCanvas->Show();
    __pCanvas->
   Canvas* pCanvas = GetCanvasN();

   if (pCanvas)
   {
      pCanvas->Copy(Point(GetClientAreaBounds().x, GetClientAreaBounds().y),
                    *__pCanvas, GetClientAreaBounds());
      delete pCanvas;
   }
   // Do not call Show(); it is called automatically after OnDraw() callback

   return E_SUCCESS;
}

这是触摸方法的示例:

void
MainForm::OnTouchMoved(const Control& source, const Point& currentPosition, const TouchEventInfo& touchInfo)
{
    __pointCount++;
    __strokes[__strokeCount].push_back(currentPosition);
    DrawLine(__prevPosition, currentPosition, Color::GetColor(COLOR_ID_BLACK));
    Tizen::Base::String string;
    string.Append("...");
    string.Append((int)__pointCount);
    __pCanvas->DrawText(Point(50, 300), string);
    __prevPosition = currentPosition;
    Invalidate(false);
    AppLog("OnTouchMoved");
}

我的 drawLine() 方法:

void
MainForm::DrawLine(const Tizen::Graphics::Point& prevPoint, const Tizen::Graphics::Point& point, const Tizen::Graphics::Color& color)
{
    if (__pCanvas)
    {
        __pCanvas->DrawLine(prevPoint, point);
    }
}

我不明白为什么在这里创建新画布(为什么旧画布被复制到新画布)。为什么调用 drawLine() 后旧画布没有更新?我们不能以某种方式刷新旧画布吗?

更新:

经过一些编辑,我设法创建了两个画布。我希望只能在 __pCanvas 范围内绘制,但现在它们是“蓝色”,我无法将它们设置为“红色”。

在此处输入图像描述

这是我的代码:

result
MainForm::OnInitializing(void)
{
    (...)
    AddTouchEventListener(*this);
    SetMultipointTouchEnabled(false);
    //SetMultipointTouchEnabled(true);

    __pCanvas = new (std::nothrow) Canvas();
    Rectangle rect = Rectangle(GetBounds().x, GetBounds().y, GetBounds().width, GetBounds().height);
    result r = __pCanvas->Construct(rect);

    __pInformationCanvas = new (std::nothrow) Canvas();
    r = __pInformationCanvas->Construct(Rectangle(0, 0, GetBounds().width, verticalDivider));
    if (r == E_SUCCESS)
    {
        __pCanvas->SetBackgroundColor(Color::GetColor(COLOR_ID_WHITE));
        __pCanvas->Clear();

        __pInformationCanvas->SetBackgroundColor(Color::GetColor(COLOR_ID_BLACK));
        __pInformationCanvas->Clear();

        Font font;
        font.Construct(FONT_STYLE_PLAIN, FONT_SIZE);
        __pCanvas->SetFont(font);

        Invalidate(false);
    }

    return E_SUCCESS;
}

和 onDraw 方法:

result
MainForm::OnDraw(void)
{
    Canvas* pCanvas = GetCanvasN();
    if (pCanvas != null)
    {
        pCanvas->Clear();
        pCanvas->Copy(Point(GetClientAreaBounds().x, GetClientAreaBounds().y),
                      *__pCanvas,
                      GetClientAreaBounds());

        // Copy the second Canvas to the center of the Form's Canvas
        pCanvas->Copy(Point(GetClientAreaBounds().x, GetClientAreaBounds().y),
                      *__pInformationCanvas,
                      __pInformationCanvas->GetBounds());

        delete pCanvas;
    }

    // Do not call Show(). It will be called automatically after OnDraw() callback.
    return E_SUCCESS;
}
4

1 回答 1

1

如果需要,您可以直接绘制到表单的画布上。尝试删除OnDrawreturn语句之外的所有内容,从and中删除Invalidate调用,并替换为:OnTouchPressedOnTouchMovedMainForm::DrawCircle

void
MainForm::DrawCircle(const Tizen::Graphics::Point& point, const Tizen::Graphics::Color& color)
{
    Canvas* pCanvas = GetCanvasN();
    if (pCanvas)
    {
        Rectangle rcCircle(Point(point.x - ELLIPSE_RADIUS, point.y - ELLIPSE_RADIUS), Dimension(ELLIPSE_RADIUS * 2, ELLIPSE_RADIUS * 2));
        pCanvas->FillEllipse(color, rcCircle);

        delete pCanvas;
    }
}

他们在示例应用程序中建议的方法是单独Canvas绘制一个,然后将其复制Canvas到 Form 的Canvasin 中OnDraw。我猜这GetCanvasN只会创建一个Canvas描述相同底层表面的新对象,但我可能是错的。随意检查Tizen 源代码

至于他们为什么建议这种方法;可能有几个原因。例如,如果您Form被无效化,您将丢失您在表单的Canvas. 恢复使用单个复制命令绘制的内容显然比重复之前执行的每个绘制命令要容易得多。
也可能有性能方面的原因 - 即OnDraw在处理屏幕表面时,执行单个复制命令在某种程度上更有效。
或者它可能只是编写示例应用程序的人的个人偏好。


编辑:这是使用两个画布的示例:

Canvas在 中创建另一个MainForm::OnInitializing

__pCanvas = new (std::nothrow) Canvas();
result r = __pCanvas->Construct(GetBounds());
__pAnotherCanvas = new (std::nothrow) Canvas();
r = __pAnotherCanvas->Construct(Rectangle(0, 0, 100, 64));
if (r == E_SUCCESS)
{
    __pCanvas->SetBackgroundColor(Color::GetColor(COLOR_ID_WHITE));
    __pCanvas->Clear();
    __pAnotherCanvas->SetBackgroundColor(Color::GetColor(COLOR_ID_WHITE));
    __pAnotherCanvas->Clear();
    Invalidate(false);
}

修改OnDraw以将两个画布复制到表单的Canvas

result
MainForm::OnDraw(void)
{
    Canvas* pCanvas = GetCanvasN();
    if (pCanvas != null)
    {
        pCanvas->Clear();
        pCanvas->Copy(Point(GetClientAreaBounds().x, GetClientAreaBounds().y),*__pCanvas, GetClientAreaBounds());

        // Copy the second Canvas to the center of the Form's Canvas
        int cx = GetClientAreaBounds().x + GetClientAreaBounds().width / 2;
        int cy = GetClientAreaBounds().y + GetClientAreaBounds().height / 2;
        pCanvas->Copy(Point(cx - __pAnotherCanvas->GetBounds().width / 2, cy - __pAnotherCanvas->GetBounds().height / 2),
                  *__pAnotherCanvas,
                  __pAnotherCanvas->GetBounds());


         delete pCanvas;
    }
    // Do not call Show(). It will be called automatically after OnDraw() callback.
    return E_SUCCESS;
}

在某处绘制第二个Canvas,例如OnTouchDoublePressed

void MainForm:: OnTouchDoublePressed(const Control& source, const Point& currentPosition, const TouchEventInfo& touchInfo)
{
    Point point(0, 0);
    String str; str.Format(10, L"x = %d", currentPosition.x);
    __pAnotherCanvas->Clear();
    __pAnotherCanvas->DrawText(point, str, str.GetLength());
    Invalidate(false);
} 

每次双击表单客户端区域的某个位置时,这将打印您在表单中心单击的 x 坐标,使用Canvas显示在涂鸦顶部的秒。

于 2013-06-27T12:13:48.813 回答