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.content.Context;
19 import android.content.res.Configuration;
20 import android.support.v4.view.PagerAdapter;
21 import android.view.LayoutInflater;
22 import android.view.View;
23 import android.view.ViewGroup;
24 import android.view.animation.AccelerateInterpolator;
25 import android.view.animation.DecelerateInterpolator;
26 import android.view.animation.Interpolator;
27 import android.widget.FrameLayout;
28 import android.widget.LinearLayout;
29 import android.widget.ScrollView;
32 * A PagerAdapter used by PreviewSeekBarPreferenceFragment that for showing multiple preview screen
33 * regarding a single setting and allowing the user to swipe across them.
35 public class PreviewPagerAdapter extends PagerAdapter {
37 private FrameLayout[] mPreviewFrames;
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 public PreviewPagerAdapter(Context context, int[] previewSampleResIds,
49 Configuration[] configurations) {
50 mPreviewFrames = new FrameLayout[previewSampleResIds.length];
52 for (int i = 0; i < previewSampleResIds.length; ++i) {
53 mPreviewFrames[i] = new FrameLayout(context);
54 mPreviewFrames[i].setLayoutParams(new LinearLayout.LayoutParams(
55 LinearLayout.LayoutParams.MATCH_PARENT,
56 LinearLayout.LayoutParams.MATCH_PARENT));
57 mPreviewFrames[i].setContentDescription(
58 context.getString(R.string.preview_page_indicator_content_description, i + 1,
59 previewSampleResIds.length));
61 for (Configuration configuration : configurations) {
62 // Create a new configuration for the specified value. It won't
63 // have any theme set, so manually apply the current theme.
64 final Context configContext = context.createConfigurationContext(configuration);
65 configContext.setTheme(context.getThemeResId());
67 final LayoutInflater configInflater = LayoutInflater.from(configContext);
68 final View sampleView = configInflater.inflate(previewSampleResIds[i],
69 mPreviewFrames[i], false);
70 sampleView.setAlpha(0);
71 sampleView.setVisibility(View.INVISIBLE);
72 mPreviewFrames[i].addView(sampleView);
78 public void destroyItem (ViewGroup container, int position, Object object) {
79 container.removeView((View) object);
83 public int getCount() {
84 return mPreviewFrames.length;
88 public Object instantiateItem(ViewGroup container, int position) {
89 container.addView(mPreviewFrames[position]);
90 return mPreviewFrames[position];
94 public boolean isViewFromObject(View view, Object object) {
95 return (view == object);
98 void setPreviewLayer(int newIndex, int currentIndex, int currentItem, boolean animate) {
99 for (FrameLayout previewFrame : mPreviewFrames) {
100 if (currentIndex >= 0) {
101 final View lastLayer = previewFrame.getChildAt(currentIndex);
102 if (animate && previewFrame == mPreviewFrames[currentItem]) {
105 .setInterpolator(FADE_OUT_INTERPOLATOR)
106 .setDuration(CROSS_FADE_DURATION_MS)
107 .withEndAction(new Runnable() {
110 lastLayer.setVisibility(View.INVISIBLE);
114 lastLayer.setAlpha(0);
115 lastLayer.setVisibility(View.INVISIBLE);
119 final View nextLayer = previewFrame.getChildAt(newIndex);
120 if (animate && previewFrame == mPreviewFrames[currentItem]) {
123 .setInterpolator(FADE_IN_INTERPOLATOR)
124 .setDuration(CROSS_FADE_DURATION_MS)
125 .withStartAction(new Runnable() {
128 nextLayer.setVisibility(View.VISIBLE);
132 nextLayer.setVisibility(View.VISIBLE);
133 nextLayer.setAlpha(1);