Silverlight 背景平铺——比较简单的实现方法

Silverlight 背景平铺——比较简单的实现方法,第1张

概述         由于blend不够普及,图片平铺问题在所难免。以前写过如何在WPF中利用VisualBrush进行平铺。但是在silverlihgt中是没有VisualBrush的。那只能通过代码来控制图片平铺用了。         在google中找了一些实现方法,但总感觉太复杂。比较经典的当然是Elite.Silverlight3.PixelShaders这个项目中的平铺方法,但是过于复杂,

         由于blend不够普及,图片平铺问题在所难免。以前写过如何在WPF中利用VisualBrush进行平铺。但是在silverlihgt中是没有VisualBrush的。那只能通过代码来控制图片平铺用了。

        在Google中找了一些实现方法,但总感觉太复杂。比较经典的当然是Elite.Silverlight3.PixelShaders这个项目中的平铺方法,但是过于复杂,并且不支持绝对的平铺即需要指明在X轴平铺数量和在Y轴的平铺数量。即便是使用宽度除以图片宽度来计算,也是稍显麻烦。后来就找到了一个继承于Panel的TitlePanel,这个是比较简单的实现方法,切不用修改以往的代码。贴上代码以及示例。
        
/*copyright (c) 2008,WiredPrairIE.usAll rights reserved.Redistribution and use in source and binary forms,with or without modification,are permitted provIDed that the following conditions are met:    * Redistributions of source code must retain the above copyright notice,this List of conditions and the following disclaimer.    * Redistributions in binary form must reproduce the above copyright notice,this List of conditions and the following disclaimer         in the documentation and/or other materials provIDed with the distribution.    * Neither the name of the <ORGANIZATION> nor the names of its contributors may be used to endorse or promote products derived         from this software without specific prior written permission.THIS SOFTWARE IS PROVIDED BY THE copYRIGHT HolDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,INCLUDING,BUT NOT liMITED TO,THE IMPLIED WARRANTIES OF MERCHANTABIliTY AND fitness FOR A PARTIculaR PURPOSE ARE disCLaimED. IN NO EVENT SHALL THE copYRIGHT OWNER OR CONTRIBUTORS BE liABLE FOR ANY DIRECT,INDIRECT,INCIDENTAL,SPECIAL,EXEMPLARY,OR CONSEQUENTIAL damAGES (INCLUDING,PROCUREMENTOF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,DATA,OR PROFITS; OR BUSInesS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF liABIliTY,WHETHERIN CONTRACT,STRICT liABIliTY,OR TORT (INCLUDING NEGliGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,EVEN IF ADVISEDOF THE POSSIBIliTY OF SUCH damAGE.   */using System;using System.Net;using System.windows;using System.windows.Controls;using System.windows.documents;using System.windows.Ink;using System.windows.input;using System.windows.Media;using System.windows.Media.Animation;using System.windows.Shapes;using System.windows.Media.Imaging;using System.Diagnostics;namespace glPro.Common{    public class TilePanel : Panel    {        #region DependencyProperty setup        // Dependency propertIEs for Tile WIDth,Height and Image        public static Readonly DependencyProperty TileWIDthProperty =            DependencyProperty.Register("TileWIDth",typeof(double),typeof(TilePanel),new PropertyMetadata(double.NaN,new PropertyChangedCallback(TileWIDthPropertyChanged)));        public static Readonly DependencyProperty TileHeightProperty =            DependencyProperty.Register("TileHeight",new PropertyChangedCallback(TileHeightPropertyChanged)));        public static Readonly DependencyProperty ImageProperty =            DependencyProperty.Register("Image",typeof(ImageBrush),new PropertyMetadata(null,new PropertyChangedCallback(ImagePropertyChanged)));        public double TileWIDth        {            get { return (double)GetValue(TileWIDthProperty); }            set { SetValue(TileWIDthProperty,value); }        }        public double TileHeight        {            get { return (double)GetValue(TileHeightProperty); }            set { SetValue(TileHeightProperty,value); }        }        public ImageBrush Image        {            get { return (ImageBrush)GetValue(ImageProperty); }            set { SetValue(ImageProperty,value); }        }        #endregion        private Size _lastFinalSize;        private ImageBrush _imgBrush;        private bool _needsUpdate = false;        public TilePanel()        {            this.SizeChanged += new SizeChangedEventHandler(TilePanelSizeChanged);        }        /// <summary>        /// Called when the size of the panel changes.        /// </summary>        /// <param name="sender">This panel</param>        /// <param name="e">The new size information</param>        protected virtual voID TilePanelSizeChanged(object sender,SizeChangedEventArgs e)        {            if (e.NewSize != _lastFinalSize)            {                _lastFinalSize = e.NewSize;                // adjust the clipPing rectangle based on the new size                RectangleGeometry rg = new RectangleGeometry();                rg.Rect = new Rect(new Point(),_lastFinalSize);                this.Clip = rg;                TileAdjustmentNeededAsync();            }                    }        /// <summary>        /// Signal that an adjustment to the tiles is needed,/// then makes the call asynchronously.        /// By asynchronously making this call,it can help prevent        /// unnecessary work by the panel.        /// </summary>        protected voID TileAdjustmentNeededAsync()        {            // by doing this sync,we can queue up several request            // but then really only handle the last one.            _needsUpdate = true;            // async call the adjust tiles            this.dispatcher.BeginInvoke(delegate            {                AdjustTiles();            });        }                private static voID TileHeightPropertyChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)        {            TilePanel tilePanel = d as TilePanel;            if (tilePanel != null)            {                DeBUG.Writeline("TileHeight Changed: {0}",e.NewValue);                double val = (double)e.NewValue;                if ( val == double.NaN || val <= 0.0)                {                    throw new ArgumentOutOfRangeException("TileHeight");                }                tilePanel.TileAdjustmentNeededAsync();            }        }         private static voID TileWIDthPropertyChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)        {            TilePanel tilePanel = d as TilePanel;            if (tilePanel != null)            {                DeBUG.Writeline("TileWIDth Changed: {0}",e.NewValue);                double val = (double)e.NewValue;                if (val == double.NaN || val <= 0.0)                {                    throw new ArgumentOutOfRangeException("TileWIDth");                }                tilePanel.TileAdjustmentNeededAsync();            }        }              private static voID ImagePropertyChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)        {            // put code here to handle the property changed for Image            TilePanel tilePanel = d as TilePanel;            if (tilePanel != null )            {                DeBUG.Writeline("ImagePropertyChanged");                tilePanel._imgBrush = e.NewValue as ImageBrush;                // remove all of the existing tiles as they no longer are valID                tilePanel.Children.Clear();                tilePanel.TileAdjustmentNeededAsync();            }        }         /// <summary>        /// Perform the standard Arrange. Here though,all of the tiles        /// are placed based on the tilesize specifIEd.        /// </summary>        /// <param name="finalSize">the actual size that the panel has been given</param>        /// <returns>The size taken (which is always what it was given).</returns>        protected overrIDe Size ArrangeOverrIDe(Size finalSize)        {            UIElementCollection children = Children;            double tw = TileWIDth;            double th = TileHeight;            double x = 0,y = 0;            foreach (UIElement uIE in Children)            {                if (x >= finalSize.WIDth)                {                    x = 0;                    y += th;                }                uIE.Arrange(new Rect(x,y,tw,th));                x += tw;            }            return finalSize;        }        /// <summary>        /// Adjusts the quantity of the tiles based on the         /// size of the panel and immediately calls        /// UpdateLayout so the new tiles can be properly         /// layed out.        /// </summary>        protected virtual voID AdjustTiles()        {            DeBUG.Writeline("AdjustTiles - Update Needed: {0}",_needsUpdate);            // seems that it was already done ...            if (!_needsUpdate) { return; }            _needsUpdate = false;            // if no image brush is set ... all the children are cleared!            if (_imgBrush == null)             {                this.Children.Clear();                return ;             }            double tw = TileWIDth;            double th = TileHeight;            // determine how many we actually need            int totalNeededX = (int) Math.Ceiling(this.ActualWIDth / tw);            int totalNeededY = (int) Math.Ceiling(this.ActualHeight / th);            int totalNeeded = totalNeededX * totalNeededY;            // there may be too many             if (totalNeeded < this.Children.Count)            {                while (this.Children.Count > totalNeeded)                {                    this.Children.RemoveAt(this.Children.Count - 1);                                   }            }            // or too few ...            else if (this.Children.Count < totalNeeded)            {                while (this.Children.Count < totalNeeded)                {                    Rectangle r = new Rectangle();                    r.WIDth = tw;                    r.Height = th;                    r.Fill = _imgBrush;                    this.Children.Add(r);                                                        }            }            this.UpdateLayout();        }    }}

在xaml页面中:引用此类: xmlns:local="clr-namespace:glPro.Common"

    <GrID>            <local:TilePanel  x:name="pnlTile"                              TileWIDth="4"                              TileHeight="61">                <local:TilePanel.Image>                    <ImageBrush ImageSource="Images/2-1上平铺.png" />                </local:TilePanel.Image>            </local:TilePanel>        </GrID>
TileWIDth是图片的宽度,TileHeight是图片的高度.其他的都是自动计算的。就这么简单。贴上找来的源码。靠,才发现CSDN不能上传源码。抱歉!不过基本上代码拷贝过去就能用。 总结

以上是内存溢出为你收集整理的Silverlight 背景平铺——比较简单的实现方法全部内容,希望文章能够帮你解决Silverlight 背景平铺——比较简单的实现方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1065594.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存