How to run an animation from an XML file?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to run an animation from an XML file?



I have created an animation file in the animator directory of android studio. I am trying to change the colour of a button.


<?xml version="1.0" encoding="utf-8"?>
<objectAnimator android:duration="500"
xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="color"
android:valueTo="#333333"
android:valueFrom="@color/start_button"
/>



Then i tried to run the animation from my MainActivity.java, but when i click on the button to run the animation the app crashes.


public void btnClick(View view)
AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.rotation);
set.setTarget(view); // set the view you want to animate
set.start();





please post the error stacktrace. Thanks
– Lino
Aug 6 at 7:48





i don't get an error instead, when i run it in the emulator the app simply stops working when the animation starts.
– pabolo12
Aug 6 at 7:49





Check logs there must be some error regarding Crash . If you are having problems with animation then you should also mention what kind of animation you need . Question is incomplete right now .
– ADM
Aug 6 at 7:51





i just get a message in the emulator that the app has stopped running. and there is nothing in the event log.
– pabolo12
Aug 6 at 7:58





Where did you create animation file under res/anim folder?
– Khaled Lela
Aug 6 at 8:00


res/anim




2 Answers
2



android:propertyName


android:propertyName



String. Required. The object's property to animate, referenced by its
name. For example you can specify "alpha" or "backgroundColor" for a
View object. The objectAnimator element does not expose a target
attribute, however, so you cannot set the object to animate in the XML
declaration. You have to inflate your animation XML resource by
calling loadAnimator() and call setTarget() to set the target object
that contains this property.


alpha


backgroundColor


<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

<objectAnimator android:duration="500"
xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="backgroundColor"
android:valueTo="#333333"
android:valueFrom="@color/start_button"
/>
</set>



Inflating and run AnimatorSet


AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(getActivity(), R.animator.rotation);
set.setTarget(view); // set the view you want to animate
set.start();



Update



If you set propertyName="color" then the target view shall have setColor method.


propertyName="color"


setColor



W/PropertyValuesHolder: Method setColor() with type int not found on target class class android.support.v7.widget.Toolbar



In general propertyName="someName" then target view shall has method setSomeName


propertyName="someName"


setSomeName



Ex. If you need to change custom view angle then propertyName="angle"
and custom target view must implement setAngle


angle


propertyName="angle"


setAngle





@pabolo12 Please check my answer update, and don't forget to accept my answer if you feel that helped you to fix your issue.
– Khaled Lela
Aug 6 at 8:37



Try this.


public void onClick(View v)
int colorStart = v.getSolidColor();
int colorEnd = 0xFFFF0000;
ValueAnimator colorAnim = ObjectAnimator.ofInt(v,"backgroundColor",colorStart, colorEnd);
colorAnim.setDuration(2000);
colorAnim.setEvaluator(new ArgbEvaluator());
colorAnim.setRepeatCount(1);
colorAnim.setRepeatMode(ValueAnimator.REVERSE);
colorAnim.start();







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard