2 * Copyright (C) 2016 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.android.settings;
18 import android.animation.Animator;
19 import android.animation.Animator.AnimatorListener;
20 import android.content.Context;
21 import android.content.res.Configuration;
22 import android.support.v4.view.PagerAdapter;
23 import android.view.LayoutInflater;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.view.animation.AccelerateInterpolator;
27 import android.view.animation.DecelerateInterpolator;
28 import android.view.animation.Interpolator;
29 import android.widget.FrameLayout;
30 import android.widget.LinearLayout;
31 import android.widget.ScrollView;
34 * A PagerAdapter used by PreviewSeekBarPreferenceFragment that for showing multiple preview screen
35 * regarding a single setting and allowing the user to swipe across them.
37 public class PreviewPagerAdapter extends PagerAdapter {
39 /** Duration to use when cross-fading between previews. */
40 private static final long CROSS_FADE_DURATION_MS = 400;
42 /** Interpolator to use when cross-fading between previews. */
43 private static final Interpolator FADE_IN_INTERPOLATOR = new DecelerateInterpolator();
45 /** Interpolator to use when cross-fading between previews. */
46 private static final Interpolator FADE_OUT_INTERPOLATOR = new AccelerateInterpolator();
48 private final FrameLayout[] mPreviewFrames;
50 private Runnable mAnimationEndAction;
52 private int mAnimationCounter;
54 public PreviewPagerAdapter(Context context, int[] previewSampleResIds,
55 Configuration[] configurations) {
56 mPreviewFrames = new FrameLayout[previewSampleResIds.length];
58 for (int i = 0; i < previewSampleResIds.length; ++i) {
59 mPreviewFrames[i] = new FrameLayout(context);
60 mPreviewFrames[i].setLayoutParams(new LinearLayout.LayoutParams(
61 LinearLayout.LayoutParams.MATCH_PARENT,
62 LinearLayout.LayoutParams.MATCH_PARENT));
64 for (Configuration configuration : configurations) {
65 // Create a new configuration for the specified value. It won't
66 // have any theme set, so manually apply the current theme.
67 final Context configContext = context.createConfigurationContext(configuration);
68 configContext.setTheme(context.getThemeResId());
70 final LayoutInflater configInflater = LayoutInflater.from(configContext);
71 final View sampleView = configInflater.inflate(previewSampleResIds[i],
72 mPreviewFrames[i], false);
73 sampleView.setAlpha(0);
74 sampleView.setVisibility(View.INVISIBLE);
75 mPreviewFrames[i].addView(sampleView);
81 public void destroyItem (ViewGroup container, int position, Object object) {
82 container.removeView((View) object);
86 public int getCount() {
87 return mPreviewFrames.length;
91 public Object instantiateItem(ViewGroup container, int position) {
92 container.addView(mPreviewFrames[position]);
93 return mPreviewFrames[position];
97 public boolean isViewFromObject(View view, Object object) {
98 return (view == object);
101 boolean isAnimating() {
102 return mAnimationCounter > 0;
105 void setAnimationEndAction(Runnable action) {
106 mAnimationEndAction = action;
109 void setPreviewLayer(int newIndex, int currentIndex, int currentItem, boolean animate) {
110 for (FrameLayout previewFrame : mPreviewFrames) {
111 if (currentIndex >= 0) {
112 final View lastLayer = previewFrame.getChildAt(currentIndex);
113 if (animate && previewFrame == mPreviewFrames[currentItem]) {
116 .setInterpolator(FADE_OUT_INTERPOLATOR)
117 .setDuration(CROSS_FADE_DURATION_MS)
118 .setListener(new PreviewFrameAnimatorListener())
119 .withEndAction(new Runnable() {
122 lastLayer.setVisibility(View.INVISIBLE);
126 lastLayer.setAlpha(0);
127 lastLayer.setVisibility(View.INVISIBLE);
131 final View nextLayer = previewFrame.getChildAt(newIndex);
132 if (animate && previewFrame == mPreviewFrames[currentItem]) {
135 .setInterpolator(FADE_IN_INTERPOLATOR)
136 .setDuration(CROSS_FADE_DURATION_MS)
137 .setListener(new PreviewFrameAnimatorListener())
138 .withStartAction(new Runnable() {
141 nextLayer.setVisibility(View.VISIBLE);
145 nextLayer.setVisibility(View.VISIBLE);
146 nextLayer.setAlpha(1);
151 private void runAnimationEndAction() {
152 if (mAnimationEndAction != null && !isAnimating()) {
153 mAnimationEndAction.run();
154 mAnimationEndAction = null;
158 private class PreviewFrameAnimatorListener implements AnimatorListener {
160 public void onAnimationStart(Animator animation) {
165 public void onAnimationEnd(Animator animation) {
167 runAnimationEndAction();
171 public void onAnimationCancel(Animator animation) {
176 public void onAnimationRepeat(Animator animation) {