调整多边形大小以填充Canvas

时间:2014-03-04 14:05:33

标签: c# wpf resize polygon

如何调整放置在Canvas中的Polygon以使其填充画布?

我不想更改多边形点的值,我只是希望它在画布内部显示,以防多边形坐标太大,太小或超出屏幕。

2 个答案:

答案 0 :(得分:0)

您是否有机会用网格替换Canvas?如果是这样,只需设置多边形的Stretch属性,例如Uniform保持宽高比:

  <Grid Width="297" Height="159">
    <Polygon Points="10,110 110,110 110,10" Fill="Blue" Stretch="Uniform" />
  </Grid>

答案 1 :(得分:0)

最近遇到了这个问题并制定了一个可靠的解决方案:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UIView *spacer1 = [[UIView alloc] init];
    UIView *spacer2 = [[UIView alloc] init];
    spacer1.translatesAutoresizingMaskIntoConstraints = NO;
    spacer2.translatesAutoresizingMaskIntoConstraints = NO;
    spacer1.backgroundColor = [UIColor redColor];
    spacer2.backgroundColor = [UIColor redColor];
    [self.view addSubview:spacer1];
    [self.view addSubview:spacer2];

    UIView *digit1 = [UIView new];
    digit1.backgroundColor = [UIColor yellowColor];
    digit1.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:digit1];

    UIView *digit2 = [UIView new];
    digit2.backgroundColor = [UIColor magentaColor];
    digit2.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:digit2];

    UIView *digit3 = [UIView new];
    digit3.backgroundColor = [UIColor lightGrayColor];

    digit3.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:digit3];

    UIView *digit4 = [UIView new];
    digit4.backgroundColor = [UIColor darkGrayColor];

    digit4.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:digit4];

    UIView *digit5 = [UIView new];
    digit5.backgroundColor = [UIColor blueColor];

    digit5.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:digit5];

    UIView *digit6 = [UIView new];
    digit6.backgroundColor = [UIColor brownColor];

    digit6.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:digit6];

    NSDictionary *viewsDict = @{@"spacer1":spacer1, @"digit1":digit1, @"digit2":digit2, @"digit3":digit3, @"digit4":digit4, @"digit5":digit5, @"digit6":digit6, @"spacer2":spacer2};
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[spacer1(16)][digit1][digit2(==digit1)][digit3(==digit1)][digit4(==digit1)][digit5(==digit1)][digit6(==digit1)][spacer2(16)]|" options:0 metrics:nil views:viewsDict]];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[spacer1(100)]" options:0 metrics:nil views:viewsDict]];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[spacer2(100)]" options:0 metrics:nil views:viewsDict]];


    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[digit1(100)]" options:0 metrics:nil views:viewsDict]];


    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[digit2(100)]" options:0 metrics:nil views:viewsDict]];


    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[digit3(100)]" options:0 metrics:nil views:viewsDict]];


    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[digit4(100)]" options:0 metrics:nil views:viewsDict]];


    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[digit5(100)]" options:0 metrics:nil views:viewsDict]];


    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[digit6(100)]" options:0 metrics:nil views:viewsDict]];

}