Android Fragmenttransaction Custom Animation (Unknown Animator Name: Translate)

Android FragmentTransaction Custom Animation (Unknown Animator Name: Translate)

It will not work, you should use object animator

animator/slide_in_left.xml

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

<objectAnimator
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:propertyName="x"
android:valueFrom="1000"
android:valueTo="0"
android:valueType="floatType" />

</set>

animator/slide_out_right.xml

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

<objectAnimator
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:propertyName="x"
android:valueFrom="0"
android:valueTo="-1000"
android:valueType="floatType" />

</set>

Class Subcategory

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
// return super.onCreateView(inflater, container, savedInstanceState);

View view = (ViewGroup) inflater.inflate(R.layout.product_frame, null);
getFragmentManager().beginTransaction()
.replace(R.id.sub_header, new Sub_Header()).commit();
getFragmentManager()
.beginTransaction()
.setCustomAnimations(R.animator.slide_in_left,
R.animator.slide_out_right, 0, 0)
.replace(R.id.product_frame, new Sub_Catagory_Grid()).commit();

view.getWidth();
return view;

}

unknown animator name : translate

No, you can use translate for Activities but not for fragment. You need to use objectAnimator for fragment.

Look at the 1st answer in this question, its working :

Android FragmentTransaction Custom Animation (Unknown Animator Name: Translate)

Android Fragment Translate Animation Not Working

Looks like you are confusing two animation frameworks, android.animation and android.view.animation (yes, it's confusing).

Your XML animation is kind of a mixture between the two. Either you set an android.animation.ObjectAnimator or a android.view.animation.Animation: see here and here for reference. In this particular case I think you are looking for a simple translate animation, which belongs to the latter, older, simpler class (and link).

So:

  • change <objectAnimator> tag to <translate>;
  • move your file from the animator folder to the anim resource folder, and recall it using R.anim.

I recommend reading the official documentation linked which is very clear on this topic. Basically, for simple translation / rotation / alpha animation, it's better to use view animations (<translate>, <rotate>, <scale>, <alpha>) in res/anim folder.

Property animation (like <objectAnimator> in res/animator) are a more powerful tool that you would rather use for complex situations.


With some research I found that setCustomAnimations() has an even more confusing behaviour.

If you are using support libraries, setCustomAnimations() only accepts simple animation objects (like your <translate>). In that case it all should work, you just have to change getFragmentManager() to getSupportFragmentManager().

If you are not using support libraries, setCustomAnimations() only accepts property animations (like the <objectAnimator>).

In this second case your simple animation becomes quite hard to do (see here and here for reference).

You can:

  • switch to support libraries, which can be boring if your development is not at an early stage, and use the support fragment manager;
  • do some work about objectAnimators and translations - there are lots of questions about, I just linked one above;
  • use one of the default transitions:

    FragmentTransaction t = getFragmentManager().beginTransaction();
    t.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); or
    t.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE); or
    t.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); or
  • try with a very bad <objectAnimator> in your res/animator resource folder:

    <objectAnimator
    android:propertyName="translationX"
    android:duration="1000"
    android:valueFrom="1000"
    android:valueTo="0"
    android:valueType="floatType"/>

This is bad because you need to specify a value in pixels (here I put 1000), so that will look different on different devices. But maybe for a fast translation that's not a real problem.

FragmentTransaction animation is working but appears glitchy

I've had exactly the same issue...

The flicker/glitch is caused because when setXFraction is first called its before onMeasure has happened and so getWidth() returns 0 meaning for the first drawn frame the fragment is in the un-scrolled position, the next call happens after the onMeasure and its drawn in the correct place as getWidth is now returning valid data.

The solution I've found is to save the X fraction in the view object as a float and then override onMeasure() and re-do the setTranslationX, using mXfract * measureSpec.

Works for me, code snippet below:

public class SlidingFrameLayout extends FrameLayout {

private static final String TAG = SlidingFrameLayout.class.getSimpleName();

private float mXfract=0f;
private float mYfract=0f;

public SlidingFrameLayout(Context context) {
super(context);
}

public SlidingFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}

public SlidingFrameLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

public void setYFraction(final float fraction) {
mYfract=fraction;
float translationY = getHeight() * fraction;
setTranslationY(translationY);
}

public float getYFraction() {
return mYfract;
}

public void setXFraction(final float fraction) {
mXfract=fraction;
float translationX = getWidth() * fraction;
setTranslationX(translationX);
}

public float getXFraction() {
return mXfract;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
// Correct any translations set before the measure was set
setTranslationX(mXfract*width);
setTranslationY(mYfract*height);
}
}

Edited: To use measurespec for width/height instead of call.

Unknown animation name: decelerateInterpolator

If you want to use a decelerate interpolator you need to set it AS an interpolator, not as the animator:

private void setAnimation(CardView viewToAnimate, int position) {
Animation animation = AnimationUtils.loadAnimation(mContext, android.R.anim.fade_in); //change this with your desidered (or custom) animation
animation.setInterpolator(mContext, android.R.anim.decelerate_interpolator);
viewToAnimate.startAnimation(animation);
}

UPDATE

You said that you are using com.bignerdranch.android:expandablerecyclerview:2.0.3.

From the official docs of the library, it's clearly state how to create expand/collapse animations:

You can also create your own animations for expansion by overriding
ParentViewHolder#onExpansionToggled(boolean), which will be called for
you when the itemView is expanded or collapsed.

I suggest you to take a look at the official example of the library.



Related Topics



Leave a reply



Submit