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 */ 14package com.example.android.leanback; 15 16import android.animation.Animator; 17import android.animation.AnimatorListenerAdapter; 18import android.animation.AnimatorSet; 19import android.animation.ObjectAnimator; 20import android.graphics.drawable.AnimationDrawable; 21import android.support.v17.leanback.app.OnboardingFragment; 22import android.view.LayoutInflater; 23import android.view.View; 24import android.view.ViewGroup; 25import android.widget.ImageView; 26 27import java.util.ArrayList; 28 29public 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 @SuppressWarnings("deprecation") 55 @Override 56 public void onAttach(android.app.Activity activity) { 57 super.onAttach(activity); 58 mTitles = getResources().getStringArray(R.array.onboarding_page_titles); 59 mDescriptions = getResources().getStringArray(R.array.onboarding_page_descriptions); 60 setLogoResourceId(R.drawable.ic_launcher); 61 } 62 63 @Override 64 protected int getPageCount() { 65 return mTitles.length; 66 } 67 68 @Override 69 protected CharSequence getPageTitle(int i) { 70 return mTitles[i]; 71 } 72 73 @Override 74 protected CharSequence getPageDescription(int i) { 75 return mDescriptions[i]; 76 } 77 78 @Override 79 protected View onCreateBackgroundView(LayoutInflater layoutInflater, ViewGroup viewGroup) { 80 mBackgroundView = layoutInflater.inflate(R.layout.onboarding_image, viewGroup, false); 81 return mBackgroundView; 82 } 83 84 @Override 85 protected View onCreateContentView(LayoutInflater layoutInflater, ViewGroup viewGroup) { 86 mContentView = layoutInflater.inflate(R.layout.onboarding_content, viewGroup, false); 87 mContentBackgroundView = (ImageView) mContentView.findViewById(R.id.background_image); 88 mContentAnimationView = (ImageView) mContentView.findViewById(R.id.animation_image); 89 return mContentView; 90 } 91 92 @Override 93 protected View onCreateForegroundView(LayoutInflater layoutInflater, ViewGroup viewGroup) { 94 return null; 95 } 96 97 @Override 98 protected Animator onCreateEnterAnimation() { 99 ArrayList<Animator> animators = new ArrayList<>(); 100 animators.add(createFadeInAnimator(mBackgroundView)); 101 mContentBackgroundView.setImageResource(CONTENT_BACKGROUNDS[0]); 102 mContentAnimationView.setImageResource(CONTENT_ANIMATIONS[0]); 103 mContentAnimator = createFadeInAnimator(mContentView); 104 animators.add(mContentAnimator); 105 AnimatorSet set = new AnimatorSet(); 106 set.playTogether(animators); 107 set.addListener(new AnimatorListenerAdapter() { 108 @Override 109 public void onAnimationEnd(Animator animation) { 110 ((AnimationDrawable) mContentAnimationView.getDrawable()).start(); 111 } 112 }); 113 return set; 114 } 115 116 @Override 117 protected void onPageChanged(final int newPage, int previousPage) { 118 if (mContentAnimator != null) { 119 mContentAnimator.cancel(); 120 } 121 ((AnimationDrawable) mContentAnimationView.getDrawable()).stop(); 122 ArrayList<Animator> animators = new ArrayList<>(); 123 Animator fadeOut = createFadeOutAnimator(mContentView); 124 fadeOut.addListener(new AnimatorListenerAdapter() { 125 @Override 126 public void onAnimationEnd(Animator animation) { 127 mContentBackgroundView.setImageResource(CONTENT_BACKGROUNDS[newPage]); 128 mContentAnimationView.setImageResource(CONTENT_ANIMATIONS[newPage]); 129 } 130 }); 131 Animator fadeIn = createFadeInAnimator(mContentView); 132 fadeIn.addListener(new AnimatorListenerAdapter() { 133 @Override 134 public void onAnimationEnd(Animator animation) { 135 ((AnimationDrawable) mContentAnimationView.getDrawable()).start(); 136 } 137 }); 138 AnimatorSet set = new AnimatorSet(); 139 set.playSequentially(fadeOut, fadeIn); 140 set.start(); 141 mContentAnimator = set; 142 } 143 144 private Animator createFadeInAnimator(View view) { 145 return ObjectAnimator.ofFloat(view, View.ALPHA, 0.0f, 1.0f).setDuration(ANIMATION_DURATION); 146 } 147 148 private Animator createFadeOutAnimator(View view) { 149 return ObjectAnimator.ofFloat(view, View.ALPHA, 1.0f, 0.0f).setDuration(ANIMATION_DURATION); 150 } 151 152 @Override 153 protected void onFinishFragment() { 154 getActivity().finish(); 155 } 156} 157