This commit is contained in:
NaBian committed 2019-07-20 19:42:40 +08:00
1 parent 0ae2576c60
commit a13e41bdc3
2 files changed
+35 -24

No files matched your search

@@ -5,16 +5,16 @@
xmlns:hc="https://handyorg.github.io/handycontrol"> xmlns:hc="https://handyorg.github.io/handycontrol">
<hc:TransitioningContentControl> <hc:TransitioningContentControl>
<UniformGrid Rows="3" Columns="4" Margin="32"> <UniformGrid Rows="3" Columns="4" Margin="32">
<hc:ImageBlock Source="/HandyControlDemo;component/Resources/Img/Dance.png" Interval="0:0:0.125" Columns="8" Rows="10" StartColumn="0" StartRow="0" Width="110" Height="128"/> <hc:ImageBlock Source="/HandyControlDemo;component/Resources/Img/Dance.png" Interval="0:0:0.125" Columns="8" Rows="10" StartColumn="0" StartRow="0" EndColumn="7" EndRow="0" Width="110" Height="128" IsPlaying="True"/>
<hc:ImageBlock Source="/HandyControlDemo;component/Resources/Img/Dance.png" Interval="0:0:0.125" Columns="8" Rows="10" StartColumn="0" StartRow="1" Width="110" Height="128"/> <hc:ImageBlock Source="/HandyControlDemo;component/Resources/Img/Dance.png" Interval="0:0:0.125" Columns="8" Rows="10" StartColumn="0" StartRow="1" EndColumn="7" EndRow="1" Width="110" Height="128" IsPlaying="True"/>
<hc:ImageBlock Source="/HandyControlDemo;component/Resources/Img/Dance.png" Interval="0:0:0.125" Columns="8" Rows="10" StartColumn="0" StartRow="2" Width="110" Height="128"/> <hc:ImageBlock Source="/HandyControlDemo;component/Resources/Img/Dance.png" Interval="0:0:0.125" Columns="8" Rows="10" StartColumn="0" StartRow="2" EndColumn="7" EndRow="2" Width="110" Height="128" IsPlaying="{Binding IsMouseOver,RelativeSource={RelativeSource Self}}"/>
<hc:ImageBlock Source="/HandyControlDemo;component/Resources/Img/Dance.png" Interval="0:0:0.125" Columns="8" Rows="10" StartColumn="0" StartRow="3" Width="110" Height="128"/> <hc:ImageBlock Source="/HandyControlDemo;component/Resources/Img/Dance.png" Interval="0:0:0.125" Columns="8" Rows="10" StartColumn="0" StartRow="3" EndColumn="7" EndRow="3" Width="110" Height="128" IsPlaying="{Binding IsMouseOver,RelativeSource={RelativeSource Self}}"/>
<hc:ImageBlock Source="/HandyControlDemo;component/Resources/Img/Dance.png" Interval="0:0:0.1" Columns="8" Rows="10" StartColumn="0" StartRow="4" EndColumn="7" EndRow="4" Width="110" Height="128" IsPlaying="True"/> <hc:ImageBlock Source="/HandyControlDemo;component/Resources/Img/Dance.png" Interval="0:0:0.1" Columns="8" Rows="10" StartColumn="0" StartRow="4" EndColumn="7" EndRow="4" Width="110" Height="128" IsPlaying="True"/>
<hc:ImageBlock Source="/HandyControlDemo;component/Resources/Img/Dance.png" Interval="0:0:0.125" Columns="8" Rows="10" StartColumn="0" StartRow="5" EndColumn="7" EndRow="5" Width="110" Height="128" IsPlaying="True"/> <hc:ImageBlock Source="/HandyControlDemo;component/Resources/Img/Dance.png" Interval="0:0:0.125" Columns="8" Rows="10" StartColumn="0" StartRow="5" EndColumn="7" EndRow="5" Width="110" Height="128" IsPlaying="True"/>
<hc:ImageBlock Source="/HandyControlDemo;component/Resources/Img/Dance.png" Interval="0:0:0.125" Columns="8" Rows="10" StartColumn="0" StartRow="6" Width="110" Height="128"/> <hc:ImageBlock Source="/HandyControlDemo;component/Resources/Img/Dance.png" Interval="0:0:0.125" Columns="8" Rows="10" StartColumn="0" StartRow="6" EndColumn="7" EndRow="6" Width="110" Height="128" IsPlaying="{Binding IsMouseOver,RelativeSource={RelativeSource Self}}"/>
<hc:ImageBlock Source="/HandyControlDemo;component/Resources/Img/Dance.png" Interval="0:0:0.1" Columns="8" Rows="10" StartColumn="0" StartRow="7" EndColumn="7" EndRow="7" Width="110" Height="128" IsPlaying="True"/> <hc:ImageBlock Source="/HandyControlDemo;component/Resources/Img/Dance.png" Interval="0:0:0.1" Columns="8" Rows="10" StartColumn="0" StartRow="7" EndColumn="7" EndRow="7" Width="110" Height="128" IsPlaying="True"/>
<hc:ImageBlock Source="/HandyControlDemo;component/Resources/Img/Dance.png" Interval="0:0:0.125" Columns="8" Rows="10" StartColumn="0" StartRow="8" Width="110" Height="128"/> <hc:ImageBlock Source="/HandyControlDemo;component/Resources/Img/Dance.png" Interval="0:0:0.125" Columns="8" Rows="10" StartColumn="0" StartRow="8" EndColumn="7" EndRow="8" Width="110" Height="128" IsPlaying="{Binding IsMouseOver,RelativeSource={RelativeSource Self}}"/>
<hc:ImageBlock Source="/HandyControlDemo;component/Resources/Img/Dance.png" Interval="0:0:0.125" Columns="8" Rows="10" StartColumn="0" StartRow="9" Width="110" Height="128"/> <hc:ImageBlock Source="/HandyControlDemo;component/Resources/Img/Dance.png" Interval="0:0:0.125" Columns="8" Rows="10" StartColumn="0" StartRow="9" EndColumn="7" EndRow="9" Width="110" Height="128" IsPlaying="{Binding IsMouseOver,RelativeSource={RelativeSource Self}}"/>
</UniformGrid> </UniformGrid>
</hc:TransitioningContentControl> </hc:TransitioningContentControl>
</UserControl> </UserControl>
@@ -11,12 +11,18 @@ namespace HandyControl.Controls
{ {
private readonly DispatcherTimer _dispatcherTimer; private readonly DispatcherTimer _dispatcherTimer;
private BitmapSource _source;
private int _indexMax; private int _indexMax;
private int _indexMin; private int _indexMin;
private int _currentIndex; private int _currentIndex;
private int _blockWidth;
private int _blockHeight;
public ImageBlock() public ImageBlock()
{ {
_dispatcherTimer = new DispatcherTimer _dispatcherTimer = new DispatcherTimer
@@ -26,17 +32,20 @@ namespace HandyControl.Controls
_dispatcherTimer.Tick += DispatcherTimer_Tick; _dispatcherTimer.Tick += DispatcherTimer_Tick;
} }
private void OnPositionsChanged() private void UpdateDatas()
{ {
if (_source == null) return;
_indexMin = StartRow * Columns + StartColumn; _indexMin = StartRow * Columns + StartColumn;
_indexMax = EndRow * Columns + EndColumn; _indexMax = EndRow * Columns + EndColumn;
_currentIndex = _indexMin; _currentIndex = _indexMin;
_blockWidth = _source.PixelWidth / Columns;
_blockHeight = _source.PixelHeight / Rows;
} }
private static void OnPositionsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) private static void OnPositionsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{ {
var ctl = (ImageBlock)d; var ctl = (ImageBlock)d;
ctl.OnPositionsChanged(); ctl.UpdateDatas();
} }
private void DispatcherTimer_Tick(object sender, EventArgs e) => InvalidateVisual(); private void DispatcherTimer_Tick(object sender, EventArgs e) => InvalidateVisual();
@@ -106,7 +115,7 @@ namespace HandyControl.Controls
public static readonly DependencyProperty ColumnsProperty = DependencyProperty.Register( public static readonly DependencyProperty ColumnsProperty = DependencyProperty.Register(
"Columns", typeof(int), typeof(ImageBlock), new FrameworkPropertyMetadata(ValueBoxes.Int1Box, "Columns", typeof(int), typeof(ImageBlock), new FrameworkPropertyMetadata(ValueBoxes.Int1Box,
FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender), obj => (int)obj >= 1); FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender, OnPositionsChanged), obj => (int)obj >= 1);
public int Columns public int Columns
{ {
@@ -116,7 +125,7 @@ namespace HandyControl.Controls
public static readonly DependencyProperty RowsProperty = DependencyProperty.Register( public static readonly DependencyProperty RowsProperty = DependencyProperty.Register(
"Rows", typeof(int), typeof(ImageBlock), new FrameworkPropertyMetadata(ValueBoxes.Int1Box, "Rows", typeof(int), typeof(ImageBlock), new FrameworkPropertyMetadata(ValueBoxes.Int1Box,
FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender), obj => (int)obj >= 1); FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender, OnPositionsChanged), obj => (int)obj >= 1);
public int Rows public int Rows
{ {
@@ -141,7 +150,14 @@ namespace HandyControl.Controls
public static readonly DependencyProperty SourceProperty = DependencyProperty.Register( public static readonly DependencyProperty SourceProperty = DependencyProperty.Register(
"Source", typeof(ImageSource), typeof(ImageBlock), new FrameworkPropertyMetadata(default(ImageSource), "Source", typeof(ImageSource), typeof(ImageBlock), new FrameworkPropertyMetadata(default(ImageSource),
FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender)); FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender, OnSourceChanged));
private static void OnSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var ctl = (ImageBlock)d;
ctl._source = e.NewValue as BitmapSource;
ctl.UpdateDatas();
}
public ImageSource Source public ImageSource Source
{ {
@@ -151,27 +167,22 @@ namespace HandyControl.Controls
protected override void OnRender(DrawingContext dc) protected override void OnRender(DrawingContext dc)
{ {
if (Source != null && Source is BitmapSource source) if (_source == null) return;
{ var croppedBitmap = new CroppedBitmap(_source, CalDisplayRect());
// ReSharper disable once AssignNullToNotNullAttribute dc.DrawImage(croppedBitmap, new Rect(0, 0, RenderSize.Width, RenderSize.Height));
var croppedBitmap = new CroppedBitmap(source,
CalDisplayRect(source.PixelWidth / Columns, source.PixelHeight / Rows));
dc.DrawImage(croppedBitmap, new Rect(0, 0, RenderSize.Width, RenderSize.Height));
}
} }
private Int32Rect CalDisplayRect(int width, int height) private Int32Rect CalDisplayRect()
{ {
if (_currentIndex > _indexMax) if (_currentIndex > _indexMax)
{ {
_currentIndex = _indexMin; _currentIndex = _indexMin;
} }
var x = _currentIndex % Columns * width; var x = _currentIndex % Columns * _blockWidth;
// ReSharper disable once PossibleLossOfFraction var y = _currentIndex / Columns * _blockHeight;
var y = _currentIndex / Columns * height;
var rect = new Int32Rect(x, y, width, height); var rect = new Int32Rect(x, y, _blockWidth, _blockHeight);
_currentIndex++; _currentIndex++;
return rect; return rect;
} }