Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Don't understand how to use animation with adapterViewFlipper

Tags:

android

I don't understand how to use animation with adapterViewflipper. I'm in API13, and I use an adapterViewFlipper in a Fragment.

So in My xml file I only have:

        <AdapterViewFlipper
            android:id="@+id/avfPicturesSite"
            android:layout_width="match_parent"
                android:layout_height="300dp"
                android:background="@drawable/gradientbackground"
             >

        </AdapterViewFlipper>

and I dynamically create ImageView in getView().

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = null;

    if (convertView == null) {
        final ViewHolder viewHolder = new ViewHolder();
        view = new ImageView(context);
        viewHolder.img = (ImageView) view;
        view.setTag(viewHolder);
    } else {
        view = convertView;
    }


    ViewHolder viewHolder = (ViewHolder)view.getTag();

    viewHolder.img.setImageURI(getItem(position));

    return view;
}

I browse images with buttons where I try to set the animations. It seems that sitePicturesFlipper.setInAnimation(Context context, int resourceID) is waiting for an AnimatorSet.

    nextSitePicture.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            //sitePicturesFlipper.setInAnimation(R.animator.right_in);
            sitePicturesFlipper.setInAnimation(getActivity(), R.anim.left_in);
            sitePicturesFlipper.setOutAnimation(getActivity(), R.anim.right_out);
            sitePicturesFlipper.showNext();
        }
    });

and the animators are declared like that:

left_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:shareInterpolator="false" >
    <translate android:duration="5000" android:fromXDelta="-100%" android:toXDelta="0%"/>
    <alpha android:duration="5000" android:fromAlpha="0.0" android:toAlpha="1.0" />
 </set>

right_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >
    <translate android:duration="5000" android:fromXDelta="0%" android:toXDelta="100%"/>
    <alpha android:duration="5000" android:fromAlpha="1.0" android:toAlpha="0.0" />
</set>

But when I click on the button, I get a RuntimeException Unknown animator name: translate!

I also tried to use ObjectAnimator instead, like that:

    nextSitePicture.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            sitePicturesFlipper.setInAnimation(getActivity(), R.animator.left_in);
            sitePicturesFlipper.setInAnimation(getActivity(), R.animator.right_out);
            sitePicturesFlipper.showNext();
        }
    });

and the xml files are:

left_in2.xml

<?xml version="1.0" encoding="utf-8"?>
<set>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
                android:interpolator="@android:anim/accelerate_decelerate_interpolator"
                android:propertyName="yFraction"
                android:valueType="floatType"
                android:valueFrom="-1"
                android:valueTo="0"
                android:duration="600"/>
</set>

right_out2.xml:

<?xml version="1.0" encoding="utf-8"?>
<set>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
                android:interpolator="@android:anim/accelerate_decelerate_interpolator"
                android:propertyName="yFraction"
                android:valueType="floatType"
                android:valueFrom="0"
                android:valueTo="1"
                android:duration="600"/>
</set>

but in that case I get java.lang.ClassCastException: android.animation.AnimatorSet cannot be cast to android.animation.ObjectAnimator

So I don't understand to set and use it correctly and it's quite difficult to find example using adapterViewFlipper on Internet, except in Thai, but I'm not very comfortable with Thai language...

So, how to use animation with adapterViewFlipper?

like image 630
fralbo Avatar asked Oct 04 '14 10:10

fralbo


1 Answers

Well, I finally found that ObjectAnimator is the rigth way to declare an animation with adapterViewFlipper. Anyway, it doesn't like the tags "<"set">" and "<"/set">" at least as I wrote them. And I didn't find how to declare several property animations. I admit that I still have problems to really understand Android documentation...

Waiting for infos if someone has ideas about it.

So, to solve the first problem I had, a complete set of ObjectAnimator to slide images left and right in an adapterViewFlipper is:

left_in.xml, declared in animator folder

<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
            android:interpolator="@android:anim/accelerate_decelerate_interpolator"
            android:propertyName="x"
            android:valueType="floatType"
            android:valueFrom="-1500"
            android:valueTo="0"
            android:duration="600"/>

right_out.xml

<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
            android:interpolator="@android:anim/accelerate_decelerate_interpolator"
            android:propertyName="x"
            android:valueType="floatType"
            android:valueFrom="0"
            android:valueTo="1500"
            android:duration="600"/>

right_in.xml

<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
            android:interpolator="@android:anim/accelerate_decelerate_interpolator"
            android:propertyName="x"
            android:valueType="floatType"
            android:valueFrom="1500"
            android:valueTo="0"
            android:duration="600"/>

left_out.xml

<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
            android:interpolator="@android:anim/accelerate_decelerate_interpolator"
            android:propertyName="x"
            android:valueType="floatType"
            android:valueFrom="0"
            android:valueTo="-1500"
            android:duration="600"/>

Then to use animations:

    myAdapterViewFlipper.setInAnimation(getActivity(), R.animator.right_in);
    myAdapterViewFlipper.setOutAnimation(getActivity(), R.animator.left_out);
    myAdapterViewFlipper.showNext();

or

    myAdapterViewFlipper.setInAnimation(getActivity(), R.animator.left_in);
    myAdapterViewFlipper.setOutAnimation(getActivity(), R.animator.right_out);
    myAdapterViewFlipper.showPrevious();
like image 89
fralbo Avatar answered Oct 21 '22 11:10

fralbo