超炫,超酷的silverlight 动画效果

silverlight  不怎么回事他不显示了

图片旋转控件(.cs)

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Windows.Media.Imaging;

/*
*	A 3D Image Space Demonstration in C#
*/

namespace ImageSpace3D
{
    public partial class ImageSpace3D : UserControl
    {
        private static String[] IMAGES = {"image1.png", "image2.png", "image3.png", "image4.png", "image5.png", "image6.png", "image7.png", "image8.png", "image9.png", "image10.png",
		 "image11.png", "image12.png", "image13.png", "image14.png", "image15.png", "image16.png", "image17.png", "image18.png", "image19.png"  }; 
        // images
        private static string IMAGE_PATH = "./images/";
		 				
        private static double IMAGE_WIDTH = 128;    // Image Width
        private static double IMAGE_HEIGHT = 128;   // Image Height		
		private static double SPACE_LENGTH = 2000; // dimension of the 3d space
		private static double X_MUL = 10; 		// springness toward x axis
		private static double Y_MUL = 10; 		// springness toward y axis
		private static double Z_MUL = 20;		// springness toward z axis
		private static double Z_MOVEMENT = 8;	// speed toward z axis
		private static double NEW_SCALE = 0.6;	// additional scale on the images (Use this if your image is too large)
		private static double EFFECT_FACTOR = 0.3; // Control the feeling of the 3D Space (Change it to differnt to see the differences)
													   // Must set less than 0.5
		

        private List<Image> _images= new List<Image>();
        private Dictionary<Image, Point3D> _imagePoint3Ds = new Dictionary<Image, Point3D>();
        private Canvas _holder = new Canvas();
        public Point3D _camera = new Point3D(); // _camera
        public Point3D _destination = new Point3D(0, 0, -500); // move desination

        // on enter frame simulator
        private DispatcherTimer _timer;
        private static int FPS = 24;

        public ImageSpace3D()
        {
            InitializeComponent();

            // add the holder to the canvas
            _holder.SetValue(Canvas.LeftProperty, Width/ 2);
            _holder.SetValue(Canvas.TopProperty, Height / 2);
            LayoutRoot.Children.Add(_holder);

            // add all the texts to the stage
            addImages();

            // not enough? create more
            addImages();

            // start the enter frame event
            _timer = new DispatcherTimer();
            _timer.Interval = new TimeSpan(0, 0, 0, 0, 1000 / FPS);
            _timer.Tick +=new EventHandler(_timer_Tick);
            _timer.Start();
        }

		/////////////////////////////////////////////////////        
		// Handlers
		/////////////////////////////////////////////////////	

        void image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Image image = sender as Image;
            Point3D point3D = _imagePoint3Ds[image];
			
			// set the _camera to the position of the text
			_destination.z = point3D.z + SPACE_LENGTH;
            _destination.y = point3D.y + IMAGE_WIDTH / 2 * NEW_SCALE;
            _destination.x = point3D.x + IMAGE_HEIGHT / 2 * NEW_SCALE;
        }


        void  _timer_Tick(object sender, EventArgs e)
        {
			// move the _camera automatically
			_destination.z += Z_MOVEMENT;
			
			_camera.z += (_destination.z - _camera.z) / Z_MUL;
			_camera.x += (_destination.x - _camera.x) / X_MUL;
			_camera.y += (_destination.y - _camera.y) / Y_MUL;
			
			// rearrange the position of the texts
           posImage();
        }

		/////////////////////////////////////////////////////        
		// Private Methods
		/////////////////////////////////////////////////////	

        private void addImages(){
            int seed = (int)DateTime.Now.Ticks;

            for (int i = 0; i < IMAGES.Length; i++)
            {
                // create a random object
                seed += (int)DateTime.Now.Ticks;
                Random r = new Random(seed);

                // load the image
                String url = IMAGE_PATH + IMAGES[i];
                Image image = new Image();
                image.Source = new BitmapImage(new Uri(url, UriKind.Relative));

                // randomly assign the position
                Point3D point3D = new Point3D();
                point3D.x = r.NextDouble() * Width - Width / 2;
                point3D.y = r.NextDouble() * Height - Height / 2;
                point3D.z = r.NextDouble() * SPACE_LENGTH * 2 - SPACE_LENGTH;


                // update the image property
                image.SetValue(Canvas.LeftProperty, point3D.x);
                image.SetValue(Canvas.TopProperty, point3D.y);
                image.MouseLeftButtonDown += new MouseButtonEventHandler(image_MouseLeftButtonDown);
                image.Cursor = Cursors.Hand;
                

                _imagePoint3Ds.Add(image, point3D);
                _holder.Children.Add(image);
                _images.Add(image);
            }
        }


        private void posImage()
        {
			for( int i = 0; i < _images.Count; i++){
				Image image = _images[i];
                Point3D point3D = _imagePoint3Ds[image];
				
				double zActual = SPACE_LENGTH + (point3D.z - _camera.z);
                double scale = SPACE_LENGTH / zActual - EFFECT_FACTOR;

                // update the image position and scale
				if(scale > 0){
                    image.SetValue(Canvas.LeftProperty,  (point3D.x - _camera.x) * scale);
                    image.SetValue(Canvas.TopProperty,  (point3D.y - _camera.y) * scale);

                    ScaleTransform scaleTransform = new ScaleTransform();
                    scaleTransform.ScaleX = scale * NEW_SCALE;
                    scaleTransform.ScaleY = scale * NEW_SCALE;
                    image.RenderTransform = scaleTransform;

                    image.Opacity = 1 - 0.99 * zActual / SPACE_LENGTH * 0.5;

                    // sort the children according to the scale
                    image.SetValue(Canvas.ZIndexProperty, (int)(SPACE_LENGTH - point3D.z));

				}else{ 
					// if text move over the screen, place it at the back
					point3D.z += SPACE_LENGTH * 2;	
				}

			}
        }
    }
}

 

xaml

 

<UserControl x:Class="ImageSpace3D.ImageSpace3D"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="550" Height="400">
    <Canvas x:Name="LayoutRoot" Background="White">

    </Canvas>
</UserControl>

 

point 3D

 

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;

/*
*	A 3D Image Space Demonstration in C#
*/

namespace ImageSpace3D
{
    public class Point3D
    {
        public double x;
        public double y;
        public double z;

        public Point3D()
        {

        }

        public Point3D(double x, double y, double z){
            this.x = x;
            this.y = y;
            this.z = z;
        }
    }
}

posted @ 2010-08-26 13:02  cnblogs版主  Views(1322)  Comments(4Edit  收藏  举报