Xamarin Android - 如何防止文本在 textview 动画期间移动
Xamarin Android - How prevent text move during textview animation
我必须在我的应用程序中做一些动画,但我对 textview 有疑问。
我需要为文本视图设置动画并使其从右上角进行比较。
这是我的布局:
<RelativeLayout
android:id="@+id/ThirdPartBottomLayout"
android:layout_width="2000dp"
android:layout_height="250dp"
android:layout_alignParentBottom="true"
android:background="@color/RedTA"
android:paddingTop="20dp"
android:paddingBottom="80dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/ThirdPartText1"
android:textSize="20dp"
android:textColor="#ffffff"
android:lines="1"
android:text="@string/Onboarding_Page3_Text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" />
<TextView
android:id="@+id/ThirdPartText2"
android:textSize="16dp"
android:textColor="#ffffff"
android:layout_below="@+id/ThirdPartText1"
android:text="@string/Onboarding_Page3_Text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center" />
</RelativeLayout>
</RelativeLayout>
这是我初始化变量的地方:
int widhtR1 = 0;
if (ThirdPartText1.Width > WidthPixel - PixelsToDp(50))
widhtR1 = WidthPixel - PixelsToDp(50);
else
widhtR1 = ThirdPartText1.Width;
lp = new RelativeLayout.LayoutParams(widhtR1,
ThirdPartText1.Height);
lp.LeftMargin = WidthPixel;
ThirdPartText1LeftMargin = (WidthPixel - widhtR1) / 2;
ThirdPartText1.LayoutParameters = lp;
int widhtR2 = 0;
if (ThirdPartText2.Width > WidthPixel - PixelsToDp(50))
widhtR2 = WidthPixel - PixelsToDp(50);
else
widhtR2 = ThirdPartText2.Width;
lp = new RelativeLayout.LayoutParams(widhtR2,
ThirdPartText2.Height);
lp.LeftMargin = WidthPixel;
lp.TopMargin = PixelsToDp(10);
lp.AddRule(LayoutRules.Below, Resource.Id.ThirdPartText1);
ThirdPartText2LeftMargin = (WidthPixel - widhtR2) / 2;
ThirdPartText2.LayoutParameters = lp;
为了制作动画,我使用 ValueAnimator 将 LeftMargin 从 WidhtPixel 移动到 textview 的最小左边距。
我使用这段代码。
ThirdPartText1Animator = ValueAnimator.OfInt(1);
ThirdPartText1Animator.SetDuration(
ThirdPartText1AnimatorDuration);
ThirdPartText1Animator.SetInterpolator(new
AccelerateDecelerateInterpolator());
var lpTxt1 =
(RelativeLayout.LayoutParams)ThirdPartText1.LayoutParameters;
ThirdPartText1Animator.Update += (sender, e) =>
{
int val = (int)e.Animation.AnimatedValue;
Console.WriteLine("VAL TXT1:" + val);
lpTxt1.LeftMargin = WidthPixel - (int)((WidthPixel -
ThirdPartText1LeftMargin) * (val / 100f));
ThirdPartText1.LayoutParameters = lpTxt1;
};
ThirdPartText2Animator = ValueAnimator.OfInt(1);
ThirdPartText2Animator.SetDuration(
ThirdPartText2AnimatorDuration);
ThirdPartText2Animator.SetInterpolator(new
LinearInterpolator());
var lpTxt2 =
(RelativeLayout.LayoutParams)ThirdPartText2.LayoutParameters;
ThirdPartText2Animator.Update += (sender, e) =>
{
int val = (int)e.Animation.AnimatedValue;
Console.WriteLine("VAL TXT2:" + val);
lpTxt2.LeftMargin = WidthPixel - (int)((WidthPixel -
ThirdPartText2LeftMargin) * (val / 100f));
ThirdPartText2.LayoutParameters = lpTxt2;
};
/*** START WITH ****/
ThirdPartText1Animator.SetIntValues(0, 100);
ThirdPartText1Animator.Start();
ThirdPartText2Animator.SetIntValues(0, 100);
ThirdPartText2Animator.Start();
当动画开始时,问题来了,文本视图从右侧比较,但文本将移动以适应屏幕上的文本视图尺寸,而不是停留在文本视图实际尺寸上。
我怎样才能避免让文本在文本视图中移动。
希望我的信息足够,抱歉我的英语不好。
编辑
宽度像素 = Resources.DisplayMetrics.WidthPixels;
AccelerateDecelerateInterpolator 是一个插值器 Android.Views.Animation
完整 类
OnboardingPage.cs
OnboardingPageLayout.axml
提前致谢。
马泰奥。
大家有同样的问题我想出了一个解决办法。
我第一次使用左边距进行 textview 比较,所以当应用程序启动时,我将左边距设置为屏幕宽度,当我需要让它出现时,我减少左边距。
似乎如果您更改 textview 的某些内容,它会被迫重绘所有内容,因此宽度和高度也会发生变化。
为了避免这个问题,我创建了一个布局并将 textview 放入其中,并使用相同的左边距技巧来布局而不是在 textview 上进行布局,一切正常。
抱歉我的英语不好。
希望对某人有所帮助。
我必须在我的应用程序中做一些动画,但我对 textview 有疑问。 我需要为文本视图设置动画并使其从右上角进行比较。
这是我的布局:
<RelativeLayout
android:id="@+id/ThirdPartBottomLayout"
android:layout_width="2000dp"
android:layout_height="250dp"
android:layout_alignParentBottom="true"
android:background="@color/RedTA"
android:paddingTop="20dp"
android:paddingBottom="80dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/ThirdPartText1"
android:textSize="20dp"
android:textColor="#ffffff"
android:lines="1"
android:text="@string/Onboarding_Page3_Text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" />
<TextView
android:id="@+id/ThirdPartText2"
android:textSize="16dp"
android:textColor="#ffffff"
android:layout_below="@+id/ThirdPartText1"
android:text="@string/Onboarding_Page3_Text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center" />
</RelativeLayout>
</RelativeLayout>
这是我初始化变量的地方:
int widhtR1 = 0;
if (ThirdPartText1.Width > WidthPixel - PixelsToDp(50))
widhtR1 = WidthPixel - PixelsToDp(50);
else
widhtR1 = ThirdPartText1.Width;
lp = new RelativeLayout.LayoutParams(widhtR1,
ThirdPartText1.Height);
lp.LeftMargin = WidthPixel;
ThirdPartText1LeftMargin = (WidthPixel - widhtR1) / 2;
ThirdPartText1.LayoutParameters = lp;
int widhtR2 = 0;
if (ThirdPartText2.Width > WidthPixel - PixelsToDp(50))
widhtR2 = WidthPixel - PixelsToDp(50);
else
widhtR2 = ThirdPartText2.Width;
lp = new RelativeLayout.LayoutParams(widhtR2,
ThirdPartText2.Height);
lp.LeftMargin = WidthPixel;
lp.TopMargin = PixelsToDp(10);
lp.AddRule(LayoutRules.Below, Resource.Id.ThirdPartText1);
ThirdPartText2LeftMargin = (WidthPixel - widhtR2) / 2;
ThirdPartText2.LayoutParameters = lp;
为了制作动画,我使用 ValueAnimator 将 LeftMargin 从 WidhtPixel 移动到 textview 的最小左边距。 我使用这段代码。
ThirdPartText1Animator = ValueAnimator.OfInt(1);
ThirdPartText1Animator.SetDuration(
ThirdPartText1AnimatorDuration);
ThirdPartText1Animator.SetInterpolator(new
AccelerateDecelerateInterpolator());
var lpTxt1 =
(RelativeLayout.LayoutParams)ThirdPartText1.LayoutParameters;
ThirdPartText1Animator.Update += (sender, e) =>
{
int val = (int)e.Animation.AnimatedValue;
Console.WriteLine("VAL TXT1:" + val);
lpTxt1.LeftMargin = WidthPixel - (int)((WidthPixel -
ThirdPartText1LeftMargin) * (val / 100f));
ThirdPartText1.LayoutParameters = lpTxt1;
};
ThirdPartText2Animator = ValueAnimator.OfInt(1);
ThirdPartText2Animator.SetDuration(
ThirdPartText2AnimatorDuration);
ThirdPartText2Animator.SetInterpolator(new
LinearInterpolator());
var lpTxt2 =
(RelativeLayout.LayoutParams)ThirdPartText2.LayoutParameters;
ThirdPartText2Animator.Update += (sender, e) =>
{
int val = (int)e.Animation.AnimatedValue;
Console.WriteLine("VAL TXT2:" + val);
lpTxt2.LeftMargin = WidthPixel - (int)((WidthPixel -
ThirdPartText2LeftMargin) * (val / 100f));
ThirdPartText2.LayoutParameters = lpTxt2;
};
/*** START WITH ****/
ThirdPartText1Animator.SetIntValues(0, 100);
ThirdPartText1Animator.Start();
ThirdPartText2Animator.SetIntValues(0, 100);
ThirdPartText2Animator.Start();
当动画开始时,问题来了,文本视图从右侧比较,但文本将移动以适应屏幕上的文本视图尺寸,而不是停留在文本视图实际尺寸上。
我怎样才能避免让文本在文本视图中移动。
希望我的信息足够,抱歉我的英语不好。
编辑
宽度像素 = Resources.DisplayMetrics.WidthPixels;
AccelerateDecelerateInterpolator 是一个插值器 Android.Views.Animation
完整 类
OnboardingPage.cs
OnboardingPageLayout.axml
提前致谢。
马泰奥。
大家有同样的问题我想出了一个解决办法。
我第一次使用左边距进行 textview 比较,所以当应用程序启动时,我将左边距设置为屏幕宽度,当我需要让它出现时,我减少左边距。
似乎如果您更改 textview 的某些内容,它会被迫重绘所有内容,因此宽度和高度也会发生变化。
为了避免这个问题,我创建了一个布局并将 textview 放入其中,并使用相同的左边距技巧来布局而不是在 textview 上进行布局,一切正常。
抱歉我的英语不好。
希望对某人有所帮助。