【发布时间】:2021-11-20 06:03:59
【问题描述】:
我正在尝试像这样为我的标签设置动画:https://1drv.ms/v/s!ApO-CpoDPS4ikX5sxW4QWn8_rdxF?e=L3s2AB 所以我编写了以下代码:
xaml:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="alphatrial.MainPage"
BackgroundColor="#ffc40c"
xmlns:local="clr-namespace:alphatrial.Effects">
<ContentPage.Effects>
<local:StatusBarEffect
BackgroundColor="#ffc40c"/>
</ContentPage.Effects>
<StackLayout >
<Label Text="ISC - Alpha" TextColor="Black" FontSize="Large" FontAttributes="Bold" FontFamily="MyAwesomeCustomFont" HorizontalOptions="End" Margin="0,50,50,-14" />
<BoxView HorizontalOptions="FillAndExpand" Color="Black" HeightRequest="2" Margin="0,0,10,0"/>
<Label Text="App" TextColor="White" FontSize="Large" FontAttributes="Bold" FontFamily="MyAwesomeCustomFont" HorizontalOptions="End" Margin="0,-12,15,0"/>
<Frame Margin="0,80,0,0" BackgroundColor="Transparent">
<StackLayout >
<Label Text="Email" TextColor="Black" FontSize="16" Margin="25,0,20,0" x:Name="emailAnimate" />
<Entry Margin="20,-40,20,0" x:Name="email" />
</StackLayout>
</Frame>
<Frame Margin="0,-10,0,0" BackgroundColor="Transparent">
<StackLayout >
<Label Text="Password" TextColor="Black" FontSize="16" Margin="25,0,20,0" x:Name="PassAnimate" />
<Entry Margin="20,-40,20,0" x:Name="password" IsPassword="True"/>
<ImageButton Source="hidePass.png" WidthRequest="25" HeightRequest="25" HorizontalOptions="End" BackgroundColor="Transparent" Margin="0,-40,20,0" x:Name="show"/>
</StackLayout>
</Frame>
<Button CornerRadius="20" Text="Login" FontSize="Medium" BackgroundColor="#3d3d3d" TextColor="White" Margin="50" FontFamily="MyAwesomeCustomFont"/>
<StackLayout Orientation="Horizontal" HorizontalOptions="Center" VerticalOptions="Center">
<Switch HeightRequest="40" WidthRequest="50" OnColor="Black" ThumbColor="White"/>
<Label Text="Remember Me" TextColor="Black" FontAttributes="Bold" Margin="10"/>
</StackLayout>
</StackLayout>
</ContentPage>
xaml.cs
public MainPage()
{
InitializeComponent();
username = this.FindByName<Entry>("email");
pass = this.FindByName<Entry>("password");
anime_user = this.FindByName<Label>("emailAnimate");
anime_pass = this.FindByName<Label>("PassAnimate");
username.Focused += Email_Focused;
username.Unfocused += Email_Unfocused;
pass.Focused += Password_Focused;
pass.Unfocused += Password_Unfocused;
}
private void Password_Unfocused(object sender, FocusEventArgs e)
{
if (string.IsNullOrEmpty(pass.Text))
{
anime_pass.ScaleTo(1, 100);
anime_pass.TranslateTo(0, 0, 100);
}
}
private void Password_Focused(object sender, FocusEventArgs e)
{
if (pass.IsFocused)
{
anime_pass.ScaleTo(0.8, 150);
anime_pass.TranslateTo(-28, -30, 150);
}
}
private void Email_Unfocused(object sender, FocusEventArgs e)
{
if (username.IsFocused == false)
{
if (string.IsNullOrEmpty(username.Text) )
{
anime_user.ScaleTo(1, 100);
anime_user.TranslateTo(0, 0, 100);
}
}
}
private void Email_Focused(object sender, FocusEventArgs e)
{
if (username.IsFocused)
{
anime_user.ScaleTo(0.8,150);
anime_user.TranslateTo(-28, -30,150);
}
}
}
}
这在我用来调试和设计应用程序的我自己的手机上运行良好,但是当我在另一部屏幕较小的手机上尝试它时,标签的翻译超出了需要,它们并没有停在预期的位置。当我水平旋转手机屏幕时也发生了同样的情况。无论屏幕大小如何,我如何将视图转换到相同的位置? 提前致谢。
【问题讨论】:
标签: xamarin.forms android-animation translate-animation