android


Can't get objectAnimator working with xFraction


I'm trying to animate a slide-in/slide-out animation between Fragments. I know there're tons of similiar question out there on StackOverflow, but believe me - I tried all of them and none is working.
Following my code (basically taken from similiar questions):
Custom Layout
public class SlidingFrameLayout extends FrameLayout {
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);
}
}
Fragment Transaction
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.setCustomAnimations(R.animator.card_enter_right, R.animator.card_out_left);
ft.replace(R.id.quiz_container, CardFragment.newInstance());
ft.commit();
Object animator
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:interpolator="#android:anim/linear_interpolator"
android:propertyName="xFraction"
android:valueFrom="1.0"
android:valueTo="0.0"
android:valueType="floatType" />
Layout file
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.my.package.views.SlidingFrameLayout
android:id="#+id/quiz_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Other views -->
</FrameLayout>
So when I'm running this code nothing happens. The Fragments get replaced but without an animation.
What am doing wrong?
Note: If I'm running the animation with "x" instead of "xFraction" and changing the values to let's say from:1280 to:0 it's working. Unfortunately this isn't a solution for me, since I need a "percentage value" because of the broad range of display resolutions.
Solution
Thanks to pskink who led me to the solution. I always thought that the container of my Fragment needs to implement the getXFraction()/setXFraction() method. But that's not true. You're Fragments layout needs to implement getXFraction()/setXFraction.
So basically I changed my Fragment-Layout from something like this:
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:prefix="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="40dp"
prefix:cardCornerRadius="4dp">
<!-- Content -->
</android.support.v7.widget.CardView>
to:
<com.my.package.views.SlidingFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:prefix="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="40dp"
prefix:cardCornerRadius="4dp">
<!-- Content -->
</android.support.v7.widget.CardView>
</com.my.package.views.SlidingFrameLayout>
setXFraction and getXFraction should be defined in the root view: the view you return from onCreateView, in your case custom CardView
since above code listings are only partial, I thought I'd share with you a complete working example. It works as outlined in the question.
Pressing the button toggles between 2 fragments A and B (by slide animation right to left). The fragments are just stupid text (AAAAAA and BBBBB) with different backgrounds.
MainActivity.java
package com.example.slidetrans;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
boolean showingA = true;
Button button;
A a;
B b;
private void incarnate(FragmentManager fm){
int layoutId = R.id.frame;
boolean fragmentWasNull = false;
Fragment f = fm.findFragmentById(layoutId);
if (f == null){
if (showingA){
f = a = new A();
} else {
f = b = new B();
}
fragmentWasNull = true;
}
if (fragmentWasNull){
FragmentTransaction ft = fm.beginTransaction();
ft.add(layoutId, showingA ? a : b, "main").commit();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FragmentManager fm = getFragmentManager();
incarnate(fm);
button = (Button)findViewById(R.id.button);
OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.setCustomAnimations(R.anim.in, R.anim.out);
transaction.replace(R.id.frame, showingA ? new B() : new A()).commit();
showingA = !showingA;
button.setText(showingA ? "slide in B" : "slide in A");
}
};
button.setOnClickListener(listener);
}
}
LL.java
package com.example.slidetrans;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;
public class LL extends LinearLayout {
public LL(Context context) {
super(context);
}
public LL(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();
float newWidth = (width > 0) ? (xFraction * width) : -9999;
setX(newWidth);
}
}
main.xml (layout)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="slide in B" />
<FrameLayout
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
a.xml (layout)
<?xml version="1.0" encoding="utf-8"?>
<com.example.slidetrans.LL xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#00FF00"
>
<TextView
android:id="#+id/aText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AAAAAAAAAAAAAAAAAA"
android:textSize="30sp"
android:textStyle="bold"
/>
</com.example.slidetrans.LL>
b.xml (layout)
<?xml version="1.0" encoding="utf-8"?>
<com.example.slidetrans.LL xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FFFF00"
>
<TextView
android:id="#+id/bText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textStyle="bold"
android:text="BBBBBBBBBB"
/>
</com.example.slidetrans.LL>
in.xml (anim)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:duration="500"
android:interpolator="#android:anim/linear_interpolator"
android:propertyName="xFraction"
android:valueFrom="1.0"
android:valueTo="0.0"
android:valueType="floatType" />
</set>
out.xml (anim)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:duration="500"
android:interpolator="#android:anim/linear_interpolator"
android:propertyName="xFraction"
android:valueFrom="0.0"
android:valueTo="-1.0"
android:valueType="floatType" />
</set>
The example provided by #mathheadinclouds worked for me using "R.animator.in" instead of R.anim.in in the line:
transaction.setCustomAnimations(R.anim.in, R.anim.out);

Related Links

A simple way to distinguish between a fast and a slow android device [closed]
Android and windows application communication
Phonegap app playing mp3 using Media plugin does not work
Pre fetching Images using Glide in android
Alpha testing in Google play and Authentication problems
Universal Image Loader: Get Original height and width onClick
Toolbar and DialogFragment in android
Ionic CLI error while adding plugin
Make EditBoxes not related to each other
How to get the current frame (as a Bitmap) for android facedetector in a Tracker event?
LayoutManager.smoothScrollToPosition works only after ui scroll
Can not access com.codename1.impl.android.AndroidNativeUtil From nativeInterface
How do we set color to an underline for text in textView programatically
Vector Drawables vs Bitmap in terms of RAM (Android)
Dynamic Endless RecyclerView scrolling issues
How can I load only first 10 objects of JSON and after user scrolls load another 10 and so on?

Categories

HOME
embedded
css3
google-spreadsheet
spring-data-redis
aws-cli
simulink
mips
telegram-bot
bookmarks
jax-ws
banner
webix
arangodb
android-securityexception
php-5.6
libc
pybossa
mongodb-csharp
keytool
forgot-password
sigma.js
gmm
replication
windows-xp
jquery-ias
roslyn
expressionengine3
siri
itunes
maze
android-sharedpreferences
licensing
ms-access-2003
magento-1.8
userdefaults
panoramas
database-comparison
code-snippets
zoomify
ideascript
modernizr
odroid
tasm
resolution
microsoft-metro
claims
chronicle-map
wc
apache-directory
dynamic-linq
opennms
gitlist
jquery-ui-draggable
netbeans6.8
facebook-pixel
carrot2
amq
compiler-design
mouseleave
chromium-os
angular2-testing
latin
windows-azure-queues
drawstring
nio2
blitline
crouton-os
boomerang
apache-commons-daemon
umlgraph
xmltype
primitive-types
easyrtc
punycode
webfinger
gii
template-deduction
twitter-bootstrap-rails
amslidemenu
eclipse-kepler
notorm
ie8-compatibility-mode
modular
unit-of-work
kogrid
scringo
merb
swfobject
resource-management
rmiregistry
listitem
subproject
urlrewriter
convention
flex-mobile
saxparseexception
creole
cloudfiles
luajava
commerceserver2007
timertask
cruisecontrol.rb
facebook-iframe
burndowncharts
data-mapping
sifr3
fail-fast-fail-early

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App