OnboardingDemoFragment.java revision e3ef610a75fd382df930699ca42b61f24ac128f8
1e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee/*
2e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee * Copyright (C) 2014 The Android Open Source Project
3e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee *
4e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee * in compliance with the License. You may obtain a copy of the License at
6e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee *
7e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee * http://www.apache.org/licenses/LICENSE-2.0
8e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee *
9e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee * Unless required by applicable law or agreed to in writing, software distributed under the License
10e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee * or implied. See the License for the specific language governing permissions and limitations under
12e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee * the License.
13e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee */
14e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Leepackage com.example.android.leanback;
15e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
16e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Leeimport android.animation.Animator;
17e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Leeimport android.animation.AnimatorListenerAdapter;
18e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Leeimport android.animation.AnimatorSet;
19e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Leeimport android.animation.ObjectAnimator;
20e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Leeimport android.support.v17.leanback.app.OnboardingFragment;
21e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Leeimport android.view.LayoutInflater;
22e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Leeimport android.view.View;
23e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Leeimport android.view.ViewGroup;
24e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Leeimport android.view.ViewGroup.MarginLayoutParams;
25e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Leeimport android.widget.ImageView;
26e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
27e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Leeimport java.util.ArrayList;
28e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
29e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Leepublic class OnboardingDemoFragment extends OnboardingFragment {
30e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    private static final long ANIMATION_DURATION = 1000;
31e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
32e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    private static final int[] CONTENT_IMAGES = {
33e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee            R.drawable.gallery_photo_1,
34e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee            R.drawable.gallery_photo_2,
35e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee            R.drawable.gallery_photo_3
36e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    };
37e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    private String[] mTitles;
38e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    private String[] mDescriptions;
39e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
40e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    private View mBackgroundView;
41e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    private ImageView mContentView;
42e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    private ImageView mImage1;
43e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    private ImageView mImage2;
44e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
45e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    private Animator mContentAnimator;
46e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
47e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    @Override
48e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    public void onAttach(android.app.Activity activity) {
49e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        super.onAttach(activity);
50e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        mTitles = getResources().getStringArray(R.array.onboarding_page_titles);
51e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        mDescriptions = getResources().getStringArray(R.array.onboarding_page_descriptions);
52e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    }
53e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
54e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    @Override
55e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    protected int getPageCount() {
56e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        return mTitles.length;
57e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    }
58e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
59e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    @Override
60e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    protected CharSequence getPageTitle(int i) {
61e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        return mTitles[i];
62e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    }
63e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
64e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    @Override
65e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    protected CharSequence getPageDescription(int i) {
66e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        return mDescriptions[i];
67e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    }
68e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
69e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    @Override
70e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    protected View onCreateBackgroundView(LayoutInflater layoutInflater, ViewGroup viewGroup) {
71e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        mBackgroundView = layoutInflater.inflate(R.layout.onboarding_image, viewGroup, false);
72e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        return mBackgroundView;
73e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    }
74e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
75e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    @Override
76e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    protected View onCreateContentView(LayoutInflater layoutInflater, ViewGroup viewGroup) {
77e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        mContentView = (ImageView) layoutInflater.inflate(R.layout.onboarding_image, viewGroup,
78e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee                false);
79e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        MarginLayoutParams layoutParams = ((MarginLayoutParams) mContentView.getLayoutParams());
80e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        layoutParams.topMargin = 30;
81e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        layoutParams.bottomMargin = 60;
82e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        return mContentView;
83e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    }
84e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
85e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    @Override
86e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    protected View onCreateForegroundView(LayoutInflater layoutInflater, ViewGroup viewGroup) {
87e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        return null;
88e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    }
89e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
90e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    @Override
91e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    protected Animator onCreateEnterAnimation() {
92e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        ArrayList<Animator> animators = new ArrayList<>();
93e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        animators.add(createFadeInAnimator(mBackgroundView));
94e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        mContentView.setImageResource(CONTENT_IMAGES[0]);
95e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        mContentAnimator = createFadeInAnimator(mContentView);
96e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        animators.add(mContentAnimator);
97e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        AnimatorSet set = new AnimatorSet();
98e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        set.playTogether(animators);
99e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        return set;
100e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    }
101e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
102e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    @Override
103e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    protected void onPageChanged(final int newPage, int previousPage) {
104e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        if (mContentAnimator != null) {
105e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee            mContentAnimator.end();
106e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        }
107e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        ArrayList<Animator> animators = new ArrayList<>();
108e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        Animator fadeOut = createFadeOutAnimator(mContentView);
109e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        fadeOut.addListener(new AnimatorListenerAdapter() {
110e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee            @Override
111e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee            public void onAnimationEnd(Animator animation) {
112e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee                mContentView.setImageResource(CONTENT_IMAGES[newPage]);
113e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee            }
114e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        });
115e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        animators.add(fadeOut);
116e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        animators.add(createFadeInAnimator(mContentView));
117e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        AnimatorSet set = new AnimatorSet();
118e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        set.playSequentially(animators);
119e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        set.start();
120e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        mContentAnimator = set;
121e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    }
122e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
123e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    private Animator createFadeInAnimator(View view) {
124e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        return ObjectAnimator.ofFloat(view, View.ALPHA, 0.0f, 1.0f).setDuration(ANIMATION_DURATION);
125e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    }
126e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
127e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    private Animator createFadeOutAnimator(View view) {
128e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        return ObjectAnimator.ofFloat(view, View.ALPHA, 1.0f, 0.0f).setDuration(ANIMATION_DURATION);
129e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    }
130e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee
131e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    @Override
132e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    protected void onFinishFragment() {
133e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee        getActivity().finish();
134e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee    }
135e3ef610a75fd382df930699ca42b61f24ac128f8Chulwoo Lee}
136