There is a simple AnimationDrawable animation of two frames gradient_animation.xml:
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true">
<item android:drawable="@drawable/gradient_box" android:duration="200">
<item android:drawable="@drawable/gradient_box_end" android:duration="200">
</item></item></animation-list>
gradient_box and gradient_box_end is shapes, which differ only in the length width (280 and 180dp):
<?xml version="1.0" encoding="UTF-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<size android:width="280dp" android:height="50dp">
<gradient android:startcolor="#FF3366" android:endcolor="#FF3366" android:angle="180">
<padding android:left="7dp" android:top="7dp" android:right="7dp" android:bottom="7dp">
<corners android:radius="35dp">
</corners></padding></gradient></size></shape>
Accordingly, the switch is set to android:background="@drawable/gradient_animation"
In mainactivity.cs:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
Button buttonlogin = FindViewById
(Resource.Id.button1); //a button that animates when you click on yourself
AnimationDrawable animation = (AnimationDrawable)buttonlogin.Background;
buttonlogin.Click += (sender, e) =>
{
animation.Start();
};
The idea is that the width of the button after clicking on the button should change c 280 180 dp. Actually the animation stops but starts immediately after clicking on the button, but only after the app regains focus. That is, I click the button, nothing happens, then I for example click on a text field, leaves keyboard, and it can be seen that the button changed the width, that is, the animation worked, but after some actions with the application.How to make the animation work right ? Searches led to solutions with Runnable, with Thread, with a separate class, with RunnableAnonymousInnerClassHelper (Google says that the run animation should not in the OnCreate method, and in onWindowFocusChanged). But no one way to implement did not work - if done in a separate class as clicked crashes NullPointerException.