Tải bản đầy đủ - 0 (trang)
17-28. Fill a Shape with a Pattern or Texture

17-28. Fill a Shape with a Pattern or Texture

Tải bản đầy đủ - 0trang

CHAPTER 17 ■ WINDOWS PRESENTATION FOUNDATION



in recipe 17-27, but you use a number of additional ImageBrush properties (inherited from TileBrush) to

completely fill the shape by drawing the image repeatedly instead of once.

The first step is to define the tile that the ImageBrush will use to fill the shape. The ImageBrush uses

the concept of a viewport to represent the tile. By default, the viewport is a rectangle with dimensions

equal to those of the image that the ImageBrush would normally use to fill the shape. Normally the

viewport would be completely filled with the source image, but you can define what proportion of the

viewport is filled by the source image using the Viewport property of the ImageBrush.

The Viewport property takes four comma-separated System.Double values that identify the

coordinates of the upper-left and lower-right corners of the rectangle within the viewport where you

want the ImageBrush to insert the source image. So, for example, you can take the original image and

configure it to cover only a fraction of the viewport. The point 0,0 represents the top-left corner of the

viewport, and the point 1,1 represents the bottom-right corner.

With your base tile defined, you use the TileMode property of the ImageBrush to define how the

ImageBrush fills the shape using the tile defined by the viewport. Table 17-11 lists the possible values of

the TileMode property you can assign and describes their effect.

Table 17-11. Possible Values of the TileMode Property



Value



Description



None



The default value. The base tile is drawn but not repeated. You get a single image, and the rest

of the shape is empty (transparent fill).



Tile



The base tile is used repeatedly to fill the shape. Each tile is placed next to the other using the

same orientation.



FlipX



The base tile is used repeatedly to fill the shape, except that the tiles in alternate columns are

flipped horizontally.



FlipY



The base tile is used repeatedly to fill the shape, except that the tiles in alternate rows are

flipped vertically.



FlipXY



The base tile is used repeatedly to fill the shape, except that the tiles in alternate columns are

flipped horizontally and the tiles in alternate rows are flipped vertically.



The Code

The following XAML uses a set of Rectangle, Ellipse, and Line objects (from the System.Windows.Shapes

namespace) to demonstrate how to use ImageBrush objects to fill shapes with repeating patterns loaded

from image files (see Figure 17-26). The XAML also demonstrates how to create and use static ImageBrush

resources for the purpose of tiling.


xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="Recipe17_28" Height="300" Width="380">





883



www.it-ebooks.info



CHAPTER 17 ■ WINDOWS PRESENTATION FOUNDATION


































Stretch="UniformToFill" TileMode="FlipXY"

Viewport="0,0,0.2,0.2" />




Stretch="UniformToFill" TileMode="FlipX"

Viewport="0,0,0.5,0.2" />






Height="180" Width="80"

Fill="{StaticResource IB1}" />


Height="70" Width="230"

Fill="{StaticResource IB2}" />




Height="100" Width="230">




Stretch="Fill" TileMode="Tile"

Viewport="0,0,0.25,0.5" />







884



www.it-ebooks.info



CHAPTER 17 ■ WINDOWS PRESENTATION FOUNDATION






Height="150" Width="150">




Stretch="Uniform" TileMode="Tile"

Viewport="0,0,0.5,.5" />












Stretch="UniformToFill" TileMode="Tile"

Viewport="0,0,0.25,1" />













Figure 17-26. Filling and drawing shapes with patterns



885



www.it-ebooks.info



CHAPTER 17 ■ WINDOWS PRESENTATION FOUNDATION



17-29. Animate the Property of a Control

Problem

You need to change the value of a property on a control with respect to time. This could be the opacity of

a button, the color of a rectangle, or the height of an expander, for example.



Solution

Animate the value of the property using one or more System.Windows.Media.Animation.Timeline objects

in a System.Windows.Media.Animation.Storyboard.



How It Works

Owing to the richness of WPF’s animation framework, there are myriad options when it comes to

animating something. In essence, you are able to animate just about any System.Windows.

DependencyProperty of an object that derives from System.Windows.Media.Animation.Animatable. Couple

that with the range of types for which Timeline objects already exist, and you find yourself in a position

of endless possibilities.

To animate the property of a control, you will generally declare one or more AnimationTimeline

objects that target the data type of the property being animated. These timelines are defined as children

of a System.Windows.Media.Animation.Storyboard, with the root Storyboard being activated by a

System.Windows.Media.Animation.BeginStoryboard when used in markup. It is also possible to nest

Storyboard objects and ParallelTimeline objects as children. Each AnimationTimeline can target a

different property of a different object, a different property of the same object, or the same property of

the same object. The target object or target property can also be defined at the level of the parent

ParallelTimeline or Storyboard.

For each data type that WPF supports, there exists an AnimationTimeline. Each timeline will be

named Animation, possibly with several variants for special types of Timeline, where is the

target data type of the Timeline. With the exception of a few AnimationTimeline objects, the animation’s

effect on a target property is defined by specifying values for one or more of the To, From, or By properties.

If the From property of an AnimationTimeline is not specified, the value of the property at the point the

timeline’s clock is applied will be used. This is useful because it means you do not need to worry about

storing a property’s initial value and then restoring it at a later date. If a value for the From property is

specified, the property will be set with that value when the Timeline is applied. Again, the original value

of the property will be restored when the timeline’s clock is removed.

The abstract Timeline class, from which all AnimationTimeline, Storyboard, and ParallelTimeline

objects derive, defines several properties that allow you to define the characteristics of an animation.

Table 17-12 describes these properties of the Timeline class.



886



www.it-ebooks.info



CHAPTER 17 ■ WINDOWS PRESENTATION FOUNDATION



Table 17-12. Commonly Used Properties of the Timeline Class



Property



Description



AccelerationRatio



Used to specify a percentage of the timeline’s duration that should be used to

accelerate the speed of the animation from 0 to the animation’s maximum rate.

The value should be a System.Double ranging between 0 and 1, inclusive, and is 0

by default. The sum of a timeline’s AccelerationRatio and DecelerationRatio must

not be greater than 1.



AutoReverse



A System.Boolean property that specifies whether the Timeline should play back to

the beginning once the end has been reached.



BeginTime



A System.Nullable(TimeSpan) that specifies when a timeline should become active,

relative to its parent’s BeginTime. For a root Timeline, the offset is taken from the

time that it becomes active. This value can be negative and will start the Timeline

from the specified offset, giving the appearance that the Timeline has already been

playing for the given time. The SpeedRatio of a Timeline has no effect on its

BeginTime value, although it is affected by its parent SpeedRatio. If the property is

set to null, the Timeline will never begin.



DecelerationRatio



Used to specify a percentage of the timeline’s duration that should be used to

reduce the speed of the animation from the maximum rate to 0. The value should

be a System.Double ranging between 0 and 1, inclusive, and is 0 by default. The

sum of a timeline’s AccelerationRatio and DecelerationRatio must not be greater

than 1.



Duration



A nullable System.Windows.Duration specifying the length of time the animation

should take to play from beginning to end. For Storyboard and ParallelTimeline

objects, this value will default to the longest duration of its children. For a basic

AnimationTimeline object—for example,

System.Windows.Media.Animation.DoubleAnimation—this value will default to 1

second, and a keyframe-based animation will have a value equal to the sum of

System.Windows.Media.Animation.KeyTime values for each keyframe.



FillBehavior



A value of the System.Windows.Media.Animation.FillBehavior enumeration is used

to define an animation’s behavior once it has completed, but its parent is still

active, or its parent is in its hold period. The FillBehavior.HoldEnd value is used

when an animation should hold its final value for a property until its parent is no

longer active or outside of its hold period. The FillBehavior.Stop value will cause

the timeline to not hold its final value for a property once it completes, regardless

of whether its parent is still active.



887



www.it-ebooks.info



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

17-28. Fill a Shape with a Pattern or Texture

Tải bản đầy đủ ngay(0 tr)

×