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