1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 
17 package com.android.launcher3;
18 
19 import android.content.Context;
20 import android.content.res.Resources;
21 import android.util.AttributeSet;
22 import android.widget.FrameLayout;
23 import android.widget.ImageView;
24 
25 public class PageIndicatorMarker extends FrameLayout {
26     @SuppressWarnings("unused")
27     private static final String TAG = "PageIndicator";
28 
29     private static final int MARKER_FADE_DURATION = 175;
30 
31     private ImageView mActiveMarker;
32     private ImageView mInactiveMarker;
33     private boolean mIsActive = false;
34 
PageIndicatorMarker(Context context)35     public PageIndicatorMarker(Context context) {
36         this(context, null);
37     }
38 
PageIndicatorMarker(Context context, AttributeSet attrs)39     public PageIndicatorMarker(Context context, AttributeSet attrs) {
40         this(context, attrs, 0);
41     }
42 
PageIndicatorMarker(Context context, AttributeSet attrs, int defStyle)43     public PageIndicatorMarker(Context context, AttributeSet attrs, int defStyle) {
44         super(context, attrs, defStyle);
45     }
46 
onFinishInflate()47     protected void onFinishInflate() {
48         super.onFinishInflate();
49         mActiveMarker = (ImageView) findViewById(R.id.active);
50         mInactiveMarker = (ImageView) findViewById(R.id.inactive);
51     }
52 
setMarkerDrawables(int activeResId, int inactiveResId)53     void setMarkerDrawables(int activeResId, int inactiveResId) {
54         Resources r = getResources();
55         mActiveMarker.setImageDrawable(r.getDrawable(activeResId));
56         mInactiveMarker.setImageDrawable(r.getDrawable(inactiveResId));
57     }
58 
activate(boolean immediate)59     void activate(boolean immediate) {
60         if (immediate) {
61             mActiveMarker.animate().cancel();
62             mActiveMarker.setAlpha(1f);
63             mActiveMarker.setScaleX(1f);
64             mActiveMarker.setScaleY(1f);
65             mInactiveMarker.animate().cancel();
66             mInactiveMarker.setAlpha(0f);
67         } else {
68             mActiveMarker.animate()
69                     .alpha(1f)
70                     .scaleX(1f)
71                     .scaleY(1f)
72                     .setDuration(MARKER_FADE_DURATION).start();
73             mInactiveMarker.animate()
74                     .alpha(0f)
75                     .setDuration(MARKER_FADE_DURATION).start();
76         }
77         mIsActive = true;
78     }
79 
inactivate(boolean immediate)80     void inactivate(boolean immediate) {
81         if (immediate) {
82             mInactiveMarker.animate().cancel();
83             mInactiveMarker.setAlpha(1f);
84             mActiveMarker.animate().cancel();
85             mActiveMarker.setAlpha(0f);
86             mActiveMarker.setScaleX(0.5f);
87             mActiveMarker.setScaleY(0.5f);
88         } else {
89             mInactiveMarker.animate().alpha(1f)
90                     .setDuration(MARKER_FADE_DURATION).start();
91             mActiveMarker.animate()
92                     .alpha(0f)
93                     .scaleX(0.5f)
94                     .scaleY(0.5f)
95                     .setDuration(MARKER_FADE_DURATION).start();
96         }
97         mIsActive = false;
98     }
99 
isActive()100     boolean isActive() {
101         return mIsActive;
102     }
103 }
104