First commit for SlidingFragment.
Change-Id: Iefffc341c7b1cca8a668218097bbcdbfbafa1713
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
<!-- Copyright (C) 2013 The Android Open Source Project
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.example.android.slidingfragments"
|
||||
android:versionCode="1"
|
||||
android:versionName="1.0">
|
||||
<uses-sdk android:minSdkVersion="14"
|
||||
android:targetSdkVersion="17"/>
|
||||
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
|
||||
<activity android:name=".SlidingFragments"
|
||||
android:label="@string/app_name">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN"/>
|
||||
<category android:name="android.intent.category.LAUNCHER"/>
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
</manifest>
|
||||
@@ -0,0 +1,22 @@
|
||||
<!-- Copyright (C) 2013 The Android Open Source Project
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
|
||||
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:valueFrom="0"
|
||||
android:valueTo="@dimen/slide_up_down_fraction"
|
||||
android:propertyName="yFraction"
|
||||
android:valueType="floatType"
|
||||
android:duration="@integer/slide_up_down_duration">
|
||||
</objectAnimator>
|
||||
@@ -0,0 +1,22 @@
|
||||
<!-- Copyright (C) 2013 The Android Open Source Project
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
|
||||
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:valueFrom="@dimen/slide_up_down_fraction"
|
||||
android:valueTo="0"
|
||||
android:propertyName="yFraction"
|
||||
android:valueType="floatType"
|
||||
android:duration="@integer/slide_up_down_duration" >
|
||||
</objectAnimator>
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 177 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 9.2 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 2.7 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 5.1 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 14 KiB |
@@ -0,0 +1,20 @@
|
||||
<!-- Copyright (C) 2013 The Android Open Source Project
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
|
||||
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:src="@drawable/golden_gate" >
|
||||
</ImageView>
|
||||
@@ -0,0 +1,31 @@
|
||||
<!-- Copyright (C) 2013 The Android Open Source Project
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
|
||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:id="@+id/move_to_back_container">
|
||||
|
||||
<fragment class="com.example.android.slidingfragments.ImageFragment"
|
||||
android:id="@+id/move_fragment"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" />
|
||||
|
||||
<View android:id="@+id/dark_hover_view"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="@color/black" />
|
||||
|
||||
</FrameLayout>
|
||||
@@ -0,0 +1,104 @@
|
||||
<!-- Copyright (C) 2013 The Android Open Source Project
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
|
||||
<com.example.android.slidingfragments.FractionalLinearLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_marginLeft="@dimen/slide_fragment_horizontal_margin"
|
||||
android:layout_marginRight="@dimen/slide_fragment_horizontal_margin"
|
||||
android:orientation="vertical"
|
||||
android:background="@color/white">
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/bridge"
|
||||
android:textColor="#000000"
|
||||
android:textStyle="bold"
|
||||
android:textSize="26sp"
|
||||
android:layout_marginBottom="@dimen/text_vertical_margin"
|
||||
android:layout_marginLeft="@dimen/text_horizontal_margin"
|
||||
android:layout_marginRight="@dimen/text_horizontal_margin" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/summary"
|
||||
android:textSize="18sp"
|
||||
android:textColor="#000000"
|
||||
android:layout_marginBottom="@dimen/text_vertical_margin"
|
||||
android:layout_marginLeft="@dimen/text_horizontal_margin"
|
||||
android:layout_marginRight="@dimen/text_horizontal_margin" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/length"
|
||||
android:textColor="#000000"
|
||||
android:layout_marginLeft="@dimen/text_horizontal_margin"
|
||||
android:layout_marginRight="@dimen/text_horizontal_margin" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/span"
|
||||
android:textColor="#000000"
|
||||
android:layout_marginLeft="@dimen/text_horizontal_margin"
|
||||
android:layout_marginRight="@dimen/text_horizontal_margin" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/construction"
|
||||
android:textColor="#000000"
|
||||
android:layout_marginLeft="@dimen/text_horizontal_margin"
|
||||
android:layout_marginRight="@dimen/text_horizontal_margin" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/height"
|
||||
android:textColor="#000000"
|
||||
android:layout_marginLeft="@dimen/text_horizontal_margin"
|
||||
android:layout_marginRight="@dimen/text_horizontal_margin" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/elevation"
|
||||
android:textColor="#000000"
|
||||
android:layout_marginLeft="@dimen/text_horizontal_margin"
|
||||
android:layout_marginRight="@dimen/text_horizontal_margin" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/address"
|
||||
android:textColor="#000000"
|
||||
android:layout_marginLeft="@dimen/text_horizontal_margin"
|
||||
android:layout_marginRight="@dimen/text_horizontal_margin" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/photo_credits"
|
||||
android:textColor="#000000"
|
||||
android:textSize="10sp"
|
||||
android:layout_marginTop="@dimen/text_vertical_margin"
|
||||
android:layout_marginLeft="@dimen/text_horizontal_margin"
|
||||
android:layout_marginRight="@dimen/text_horizontal_margin" />
|
||||
|
||||
</com.example.android.slidingfragments.FractionalLinearLayout>
|
||||
@@ -0,0 +1,23 @@
|
||||
<!-- Copyright (C) 2013 The Android Open Source Project
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
|
||||
<resources>
|
||||
|
||||
<color name="red">#ff0000</color>
|
||||
<color name="black">#000000</color>
|
||||
<color name="white">#ffffff</color>
|
||||
<color name="green">#00ff00</color>
|
||||
|
||||
</resources>
|
||||
@@ -0,0 +1,22 @@
|
||||
<!-- Copyright (C) 2013 The Android Open Source Project
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
|
||||
<resources>
|
||||
|
||||
<dimen name="slide_fragment_horizontal_margin">50dp</dimen>
|
||||
<dimen name="text_vertical_margin">18dp</dimen>
|
||||
<dimen name="text_horizontal_margin">10dp</dimen>
|
||||
|
||||
</resources>
|
||||
@@ -0,0 +1,22 @@
|
||||
<!-- Copyright (C) 2013 The Android Open Source Project
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
|
||||
<resources>
|
||||
|
||||
<integer name="slide_up_down_duration">200</integer>
|
||||
<integer name="half_slide_up_down_duration">150</integer>
|
||||
<integer name="slide_up_down_final_value">150</integer>
|
||||
|
||||
</resources>
|
||||
@@ -0,0 +1,20 @@
|
||||
<!-- Copyright (C) 2013 The Android Open Source Project
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
|
||||
<resources>
|
||||
|
||||
<item name="slide_up_down_fraction" type="dimen" format="float">0.67</item>
|
||||
|
||||
</resources>
|
||||
@@ -0,0 +1,29 @@
|
||||
<!-- Copyright (C) 2013 The Android Open Source Project
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
|
||||
<resources>
|
||||
|
||||
<string name="app_name">SlidingFragments</string>
|
||||
<string name="photo_credits"><b>Golden Gate Sunset</b> by <b>Romain Guy</b></string>
|
||||
<string name="address"><b>Address:</b> Golden Gate Bridge, San Francisco, CA 94129</string>
|
||||
<string name="elevation"><b>Elevation:</b> 220\' (67 m)</string>
|
||||
<string name="height"><b>Height:</b> 746\' (227 m)</string>
|
||||
<string name="construction"><b>Construction started:</b> 1933</string>
|
||||
<string name="span"><b>Longest span:</b> 4,200\' (1,280 m)</string>
|
||||
<string name="length"><b>Total length:</b> 8,980\' (2,737 m)</string>
|
||||
<string name="summary">"The Golden Gate Bridge is a suspension bridge spanning the Golden Gate, the opening of the San Francisco Bay into the Pacific Ocean."</string>
|
||||
<string name="bridge">Golden Gate Bridge</string>
|
||||
|
||||
</resources>
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright (C) 2013 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.example.android.slidingfragments;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.AttributeSet;
|
||||
import android.widget.LinearLayout;
|
||||
|
||||
/**
|
||||
* In order to animate the fragment containing text on/off the screen,
|
||||
* it is required that we know the height of the device being used. However,
|
||||
* this can only be determined at runtime, so we cannot specify the required
|
||||
* translation in an xml file. Since FragmentTransaction's setCustomAnimations
|
||||
* method requires an ID of an animation defined via an xml file, this linear
|
||||
* layout was built as a workaround. This custom linear layout is created to specify
|
||||
* the location of the fragment's layout as a fraction of the device's height. By
|
||||
* animating yFraction from 0 to 1, we can animate the fragment from the top of
|
||||
* the screen to the bottom of the screen, regardless of the device's specific size.
|
||||
*/
|
||||
public class FractionalLinearLayout extends LinearLayout {
|
||||
|
||||
private float mYFraction;
|
||||
private int mScreenHeight;
|
||||
|
||||
public FractionalLinearLayout(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
public FractionalLinearLayout(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSizeChanged (int w, int h, int oldw, int oldh) {
|
||||
super.onSizeChanged(w, h, oldw, oldh);
|
||||
mScreenHeight = h;
|
||||
setY(mScreenHeight);
|
||||
}
|
||||
|
||||
public float getYFraction() {
|
||||
return mYFraction;
|
||||
}
|
||||
|
||||
public void setYFraction(float yFraction) {
|
||||
mYFraction = yFraction;
|
||||
setY((mScreenHeight > 0) ? (mScreenHeight - yFraction * mScreenHeight) : 0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright (C) 2013 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.example.android.slidingfragments;
|
||||
|
||||
import android.app.Fragment;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
public class ImageFragment extends Fragment {
|
||||
|
||||
View.OnClickListener clickListener;
|
||||
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
View view = inflater.inflate(R.layout.image_fragment, container, false);
|
||||
view.setOnClickListener(clickListener);
|
||||
return view;
|
||||
}
|
||||
|
||||
public void setClickListener(View.OnClickListener clickListener) {
|
||||
this.clickListener = clickListener;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright (C) 2013 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.example.android.slidingfragments;
|
||||
|
||||
/**
|
||||
* This interface is used to inform the main activity when the entry
|
||||
* animation of the text fragment has completed in order to avoid the
|
||||
* start of a new animation before the current one has completed.
|
||||
*/
|
||||
public interface OnTextFragmentAnimationEndListener {
|
||||
public void onAnimationEnd();
|
||||
}
|
||||
@@ -0,0 +1,200 @@
|
||||
/*
|
||||
* Copyright (C) 2013 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.example.android.slidingfragments;
|
||||
|
||||
import android.animation.Animator;
|
||||
import android.animation.Animator.AnimatorListener;
|
||||
import android.animation.AnimatorListenerAdapter;
|
||||
import android.animation.AnimatorSet;
|
||||
import android.animation.ObjectAnimator;
|
||||
import android.animation.PropertyValuesHolder;
|
||||
import android.app.Activity;
|
||||
import android.app.FragmentManager;
|
||||
import android.app.FragmentTransaction;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
|
||||
/**
|
||||
* This application shows a simple technique to animate and overlay two fragments
|
||||
* on top of each other in order to provide a more immersive experience,
|
||||
* as opposed to only having full screen transitions. When additional content
|
||||
* (text) related to the currently displayed content (image) is to be shown,
|
||||
* the currently visible content can be moved into the background instead of
|
||||
* being removed from the screen entirely. This effect can therefore
|
||||
* provide a more natural way of displaying additional information to the user
|
||||
* using a different fragment.
|
||||
*
|
||||
* In this specific demo, tapping on the screen toggles between the two
|
||||
* animated states of the fragment. When the animation is called,
|
||||
* the fragment with an image animates into the background while the fragment
|
||||
* containing text slides up on top of it. When the animation is toggled once
|
||||
* more, the text fragment slides back down and the image fragment regains
|
||||
* focus.
|
||||
*/
|
||||
public class SlidingFragments extends Activity implements
|
||||
OnTextFragmentAnimationEndListener, FragmentManager.OnBackStackChangedListener {
|
||||
|
||||
ImageFragment mImageFragment;
|
||||
TextFragment mTextFragment;
|
||||
View mDarkHoverView;
|
||||
|
||||
boolean mDidSlideOut = false;
|
||||
boolean mIsAnimating = false;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.sliding_fragments_layout);
|
||||
|
||||
mDarkHoverView = findViewById(R.id.dark_hover_view);
|
||||
mDarkHoverView.setAlpha(0);
|
||||
|
||||
mImageFragment = (ImageFragment) getFragmentManager().findFragmentById(R.id.move_fragment);
|
||||
mTextFragment = new TextFragment();
|
||||
|
||||
getFragmentManager().addOnBackStackChangedListener(this);
|
||||
|
||||
mImageFragment.setClickListener(mClickListener);
|
||||
mTextFragment.setClickListener(mClickListener);
|
||||
mTextFragment.setOnTextFragmentAnimationEnd(this);
|
||||
mDarkHoverView.setOnClickListener(mClickListener);
|
||||
|
||||
}
|
||||
|
||||
View.OnClickListener mClickListener = new View.OnClickListener () {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
switchFragments();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* This method is used to toggle between the two fragment states by
|
||||
* calling the appropriate animations between them. The entry and exit
|
||||
* animations of the text fragment are specified in R.animator resource
|
||||
* files. The entry and exit animations of the image fragment are
|
||||
* specified in the slideBack and slideForward methods below. The reason
|
||||
* for separating the animation logic in this way is because the translucent
|
||||
* dark hover view must fade in at the same time as the image fragment
|
||||
* animates into the background, which would be difficult to time
|
||||
* properly given that the setCustomAnimations method can only modify the
|
||||
* two fragments in the transaction.
|
||||
*/
|
||||
private void switchFragments () {
|
||||
if (mIsAnimating) {
|
||||
return;
|
||||
}
|
||||
mIsAnimating = true;
|
||||
if (mDidSlideOut) {
|
||||
mDidSlideOut = false;
|
||||
getFragmentManager().popBackStack();
|
||||
} else {
|
||||
mDidSlideOut = true;
|
||||
|
||||
AnimatorListener listener = new AnimatorListenerAdapter() {
|
||||
@Override
|
||||
public void onAnimationEnd(Animator arg0) {
|
||||
FragmentTransaction transaction = getFragmentManager().beginTransaction();
|
||||
transaction.setCustomAnimations(R.animator.slide_fragment_in, 0, 0,
|
||||
R.animator.slide_fragment_out);
|
||||
transaction.add(R.id.move_to_back_container, mTextFragment);
|
||||
transaction.addToBackStack(null);
|
||||
transaction.commit();
|
||||
}
|
||||
};
|
||||
slideBack (listener);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBackStackChanged() {
|
||||
if (!mDidSlideOut) {
|
||||
slideForward(null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* This method animates the image fragment into the background by both
|
||||
* scaling and rotating the fragment's view, as well as adding a
|
||||
* translucent dark hover view to inform the user that it is inactive.
|
||||
*/
|
||||
public void slideBack(AnimatorListener listener)
|
||||
{
|
||||
View movingFragmentView = mImageFragment.getView();
|
||||
|
||||
PropertyValuesHolder rotateX = PropertyValuesHolder.ofFloat("rotationX", 40f);
|
||||
PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.8f);
|
||||
PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.8f);
|
||||
ObjectAnimator movingFragmentAnimator = ObjectAnimator.
|
||||
ofPropertyValuesHolder(movingFragmentView, rotateX, scaleX, scaleY);
|
||||
|
||||
ObjectAnimator darkHoverViewAnimator = ObjectAnimator.
|
||||
ofFloat(mDarkHoverView, "alpha", 0.0f, 0.5f);
|
||||
|
||||
ObjectAnimator movingFragmentRotator = ObjectAnimator.
|
||||
ofFloat(movingFragmentView, "rotationX", 0);
|
||||
movingFragmentRotator.setStartDelay(getResources().
|
||||
getInteger(R.integer.half_slide_up_down_duration));
|
||||
|
||||
AnimatorSet s = new AnimatorSet();
|
||||
s.playTogether(movingFragmentAnimator, darkHoverViewAnimator, movingFragmentRotator);
|
||||
s.addListener(listener);
|
||||
s.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* This method animates the image fragment into the foreground by both
|
||||
* scaling and rotating the fragment's view, while also removing the
|
||||
* previously added translucent dark hover view. Upon the completion of
|
||||
* this animation, the image fragment regains focus since this method is
|
||||
* called from the onBackStackChanged method.
|
||||
*/
|
||||
public void slideForward(AnimatorListener listener)
|
||||
{
|
||||
View movingFragmentView = mImageFragment.getView();
|
||||
|
||||
PropertyValuesHolder rotateX = PropertyValuesHolder.ofFloat("rotationX", 40f);
|
||||
PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
|
||||
PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
|
||||
ObjectAnimator movingFragmentAnimator = ObjectAnimator.
|
||||
ofPropertyValuesHolder(movingFragmentView, rotateX, scaleX, scaleY);
|
||||
|
||||
ObjectAnimator darkHoverViewAnimator = ObjectAnimator.
|
||||
ofFloat(mDarkHoverView, "alpha", 0.5f, 0.0f);
|
||||
|
||||
ObjectAnimator movingFragmentRotator = ObjectAnimator.
|
||||
ofFloat(movingFragmentView, "rotationX", 0);
|
||||
movingFragmentRotator.setStartDelay(
|
||||
getResources().getInteger(R.integer.half_slide_up_down_duration));
|
||||
|
||||
AnimatorSet s = new AnimatorSet();
|
||||
s.playTogether(movingFragmentAnimator, movingFragmentRotator, darkHoverViewAnimator);
|
||||
s.setStartDelay(getResources().getInteger(R.integer.slide_up_down_duration));
|
||||
s.addListener(new AnimatorListenerAdapter() {
|
||||
@Override
|
||||
public void onAnimationEnd(Animator animation) {
|
||||
mIsAnimating = false;
|
||||
}
|
||||
});
|
||||
s.start();
|
||||
}
|
||||
|
||||
public void onAnimationEnd() {
|
||||
mIsAnimating = false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright (C) 2013 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.example.android.slidingfragments;
|
||||
|
||||
import android.animation.Animator;
|
||||
import android.animation.AnimatorInflater;
|
||||
import android.animation.AnimatorListenerAdapter;
|
||||
import android.app.Fragment;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
public class TextFragment extends Fragment {
|
||||
|
||||
View.OnClickListener clickListener;
|
||||
OnTextFragmentAnimationEndListener mListener;
|
||||
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
View view = inflater.inflate(R.layout.text_fragment, container, false);
|
||||
view.setOnClickListener(clickListener);
|
||||
return view;
|
||||
}
|
||||
|
||||
public void setClickListener(View.OnClickListener clickListener) {
|
||||
this.clickListener = clickListener;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Animator onCreateAnimator(int transit, boolean enter, int nextAnim)
|
||||
{
|
||||
int id = enter ? R.animator.slide_fragment_in : R.animator.slide_fragment_out;
|
||||
final Animator anim = AnimatorInflater.loadAnimator(getActivity(), id);
|
||||
if (enter) {
|
||||
anim.addListener(new AnimatorListenerAdapter() {
|
||||
@Override
|
||||
public void onAnimationEnd(Animator animation) {
|
||||
mListener.onAnimationEnd();
|
||||
}
|
||||
});
|
||||
}
|
||||
return anim;
|
||||
}
|
||||
|
||||
public void setOnTextFragmentAnimationEnd(OnTextFragmentAnimationEndListener listener)
|
||||
{
|
||||
mListener = listener;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user