Add demo for OnboardingFragment

Change-Id: I538ea409fc1cb1c1e6a60fb744b53cbeb4fb9bc1
This commit is contained in:
Chulwoo Lee
2016-03-22 18:18:14 -07:00
parent b6f352bf66
commit 2ae97c7f99
14 changed files with 463 additions and 0 deletions

View File

@@ -123,5 +123,13 @@
android:theme="@style/Theme.Example.Leanback.GuidedStep" android:theme="@style/Theme.Example.Leanback.GuidedStep"
android:exported="true" /> android:exported="true" />
<activity android:name=".OnboardingActivity"
android:theme="@style/Theme.Example.Leanback.Onboarding"
android:exported="true" />
<activity android:name=".OnboardingSupportActivity"
android:theme="@style/Theme.Example.Leanback.Onboarding"
android:exported="true" />
</application> </application>
</manifest> </manifest>

View File

@@ -336,3 +336,30 @@ for line in file:
outfile.write(line) outfile.write(line)
file.close() file.close()
outfile.close() outfile.close()
file = open('src/com/example/android/leanback/OnboardingActivity.java', 'r')
outfile = open('src/com/example/android/leanback/OnboardingSupportActivity.java', 'w')
write_java_head(outfile, "OnboardingActivity")
for line in file:
line = line.replace('android.app.Fragment', 'android.support.v4.app.Fragment')
line = line.replace('android.app.Activity', 'android.support.v4.app.FragmentActivity')
line = line.replace('OnboardingActivity', 'OnboardingSupportActivity')
line = line.replace('OnboardingDemoFragment', 'OnboardingDemoSupportFragment')
line = line.replace('extends Activity', 'extends FragmentActivity')
line = line.replace('getFragmentManager()', 'getSupportFragmentManager()')
outfile.write(line)
file.close()
outfile.close()
file = open('src/com/example/android/leanback/OnboardingDemoFragment.java', 'r')
outfile = open('src/com/example/android/leanback/OnboardingDemoSupportFragment.java', 'w')
write_java_head(outfile, "OnboardingDemoFragment")
for line in file:
line = line.replace('android.app.Fragment', 'android.support.v4.app.Fragment')
line = line.replace('import android.app.Activity', 'import android.support.v4.app.FragmentActivity')
line = line.replace('OnboardingDemoFragment', 'OnboardingDemoSupportFragment')
line = line.replace('OnboardingFragment', 'OnboardingSupportFragment')
line = line.replace('OnboardingActivity', 'OnboardingSupportActivity')
outfile.write(line)
file.close()
outfile.close()

View File

@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2016 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">
<fragment
android:name="com.example.android.leanback.OnboardingDemoFragment"
android:id="@+id/main_onboarding_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>

View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2016 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:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:src="@drawable/bg"/>

View File

@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2016 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>
<!-- Titles in the onboarding page. -->
<string-array name="onboarding_page_titles">
<item>Onboarding</item>
<item>Easy to implement</item>
<item>Customize your contents</item>
</string-array>
<!-- Descriptions in the onboarding page. -->
<string-array name="onboarding_page_descriptions">
<item>Announce your new features.</item>
<item>Just override OnbaordingFragment and provide the messages and images.</item>
<item>Customize your own logo image, splash animation and the contents animations.</item>
</string-array>
</resources>

View File

@@ -16,4 +16,5 @@
<resources> <resources>
<color name="icon_background">#4A4F51</color> <color name="icon_background">#4A4F51</color>
<color name="icon_alt_background">#2A2F51</color> <color name="icon_alt_background">#2A2F51</color>
<color name="page_indicator_dot">#80FFFFFF</color>
</resources> </resources>

View File

@@ -59,6 +59,9 @@
<string name="detail_presenter_options_description">Choose Presenter for Details</string> <string name="detail_presenter_options_description">Choose Presenter for Details</string>
<string name="legacydetails_off">Use New DetailsPresenter</string> <string name="legacydetails_off">Use New DetailsPresenter</string>
<string name="legacydetails_on">Use Legacy DetailsPresenter</string> <string name="legacydetails_on">Use Legacy DetailsPresenter</string>
<string name="onboarding">Onboarding</string>
<string name="onboarding_description">Show onboarding activity.</string>
<string name="onboarding_support">Onboarding(support version)</string>
<!-- Strings related to guided sequence activity --> <!-- Strings related to guided sequence activity -->
<string name="guidedstep_first_title">First</string> <string name="guidedstep_first_title">First</string>

View File

@@ -47,4 +47,8 @@
<item name="imageCardViewTitleStyle">@style/MyImageCardViewTitleStyle</item> <item name="imageCardViewTitleStyle">@style/MyImageCardViewTitleStyle</item>
</style> </style>
<style name="Widget.Example.Leanback.OnboardingLogoStyle" parent="Widget.Leanback.OnboardingLogoStyle">
<item name="android:layout_width">192dp</item>
<item name="android:layout_height">192dp</item>
</style>
</resources> </resources>

View File

@@ -37,4 +37,8 @@
</style> </style>
<style name="Theme.Example.Leanback.GuidedStep.Half" parent="Theme.Leanback.GuidedStep.Half"> <style name="Theme.Example.Leanback.GuidedStep.Half" parent="Theme.Leanback.GuidedStep.Half">
</style> </style>
<style name="Theme.Example.Leanback.Onboarding" parent="Theme.Leanback.Onboarding">
<item name="onboardingLogoStyle">@style/Widget.Example.Leanback.OnboardingLogoStyle</item>
<item name="onboardingNavigatorDotBgColor">@color/page_indicator_dot</item>
</style>
</resources> </resources>

View File

@@ -104,6 +104,12 @@ public class MainActivity extends Activity {
addAction(actions, DetailsPresenterSelectionActivity.class, addAction(actions, DetailsPresenterSelectionActivity.class,
R.string.detail_presenter_options, R.string.detail_presenter_options,
R.string.detail_presenter_options_description); R.string.detail_presenter_options_description);
addAction(actions, OnboardingActivity.class,
R.string.onboarding,
R.string.onboarding_description);
addAction(actions, OnboardingSupportActivity.class,
R.string.onboarding_support,
R.string.onboarding_description);
} }
private void addAction(List<GuidedAction> actions, Class cls, int titleRes, int descRes) { private void addAction(List<GuidedAction> actions, Class cls, int titleRes, int descRes) {

View File

@@ -0,0 +1,26 @@
/*
* Copyright (C) 2016 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.leanback;
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewTreeObserver;
public class OnboardingActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.onboarding);
}
}

View File

@@ -0,0 +1,135 @@
/*
* Copyright (C) 2014 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.leanback;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.support.v17.leanback.app.OnboardingFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.ImageView;
import java.util.ArrayList;
public class OnboardingDemoFragment extends OnboardingFragment {
private static final long ANIMATION_DURATION = 1000;
private static final int[] CONTENT_IMAGES = {
R.drawable.gallery_photo_1,
R.drawable.gallery_photo_2,
R.drawable.gallery_photo_3
};
private String[] mTitles;
private String[] mDescriptions;
private View mBackgroundView;
private ImageView mContentView;
private ImageView mImage1;
private ImageView mImage2;
private Animator mContentAnimator;
@Override
public void onAttach(android.app.Activity activity) {
super.onAttach(activity);
mTitles = getResources().getStringArray(R.array.onboarding_page_titles);
mDescriptions = getResources().getStringArray(R.array.onboarding_page_descriptions);
}
@Override
protected int getPageCount() {
return mTitles.length;
}
@Override
protected CharSequence getPageTitle(int i) {
return mTitles[i];
}
@Override
protected CharSequence getPageDescription(int i) {
return mDescriptions[i];
}
@Override
protected View onCreateBackgroundView(LayoutInflater layoutInflater, ViewGroup viewGroup) {
mBackgroundView = layoutInflater.inflate(R.layout.onboarding_image, viewGroup, false);
return mBackgroundView;
}
@Override
protected View onCreateContentView(LayoutInflater layoutInflater, ViewGroup viewGroup) {
mContentView = (ImageView) layoutInflater.inflate(R.layout.onboarding_image, viewGroup,
false);
MarginLayoutParams layoutParams = ((MarginLayoutParams) mContentView.getLayoutParams());
layoutParams.topMargin = 30;
layoutParams.bottomMargin = 60;
return mContentView;
}
@Override
protected View onCreateForegroundView(LayoutInflater layoutInflater, ViewGroup viewGroup) {
return null;
}
@Override
protected Animator onCreateEnterAnimation() {
ArrayList<Animator> animators = new ArrayList<>();
animators.add(createFadeInAnimator(mBackgroundView));
mContentView.setImageResource(CONTENT_IMAGES[0]);
mContentAnimator = createFadeInAnimator(mContentView);
animators.add(mContentAnimator);
AnimatorSet set = new AnimatorSet();
set.playTogether(animators);
return set;
}
@Override
protected void onPageChanged(final int newPage, int previousPage) {
if (mContentAnimator != null) {
mContentAnimator.end();
}
ArrayList<Animator> animators = new ArrayList<>();
Animator fadeOut = createFadeOutAnimator(mContentView);
fadeOut.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
mContentView.setImageResource(CONTENT_IMAGES[newPage]);
}
});
animators.add(fadeOut);
animators.add(createFadeInAnimator(mContentView));
AnimatorSet set = new AnimatorSet();
set.playSequentially(animators);
set.start();
mContentAnimator = set;
}
private Animator createFadeInAnimator(View view) {
return ObjectAnimator.ofFloat(view, View.ALPHA, 0.0f, 1.0f).setDuration(ANIMATION_DURATION);
}
private Animator createFadeOutAnimator(View view) {
return ObjectAnimator.ofFloat(view, View.ALPHA, 1.0f, 0.0f).setDuration(ANIMATION_DURATION);
}
@Override
protected void onFinishFragment() {
getActivity().finish();
}
}

View File

@@ -0,0 +1,137 @@
/* This file is auto-generated from OnboardingDemoFragment.java. DO NOT MODIFY. */
/*
* Copyright (C) 2014 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.leanback;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.support.v17.leanback.app.OnboardingSupportFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.ImageView;
import java.util.ArrayList;
public class OnboardingDemoSupportFragment extends OnboardingSupportFragment {
private static final long ANIMATION_DURATION = 1000;
private static final int[] CONTENT_IMAGES = {
R.drawable.gallery_photo_1,
R.drawable.gallery_photo_2,
R.drawable.gallery_photo_3
};
private String[] mTitles;
private String[] mDescriptions;
private View mBackgroundView;
private ImageView mContentView;
private ImageView mImage1;
private ImageView mImage2;
private Animator mContentAnimator;
@Override
public void onAttach(android.app.Activity activity) {
super.onAttach(activity);
mTitles = getResources().getStringArray(R.array.onboarding_page_titles);
mDescriptions = getResources().getStringArray(R.array.onboarding_page_descriptions);
}
@Override
protected int getPageCount() {
return mTitles.length;
}
@Override
protected CharSequence getPageTitle(int i) {
return mTitles[i];
}
@Override
protected CharSequence getPageDescription(int i) {
return mDescriptions[i];
}
@Override
protected View onCreateBackgroundView(LayoutInflater layoutInflater, ViewGroup viewGroup) {
mBackgroundView = layoutInflater.inflate(R.layout.onboarding_image, viewGroup, false);
return mBackgroundView;
}
@Override
protected View onCreateContentView(LayoutInflater layoutInflater, ViewGroup viewGroup) {
mContentView = (ImageView) layoutInflater.inflate(R.layout.onboarding_image, viewGroup,
false);
MarginLayoutParams layoutParams = ((MarginLayoutParams) mContentView.getLayoutParams());
layoutParams.topMargin = 30;
layoutParams.bottomMargin = 60;
return mContentView;
}
@Override
protected View onCreateForegroundView(LayoutInflater layoutInflater, ViewGroup viewGroup) {
return null;
}
@Override
protected Animator onCreateEnterAnimation() {
ArrayList<Animator> animators = new ArrayList<>();
animators.add(createFadeInAnimator(mBackgroundView));
mContentView.setImageResource(CONTENT_IMAGES[0]);
mContentAnimator = createFadeInAnimator(mContentView);
animators.add(mContentAnimator);
AnimatorSet set = new AnimatorSet();
set.playTogether(animators);
return set;
}
@Override
protected void onPageChanged(final int newPage, int previousPage) {
if (mContentAnimator != null) {
mContentAnimator.end();
}
ArrayList<Animator> animators = new ArrayList<>();
Animator fadeOut = createFadeOutAnimator(mContentView);
fadeOut.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
mContentView.setImageResource(CONTENT_IMAGES[newPage]);
}
});
animators.add(fadeOut);
animators.add(createFadeInAnimator(mContentView));
AnimatorSet set = new AnimatorSet();
set.playSequentially(animators);
set.start();
mContentAnimator = set;
}
private Animator createFadeInAnimator(View view) {
return ObjectAnimator.ofFloat(view, View.ALPHA, 0.0f, 1.0f).setDuration(ANIMATION_DURATION);
}
private Animator createFadeOutAnimator(View view) {
return ObjectAnimator.ofFloat(view, View.ALPHA, 1.0f, 0.0f).setDuration(ANIMATION_DURATION);
}
@Override
protected void onFinishFragment() {
getActivity().finish();
}
}

View File

@@ -0,0 +1,28 @@
/* This file is auto-generated from OnboardingActivity.java. DO NOT MODIFY. */
/*
* Copyright (C) 2016 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.leanback;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.ViewTreeObserver;
public class OnboardingSupportActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.onboarding);
}
}