【WPF學習】第四十二章 透明

  WPF支持真正的透明效果。這意味着,若是在一個性質或元素上層疊另外幾個形狀或元素,並讓全部這些形狀和元素具備不一樣的透明度,就會看到所指望的效果。經過該特性可以建立透過上面的元素能夠看到的的圖像背景,這是最簡單的情形。最複雜的情形是,使用該特性可建立多層動畫和其餘效果,對於其餘框架來講這是很難實現的。html

1、使用元素半透明app

  可採用如下幾種方法使元素具備半透明效果:框架

  •   設置元素的Opacity屬性。每一個元素(包括形狀)都是從UIElement基類繼承了Opacity屬性。不透明度(Opacity)是0到1之間的小數,1表示徹底不透明(默認值),0表示徹底透明。例如,不透明度0.9會建立90%可見(10透明)的效果。當使用這種方法設置不透明度時,設置會被應用於整個元素的可見內容。
  •   設置畫刷的Opacity屬性。每一個畫刷也從Brush基類繼承了Opacity屬性。可以使用0到1之間的值設置該屬性,一控制使用畫刷繪製的內容的透明度——無論是固定顏色畫刷、漸變畫刷,還有某種類型的紋理或圖像畫刷。由於可見形狀的Stroke和Fill屬性使用不一樣的畫刷。因此可爲邊框和表面區域設置不一樣程度的透明度。
  •   使用具備透明Alpha值得顏色。全部alpha值小於255的顏色都是半透明的。例如,可在SolidColorBrush畫刷中使用半透明顏色,並使用該畫刷繪製元素的前景內容和背景表面。在有些狀況下,使用半透明顏色比設置Opacity屬性執行得更好。

  下圖顯示的例子具備多個半透明層。性能

 

  •    窗口由不透明的白色背景。
  •   頂級的StackPanel面板包含全部元素,並使用應用了一幅圖片的ImageBrush對象。減小了畫刷的Opacity屬性值,是顏色變淡,從而能夠透過該背景看到窗口的白色背景。
  •   第一個按鈕使用半透明的紅色背景(WPF在後臺建立SolidColorBrush畫刷以繪製該顏色)。圖像可透過按鈕的背景顯示,但文本是不透明的。
  •   第一個按鈕下的標籤的使用與正常狀況同樣。默認狀況下,全部標籤都有徹底透明的背景色。
  •   文本框使用不透明的文本和邊框,但使用半透明的背景色。
  •   文本框下的另外一個StackPanel面板使用TileBrush畫刷建立笑臉圖案。TileBrush畫刷的Opacity屬性被下降,因此其餘背景可透過該面板顯示。例如,可在窗口的右下角看到太陽。
  •   第三個StackPanel面板中有一個TextBlock對象,該TextBlock對象的背景徹底透明(默認設置)並具備半透明的白色文本。若是仔細觀察,會發現兩個背景都能透過一些字母顯示。

  下面是XAML中的窗口內容:動畫

<Window x:Class="Drawing.Transparency" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Background="White" Title="Transparency" Height="385" Width="450">
    <StackPanel Margin="5">
        <StackPanel.Background>
            <ImageBrush ImageSource="celestial.jpg" Opacity="0.7"/>
        </StackPanel.Background>
        <Button Foreground="White" FontSize="16" Margin="10" BorderBrush="White" Background="#60AA4030" Padding="20">A Semi-Transparent Button</Button>
        <Label Margin="10" FontSize="18" FontWeight="Bold" Foreground="White">Some Label Text</Label>
        <TextBox Margin="10" Background="#AAAAAAAA" Foreground="White" BorderBrush="White">A semi-transparent text box</TextBox>
        <Button Margin="10" Padding="25" BorderBrush="White" >
            <Button.Background>
                <ImageBrush ImageSource="happyface.jpg" Opacity="0.6" TileMode="Tile" Viewport="0,0,0.1,0.4"/>
            </Button.Background>
            <StackPanel>

                <TextBlock Foreground="#75FFFFFF" TextAlignment="Center" FontSize="30" FontWeight="Bold" TextWrapping="Wrap" >Semi-Transparent Layers</TextBlock>

            </StackPanel>
        </Button>
    </StackPanel>
</Window>

  透明是較受歡迎的WPF特性之一。實際上,透明特性很是容易使用並且工做的很是好,全部有些過於氾濫地被用於WPF用戶界面。所以主義不要過分使用透明特性。spa

2、透明掩碼code

  Opacity屬性使用元素的全部內容都是部分透明的。OpacityMask屬性提供了更大的靈活性。可以使用元素的特定區域透明或部分透明,從而實現各類常見的以及新穎的效果。例如,可以使用OpacityMask屬性將形狀逐漸褪色到徹底透明。orm

  OpacityMask屬性接受任何畫刷。畫刷的alpha一般肯定了什麼地方是透明的。例如,若是使用SolidColorBrush畫刷設置非透明顏色,元素將保持徹底可見。顏色的其餘細節(紅、綠和藍成分)並不重要,當設置OpacityMask屬性時會忽略它們。xml

  使用SolidColorBrush畫刷設置OpacityMask屬性沒有什麼意義,由於可以使用Opacity屬性更容易地實現相同的效果。然而,當使用更特殊的畫刷類型時,例如使用LinearGradient或RadialGradientBrush畫刷,OpacityMask屬性就變得更有用了。使用漸變將一種純色變換到透明色,可建立在整個元素表面褪色的透明效果。例如,下面的按鈕就使用了這種效果:htm

<Window x:Class="Drawing.OpacityMask" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="OpacityMask" Height="300" Width="300">
    <Window.Background>
        <ImageBrush ImageSource="grandpiano.jpg"></ImageBrush>
    </Window.Background>
    <Grid Margin="10,50">
        <Button Background="Purple" FontSize="14" FontWeight="Bold">
            <Button.OpacityMask>
                <LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
                    <GradientStop Offset="0" Color="Black"></GradientStop>
                    <GradientStop Offset="1" Color="Transparent"></GradientStop>
                </LinearGradientBrush>
            </Button.OpacityMask>
            <Button.Content>A Partially Transparent Button</Button.Content>
        </Button>
    </Grid>
</Window>

  下圖在一個窗口上顯示了該按鈕,在該窗口中還顯示了一幅名貴鋼琴的圖片。

 

   還可結合使用OpacityMask屬性和VisualBrush畫刷來建立反射效果。例如,如下標記建立了最多見的WPF效果之一——具備鏡像文本的文本框。當輸入文本時,VisualBrush畫刷就會在下面繪製反射文本。使用VisualBrush畫刷繪製一個矩形,該矩形使用OpacityMask屬性褪色反射的文本,使反射文本與上面真實的元素區別開來:

<Window x:Class="Drawing.Reflection" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Reflection" Height="208.8" Width="491.2" Background="LightSteelBlue"
    >
    <Grid Margin="10" Grid.IsSharedSizeScope="True" VerticalAlignment="Center">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" SharedSizeGroup="Row"></RowDefinition>
            <RowDefinition SharedSizeGroup="Row"></RowDefinition>
        </Grid.RowDefinitions>
        <TextBox Name="txt" FontSize="30">Here is some reflected text</TextBox>
        <Rectangle Grid.Row="1" RenderTransformOrigin="1,0.5">
            <Rectangle.Fill>
                <VisualBrush Visual="{Binding ElementName=txt}"></VisualBrush>
            </Rectangle.Fill>
            <Rectangle.OpacityMask>
                <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                    <GradientStop Offset="0.3" Color="Transparent"></GradientStop>
                    <GradientStop Offset="1" Color="#44000000"></GradientStop>
                </LinearGradientBrush>
            </Rectangle.OpacityMask>
            <Rectangle.RenderTransform>
                <ScaleTransform ScaleY="-1"></ScaleTransform>
            </Rectangle.RenderTransform>
        </Rectangle>
    </Grid>
</Window>

  該例使用LinearGradientBrush畫刷在徹底透明的顏色和半透明的顏色之間進行漸變,是反射內容更加平淡。該例還使用RenderTransform翻轉矩形,是反射的內容上下顛倒。由於使用了該變換,因此漸變過渡點(gradient Stops)必須反向設置。下圖顯示了最終效果:

 

   

 

原文出處:https://www.cnblogs.com/Peter-Luo/p/12312764.html

相關文章
相關標籤/搜索