如何为UWP中的TextBlock缩放动画 [英] How to animate scale of TextBlock in UWP
本文介绍了如何为UWP中的TextBlock缩放动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我使用Storyboard放大TextBlock时,它会在缩放时像素化,并且仅在完整时重新渲染。
When I use Storyboard to zoom in TextBlock it pixelates while zooming and rerenders only on complete.
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="TextBlock" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.ScaleY)">
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="1.5"/>
是否有一种方法可以在每帧重新渲染TextBlock?
Is there a way the TextBlock is rerendered each frame?
推荐答案
我找到了一个解决方案,尽管它不再与TextBlock有关,但对我来说却有效:
I found a solution though it's not about TextBlock anymore but for me it worked:
private void CreateText(string text)
{
_compositor = ElementCompositionPreview.GetElementVisual(this).Compositor;
CreateDevice();
_spriteTextVisual = _compositor.CreateSpriteVisual();
_spriteTextVisual.Size = new Vector2(512, 512);
_drawingTextSurface = _graphicsDevice.CreateDrawingSurface(new Size(512, 512), DirectXPixelFormat.B8G8R8A8UIntNormalized, DirectXAlphaMode.Premultiplied);
using (var ds = CanvasComposition.CreateDrawingSession(_drawingTextSurface))
{
ds.Clear(Colors.Transparent);
ds.DrawText(text, new Rect(0, 0, 512, 512), Colors.Black, new CanvasTextFormat
{
FontSize = 32,
FontWeight = FontWeights.Light,
VerticalAlignment = CanvasVerticalAlignment.Top,
HorizontalAlignment = CanvasHorizontalAlignment.Center,
LineSpacing = 32
});
}
_surfaceTextBrush = _compositor.CreateSurfaceBrush(_drawingTextSurface);
_spriteTextVisual.Brush = _surfaceTextBrush;
ElementCompositionPreview.SetElementChildVisual(this, _spriteTextVisual);
}
private void CreateDevice()
{
_device = CanvasDevice.GetSharedDevice();
_device.DeviceLost += Device_DeviceLost;
if (_graphicsDevice == null)
{
_graphicsDevice = CanvasComposition.CreateCompositionGraphicsDevice(_compositor, _device);
}
else
{
CanvasComposition.SetCanvasDevice(_graphicsDevice, _device);
}
}
private async void Device_DeviceLost(CanvasDevice sender, object args)
{
_device.DeviceLost -= Device_DeviceLost;
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, CreateDevice);
}
只需使此文本具有最大比例尺大小即可。
Just have to make this text of max scale size.
这篇关于如何为UWP中的TextBlock缩放动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文