How to download animated xml file to Android programmatically?

According to the Android developer site , we can programmatically load the AnimatorSet class from an xml file located on the path as follows: res/animator/filename.xml , so I created a sample project and tried to find out if it really works, and it doesn't; Nothing has happened. It would be very nice if I could understand what was missing and / or what I did wrong. Thanks in advance! Below is my animator XML file and Java code to download xml:

Res / animator / sample.xml:

 <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:ordering="sequentially" > <set> <objectAnimator android:propertyName="x" android:duration="500" android:valueTo="400" android:valueType="intType" /> <objectAnimator android:propertyName="y" android:duration="500" android:valueTo="300" android:valueType="intType" /> </set> <objectAnimator android:propertyName="alpha" android:duration="500" android:valueTo="1f" /> </set> 

And here are my Java codes for downloading the xml file above:

 FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // Load and start Animaton AnimatorSet animSet = (AnimatorSet) AnimatorInflater.loadAnimator(view.getContext(), R.animator.sample); animSet.setTarget(view); animSet.start(); } }); 
+7
java android xml animation
source share
2 answers

You set another Set res/animator/sample.xml . Simplify it

 <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:ordering="sequentially" > <objectAnimator android:propertyName="alpha" android:duration="500" android:valueTo="1f" /> </set> 

You inflate an AnimatorSet as follows

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

So far, I have not found a way to inflate objectAnimator from xml in Java. I have to wrap it inside Install

+20
source share

This is an error in the example given in the documentation.

Try changing android:valueType="intType" to android:valueType="floatType" .

It works in the case of @RaymondChenon , since it does not explicitly change android:valueType to int , so the system accepts a default float

The problem is that you are providing android:valueType="intType" in the animator , which should be android:valueType="floatType" for the android:propertyName="x" that you are animating.

While running, find the setter for the property you want to animate. As in your case, it will look for setX() , but since you determine the type of an argument of type int , it causes a mismatch, since there is no such method, I don’t know why this does not fail.

Look at the properties of the View class, there is a setX (float) method

For further understanding you can contact fooobar.com/questions/540581 / ...

0
source share

All Articles