Xamarin Android - 如何在textview动画期间阻止文本移动

时间:2018-02-21 16:11:21

标签: android animation xamarin xamarin.android 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的minium left margin。 我这样做了。

        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();

这就是动画开始时的问题,文本视图从右边开始比较,但是文本将移动到适合屏幕上的textview维度而不是在textview真实维度上保持阻止。

我怎样才能避免在文本视图中移动文本。

希望我的信息足够,抱歉我的英语不好。

修改

WidthPixel = Resources.DisplayMetrics.WidthPixels;
AccelerateDecelerateInterpolator是一个Interpolator Android.Views.Animation

全班
OnboardingPage.cs
OnboardingPageLayout.axml

提前致谢。

利玛。

1 个答案:

答案 0 :(得分:0)

对于每个人都有同样的问题,我想出了一个解决方案 我第一次使用左边距进行textview比较,所以当应用程序开始时我将左边距设置为屏幕宽度,当我需要使其显示时,我减少左边距。
似乎如果你改变了textview的某些东西,就会被迫重新绘制每一个东西,所以也要改变它们的高度。
为了避免这个问题,我创建了一个布局并在其中放置了textview,并使用左边距的相同技巧来布局而不是textview和everythings工作。

抱歉我的英语不好 希望对某人有所帮助。