简体   繁体   中英

Xamarin Android - How prevent text move during textview animation

I have to do some animation in my application but i have a problem with textview. I need to animate a textview and make it compare from right corner.

this is my layout:

       <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>

and this is where I inizialize variable:

        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;

To animate i use a ValueAnimator that move LeftMargin from WidhtPixel to the minium left margin of textview. And I do with this code.

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

And here comes the problem when the animation start, text view compare from right but text will move to fit the textview dimension on screen instead of stay blocked on textview real dimension.

How could I avoid to make text move inside a textview.

Hope my information is enough and sorry for my bad english.

EDIT

WidthPixel = Resources.DisplayMetrics.WidthPixels;
AccelerateDecelerateInterpolator is an Interpolator Android.Views.Animation

Full classes
OnboardingPage.cs
OnboardingPageLayout.axml

Thanks in advance.

Matteo.

For everyone have the same problem i figured out a solution.
In a first time i use left margin to make textview compare, so when application start i set left margin to width of screen and when i need to make it appear i reduce left margin.
It seems that if you change somethings of textview it been forced to redraw everythings so also widht and height change.
To avoid this problem I create a layout and put textview inside it and use the same trick of left margin to layout instead on textview and everythings work.

Sorry for my bad english.
Hope that should be helpful for someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM