Reputation: 450
I'm trying to implement the sliding animation to transit from fragment1 to fragment2, like this image.
Firstly, I tried to implement xml with set and translate, but I got RuntimeException "Unknown animator name translate".
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<translate
android:fromXDelta="0%p"
android:toXDelta="-100%p"
android:duration="500">
</translate>
</set>
Secondly, I tried to solve problem by using a class that extends framelayout, and add "getXFraction" and "setXFraction" method, like this post
public class SlidingFrameLayout extends FrameLayout
{
private static final String TAG = SlidingFrameLayout.class.getName();
public SlidingFrameLayout(Context context) {
super(context);
}
public SlidingFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public float getXFraction()
{
final int width = getWidth();
if(width != 0) return getX() / getWidth();
else return getX();
}
public void setXFraction(float xFraction) {
final int width = getWidth();
setX((width > 0) ? (xFraction * width) : -9999);
}
public float getYFraction()
{
final int height = getHeight();
if(height != 0) return getY() / getHeight(); else return getY();
}
public void setYFraction(float yFraction) {
final int height = getHeight();
setY((height > 0) ? (yFraction * height) : -9999);
}
}
But I still don't know How should I use SlidingFrameLayout? Please help me. T___T
Upvotes: 4
Views: 2279
Reputation: 6422
You have to use the SlidingFrameLayout as the root view of the fragments you want to slide in your xml layout file, for example if your root view was a Relative Layout:
<com.company.appname.SlidingFrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
</com.company.appname.SlidingFrameLayout>
Then use an objectAnimator, for example for a slide out in right direction:
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_mediumAnimTime"
android:interpolator="@android:anim/linear_interpolator"
android:propertyName="xFraction"
android:valueFrom="0"
android:valueTo="1.0"/>
Upvotes: 1
Reputation: 313
You need to use objectAnimator instead of translate. Here's are some posts with examples: Animate the transition between fragments and Android Fragments and animation
Upvotes: 1