Created
March 20, 2017 05:02
-
-
Save adityawibisana/a505d980039d4e76d90dafcfae8462aa to your computer and use it in GitHub Desktop.
Rotation Container's margin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void SetImages(String[] Images) | |
{ | |
if (visibleItems > itemsAmount) | |
return; | |
// cleaning up | |
if (rotationContainer!=null) | |
rotationContainer.Children.Clear(); | |
positions.Clear(); | |
currentIndex = 0; | |
//TODO: REMOVE THIS CODE, as this will override your set Images | |
Images = new String[itemsAmount]; | |
for (int i = 0; i < itemsAmount; i++) | |
{ | |
Images[i] = (String.Format(@"Images\{0}.png", i)); | |
} | |
//TODO: REMOVE THIS CODE | |
RotateTransform rt = new RotateTransform() { CenterX = r, CenterY = r }; | |
rotationContainer = new Grid() | |
{ | |
Width = 2 * r, | |
Height = 2 * r, | |
RenderTransform = rt, | |
Margin = new Thickness(-4.8*r,0,0,0) | |
}; | |
LayoutRoot.Children.Add(rotationContainer); | |
Storyboard storyboard = new Storyboard(); | |
for (int i = 0; i < visibleItems; i++) | |
{ | |
BitmapImage bi = new BitmapImage(new Uri(Images[i], UriKind.Relative)); | |
Image im = new Image() { Source = bi, Width = imageWidth, Height = imageHeight}; | |
im.RenderTransform = new TranslateTransform(); | |
rotationContainer.Children.Add(im); | |
double xPos = 0, yPos = 0; | |
if (visibleItems%2==1) //if odd | |
{ | |
xPos = r * Math.Sin((180 / (visibleItems + 1) * (i + 1)) * (Math.PI / 180)); | |
yPos = -1 * r * Math.Cos((180 / (visibleItems + 1) * (i + 1)) * (Math.PI / 180)); | |
} | |
else //if the items is even | |
{ | |
xPos = r * Math.Sin((180 / (visibleItems) * (i + 1)) * (Math.PI / 180)); | |
yPos = -1 * r * Math.Cos((180 / (visibleItems) * (i + 1)) * (Math.PI / 180)); | |
} | |
DoubleAnimation translateAnimationX = new DoubleAnimation() | |
{ | |
From = 0, | |
To = xPos | |
}; | |
Storyboard.SetTarget(translateAnimationX, im); | |
Storyboard.SetTargetProperty(translateAnimationX, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.X)")); | |
storyboard.Children.Add(translateAnimationX); | |
DoubleAnimation translateAnimationY = new DoubleAnimation() | |
{ | |
From = 0, | |
To = yPos | |
}; | |
Storyboard.SetTarget(translateAnimationY, im); | |
Storyboard.SetTargetProperty(translateAnimationY, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.Y)")); | |
storyboard.Children.Add(translateAnimationY); | |
positions.Add(new Point(translateAnimationX.To.Value, translateAnimationY.To.Value)); | |
im.Tag = i; | |
} | |
// create the rest | |
for (int i= visibleItems;i<itemsAmount;i++) | |
{ | |
BitmapImage bi = new BitmapImage(new Uri(Images[i], UriKind.Relative)); | |
Image im = new Image() { Source = bi, Width = imageWidth, Height = imageHeight }; | |
im.RenderTransform = new TranslateTransform(); | |
rotationContainer.Children.Add(im); | |
double xPos = (-1) * r * Math.Sin((180 / (itemsAmount - visibleItems + 1) * (i-visibleItems + 1)) * (Math.PI / 180)); | |
DoubleAnimation translateAnimationX = new DoubleAnimation() | |
{ | |
From = 0, | |
To = xPos | |
}; | |
Storyboard.SetTarget(translateAnimationX, im); | |
Storyboard.SetTargetProperty(translateAnimationX, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.X)")); | |
storyboard.Children.Add(translateAnimationX); | |
DoubleAnimation translateAnimationY = new DoubleAnimation() | |
{ | |
From = 0, | |
To = r * Math.Cos((180 / (itemsAmount - visibleItems + 1) * (i - visibleItems + 1)) * (Math.PI / 180)) | |
}; | |
Storyboard.SetTarget(translateAnimationY, im); | |
Storyboard.SetTargetProperty(translateAnimationY, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.Y)")); | |
storyboard.Children.Add(translateAnimationY); | |
positions.Add(new Point(translateAnimationX.To.Value, translateAnimationY.To.Value)); | |
im.Tag = i; | |
im.Visibility = Visibility.Hidden; | |
} | |
storyboard.SpeedRatio = Double.MaxValue; | |
storyboard.Begin(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment