CameraSwitcher.java revision 6478e5e71f631781e5c0e3f30560da4ca74901bb
1d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb/*
2d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb * Copyright (C) 2012 The Android Open Source Project
3d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb *
4d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb * Licensed under the Apache License, Version 2.0 (the "License");
5d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb * you may not use this file except in compliance with the License.
6d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb * You may obtain a copy of the License at
7d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb *
8d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb *      http://www.apache.org/licenses/LICENSE-2.0
9d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb *
10d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb * Unless required by applicable law or agreed to in writing, software
11d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb * distributed under the License is distributed on an "AS IS" BASIS,
12d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb * See the License for the specific language governing permissions and
14d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb * limitations under the License.
15d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb */
16d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
17d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbpackage com.android.camera.ui;
18d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
19ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reckimport android.animation.Animator;
20ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reckimport android.animation.Animator.AnimatorListener;
21ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reckimport android.animation.AnimatorListenerAdapter;
22d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.content.Context;
236478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liuimport android.content.res.Configuration;
24f16332047e1c7627c20cdf847520a51f2a89d7b0Michael Kolbimport android.graphics.Canvas;
25f16332047e1c7627c20cdf847520a51f2a89d7b0Michael Kolbimport android.graphics.drawable.Drawable;
26d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.util.AttributeSet;
27a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolbimport android.view.LayoutInflater;
285da800f7a1427066a310a41964d780924c1f1a10Michael Kolbimport android.view.MotionEvent;
29d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.view.View;
30a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolbimport android.view.View.OnClickListener;
31a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolbimport android.view.View.OnTouchListener;
32d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.view.ViewGroup;
33d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.widget.LinearLayout;
34d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
35a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolbimport com.android.camera.R;
36ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reckimport com.android.gallery3d.common.ApiHelper;
37d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
38a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolbpublic class CameraSwitcher extends RotateImageView
39a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        implements OnClickListener, OnTouchListener {
40d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
41a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    private static final String TAG = "CAM_Switcher";
426478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu    private static final int SWITCHER_POPUP_ANIM_DURATION = 200;
43d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
44d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public interface CameraSwitchListener {
45d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public void onCameraSelected(int i);
46561af39348f34db4d67bc954536d6f6cbf296054John Reck        public void onShowSwitcherPopup();
47d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
48d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
49d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private CameraSwitchListener mListener;
50a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    private int mCurrentIndex;
51a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    private int[] mDrawIds;
52a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    private int mItemSize;
53a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    private View mPopup;
54a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    private View mParent;
55ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck    private boolean mShowingPopup;
56ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck    private boolean mNeedsAnimationSetup;
57f16332047e1c7627c20cdf847520a51f2a89d7b0Michael Kolb    private Drawable mIndicator;
58ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck
596478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu    private float mTranslationX = 0;
606478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu    private float mTranslationY = 0;
616478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu
62ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck    private AnimatorListener mHideAnimationListener;
63ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck    private AnimatorListener mShowAnimationListener;
64d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
65d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public CameraSwitcher(Context context) {
66d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        super(context);
67d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        init(context);
68d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
69d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
70d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public CameraSwitcher(Context context, AttributeSet attrs) {
71d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        super(context, attrs);
72d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        init(context);
73d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
74d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
75d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void init(Context context) {
76a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        mItemSize = context.getResources().getDimensionPixelSize(R.dimen.switcher_size);
77a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        setOnClickListener(this);
78f16332047e1c7627c20cdf847520a51f2a89d7b0Michael Kolb        mIndicator = context.getResources().getDrawable(R.drawable.ic_switcher_menu_indicator);
79d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
80d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
81a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    public void setDrawIds(int[] drawids) {
82a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        mDrawIds = drawids;
83df0dca9dd26b95a424a5712f16a43413a3e8ebe1Michael Kolb    }
84df0dca9dd26b95a424a5712f16a43413a3e8ebe1Michael Kolb
85a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    public void setCurrentIndex(int i) {
86a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        mCurrentIndex = i;
87a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        setImageResource(mDrawIds[i]);
88d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
89d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
90d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void setSwitchListener(CameraSwitchListener l) {
91d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mListener = l;
92d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
93d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
94a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    @Override
95a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    public void onClick(View v) {
96a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        showSwitcher();
97561af39348f34db4d67bc954536d6f6cbf296054John Reck        mListener.onShowSwitcherPopup();
981da18852b3cddf3e2f288636a9ef9d8d002f496bJohn Reck    }
991da18852b3cddf3e2f288636a9ef9d8d002f496bJohn Reck
100a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    private void onCameraSelected(int ix) {
101a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        hidePopup();
102a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        if ((ix != mCurrentIndex) && (mListener != null)) {
103a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb            setCurrentIndex(ix);
104a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb            mListener.onCameraSelected(ix);
1051da18852b3cddf3e2f288636a9ef9d8d002f496bJohn Reck        }
106d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
107d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
108f16332047e1c7627c20cdf847520a51f2a89d7b0Michael Kolb    @Override
109f16332047e1c7627c20cdf847520a51f2a89d7b0Michael Kolb    protected void onDraw(Canvas canvas) {
110f16332047e1c7627c20cdf847520a51f2a89d7b0Michael Kolb        super.onDraw(canvas);
111f16332047e1c7627c20cdf847520a51f2a89d7b0Michael Kolb        mIndicator.setBounds(getDrawable().getBounds());
112f16332047e1c7627c20cdf847520a51f2a89d7b0Michael Kolb        mIndicator.draw(canvas);
113f16332047e1c7627c20cdf847520a51f2a89d7b0Michael Kolb    }
114f16332047e1c7627c20cdf847520a51f2a89d7b0Michael Kolb
115a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    private void initPopup() {
116a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        mParent = LayoutInflater.from(getContext()).inflate(R.layout.switcher_popup,
117a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb                (ViewGroup) getParent());
118a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        LinearLayout content = (LinearLayout) mParent.findViewById(R.id.content);
119a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        mPopup = content;
120ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        mPopup.setVisibility(View.INVISIBLE);
121ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        mNeedsAnimationSetup = true;
122b1be74d6833cd55429cc5c34885eddf613d08302Doris Liu        for (int i = mDrawIds.length - 1; i >= 0; i--) {
123a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb            RotateImageView item = new RotateImageView(getContext());
124a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb            item.setImageResource(mDrawIds[i]);
125da04f24a205d92c72c32d7768f0d3b4b1f93512dMichael Kolb            item.setBackgroundResource(R.drawable.bg_pressed);
126a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb            final int index = i;
127a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb            item.setOnClickListener(new OnClickListener() {
128a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb                @Override
129a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb                public void onClick(View v) {
130a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb                    onCameraSelected(index);
131a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb                }
132a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb            });
133d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu            switch (mDrawIds[i]) {
134d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                case R.drawable.ic_switch_camera:
135d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                    item.setContentDescription(getContext().getResources().getString(
136d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                            R.string.accessibility_switch_to_camera));
137d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                    break;
138d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                case R.drawable.ic_switch_video:
139d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                    item.setContentDescription(getContext().getResources().getString(
140d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                            R.string.accessibility_switch_to_video));
141d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                    break;
142d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                case R.drawable.ic_switch_pan:
143d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                    item.setContentDescription(getContext().getResources().getString(
144d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                            R.string.accessibility_switch_to_panorama));
145d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                    break;
146ebf2df0f2696dd287a8235cf4e7c1cac0103a3deMichael Kolb                case R.drawable.ic_switch_photosphere:
147d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                    item.setContentDescription(getContext().getResources().getString(
14821bb06827d9fec3554ee97c8a2c7b2f00dda7778Doris Liu                            R.string.accessibility_switch_to_photosphere));
149d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                    break;
150d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                default:
151d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                    break;
152d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu            }
153a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb            content.addView(item, new LinearLayout.LayoutParams(mItemSize, mItemSize));
154d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
155d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
156d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
157a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    public boolean showsPopup() {
158ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        return mShowingPopup;
159d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
160d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
161a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    public boolean isInsidePopup(MotionEvent evt) {
162ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        if (!showsPopup()) return false;
163ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        return evt.getX() >= mPopup.getLeft()
164ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                && evt.getX() < mPopup.getRight()
165ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                && evt.getY() >= mPopup.getTop()
166ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                && evt.getY() < mPopup.getBottom();
16778711c990e57ce7c6228c5446cfb6902f2a47cb6Doris Liu    }
16878711c990e57ce7c6228c5446cfb6902f2a47cb6Doris Liu
169a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    private void hidePopup() {
170ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        mShowingPopup = false;
171ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        setVisibility(View.VISIBLE);
172ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        if (mPopup != null && !animateHidePopup()) {
173ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck            mPopup.setVisibility(View.INVISIBLE);
174d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
175a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        mParent.setOnTouchListener(null);
176d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
177d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
178a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    private void showSwitcher() {
179ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        mShowingPopup = true;
180a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        if (mPopup == null) {
181a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb            initPopup();
182a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        }
183a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        mPopup.setVisibility(View.VISIBLE);
184ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        if (!animateShowPopup()) {
185ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck            setVisibility(View.INVISIBLE);
186ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        }
187a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        mParent.setOnTouchListener(this);
1888566e6d534634965a53b536571008bad882b969dMichael Kolb    }
1898566e6d534634965a53b536571008bad882b969dMichael Kolb
1908566e6d534634965a53b536571008bad882b969dMichael Kolb    @Override
191a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    public boolean onTouch(View v, MotionEvent event) {
192a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        if (showsPopup()) {
193a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb            hidePopup();
194d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
195a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        return true;
196d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
197d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
198a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    @Override
199a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    public void setOrientation(int degree, boolean animate) {
200a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        super.setOrientation(degree, animate);
201a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        ViewGroup content = (ViewGroup) mPopup;
202a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        if (content == null) return;
203a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        for (int i = 0; i < content.getChildCount(); i++) {
204a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb            RotateImageView iv = (RotateImageView) content.getChildAt(i);
205a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb            iv.setOrientation(degree, animate);
206d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
207d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
208ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck
2096478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu    private void updateInitialTranslations() {
2106478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu        if (getResources().getConfiguration().orientation
2116478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu                == Configuration.ORIENTATION_PORTRAIT) {
2126478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu            mTranslationX = -getWidth() / 2 ;
2136478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu            mTranslationY = getHeight();
2146478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu        } else {
2156478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu            mTranslationX = getWidth();
2166478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu            mTranslationY = getHeight() / 2;
2176478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu        }
2186478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu    }
219ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck    private void popupAnimationSetup() {
220ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        if (!ApiHelper.HAS_VIEW_PROPERTY_ANIMATOR) {
221ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck            return;
222ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        }
2236478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu        updateInitialTranslations();
224ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        mPopup.setScaleX(0.3f);
225ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        mPopup.setScaleY(0.3f);
2266478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu        mPopup.setTranslationX(mTranslationX);
2276478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu        mPopup.setTranslationY(mTranslationY);
228ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        mNeedsAnimationSetup = false;
229ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck    }
230ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck
231ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck    private boolean animateHidePopup() {
232ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        if (!ApiHelper.HAS_VIEW_PROPERTY_ANIMATOR) {
233ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck            return false;
234ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        }
235ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        if (mHideAnimationListener == null) {
236ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck            mHideAnimationListener = new AnimatorListenerAdapter() {
237ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                @Override
238ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                public void onAnimationEnd(Animator animation) {
239ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                    // Verify that we weren't canceled
240ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                    if (!showsPopup()) {
241ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                        mPopup.setVisibility(View.INVISIBLE);
242ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                    }
243ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                }
244ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck            };
245ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        }
246ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        mPopup.animate()
247ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                .alpha(0f)
248ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                .scaleX(0.3f).scaleY(0.3f)
2496478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu                .translationX(mTranslationX)
2506478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu                .translationY(mTranslationY)
2516478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu                .setDuration(SWITCHER_POPUP_ANIM_DURATION)
252ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                .setListener(mHideAnimationListener);
2536478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu        animate().alpha(1f).setDuration(SWITCHER_POPUP_ANIM_DURATION)
2546478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu                .setListener(null);
255ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        return true;
256ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck    }
257ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck
258ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck    private boolean animateShowPopup() {
259ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        if (!ApiHelper.HAS_VIEW_PROPERTY_ANIMATOR) {
260ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck            return false;
261ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        }
262ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        if (mNeedsAnimationSetup) {
263ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck            popupAnimationSetup();
264ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        }
265ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        if (mShowAnimationListener == null) {
266ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck            mShowAnimationListener = new AnimatorListenerAdapter() {
267ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                @Override
268ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                public void onAnimationEnd(Animator animation) {
269ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                    // Verify that we weren't canceled
270ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                    if (showsPopup()) {
271ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                        setVisibility(View.INVISIBLE);
272ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                    }
273ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                }
274ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck            };
275ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        }
276ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        mPopup.animate()
277ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                .alpha(1f)
278ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                .scaleX(1f).scaleY(1f)
279ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                .translationX(0)
280ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                .translationY(0)
2816478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu                .setDuration(SWITCHER_POPUP_ANIM_DURATION)
282ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                .setListener(null);
2836478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu        animate().alpha(0f).setDuration(SWITCHER_POPUP_ANIM_DURATION)
284ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                .setListener(mShowAnimationListener);
285ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        return true;
286ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck    }
287d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb}
288