UWP - 在可缩放的ScrollViewer

时间:2016-11-28 17:15:09

标签: matrix uwp matrix-multiplication

我在可缩放的ScrollViewer中有InkCanvas:

<ScrollViewer x:Name="ScrollViewer" ZoomMode="Enabled">
    <Border Height="5000" Width="5000" BorderBrush="Black" BorderThickness="1">
          <InkCanvas x:Name="inkCanvas"  />
    </Border>
</ScrollViewer>

我想将标尺放在左上角。 标尺位于InkCanvas上,ScrollViewer的状态由HorizontalOffsetVerticalOffsetZoomFactor

定义

我找到了这段代码(sample

void OnBringIntoView(e)
{
    // Set Ruler Origin to Scrollviewer Viewport origin.
    // The purpose of this behavior is to allow the user to "grab" the
    // ruler and bring it into view no matter where the scrollviewer viewport
    // happens to be.  Note that this is accomplished by a simple translation
    // that adjusts to the zoom factor.  The additional ZoomFactor term is to
    // make ensure the scale of the InkPresenterRuler is invariant to Zoom.

    Matrix3x2 viewportTransform =
        Matrix3x2.CreateScale(ScrollViewer.ZoomFactor) *
        Matrix3x2.CreateTranslation(
           ScrollViewer.HorizontalOffset,
           ScrollViewer.VerticalOffset) *
        Matrix3x2.CreateScale(1.0f / ScrollViewer.ZoomFactor);

    ruler.Transform = viewportTransform;
}

简而言之:

viewport = Scale(zoom) * Translate(offset) * Scale(1/zoom)

这有效,但我有点失落。

第一个音阶做什么,第二个做什么?为什么我不能使用juct TranslateTransform?

1 个答案:

答案 0 :(得分:0)

  

为什么我不能使用juct TranslateTransform?

因为InkPresenterRuler.Transform property设计为Matrix3x2

  

第一个音阶做什么,第二个做什么?

Scale(zoom)*Scale(1/zoom)旨在让尺子对Zoom不变。无论您当前的缩放级别如何,标尺都将返回原始尺寸。

Translate(offset)更改转换矩阵的转换值(offsetX和offsetY)。

有关转换理论的详细信息,请参阅:

Matrix Transform

备注 Transformation matrix Wiki

仿射变换部分。