Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 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 | |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 19 | import android.content.Context; |
Winson Chung | 7819a56 | 2013-09-19 15:55:45 -0700 | [diff] [blame] | 20 | import android.content.res.Resources; |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 21 | import android.util.AttributeSet; |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 22 | import android.widget.FrameLayout; |
Michael Jurka | 34c2e6c | 2013-12-13 16:07:45 +0100 | [diff] [blame] | 23 | import android.widget.ImageView; |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 24 | |
| 25 | public class PageIndicatorMarker extends FrameLayout { |
| 26 | @SuppressWarnings("unused") |
| 27 | private static final String TAG = "PageIndicator"; |
| 28 | |
Winson Chung | 5bc865e | 2013-07-18 15:18:25 -0700 | [diff] [blame] | 29 | private static final int MARKER_FADE_DURATION = 175; |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 30 | |
Winson Chung | 7819a56 | 2013-09-19 15:55:45 -0700 | [diff] [blame] | 31 | private ImageView mActiveMarker; |
| 32 | private ImageView mInactiveMarker; |
Winson Chung | 5bc865e | 2013-07-18 15:18:25 -0700 | [diff] [blame] | 33 | private boolean mIsActive = false; |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 34 | |
| 35 | public PageIndicatorMarker(Context context) { |
| 36 | this(context, null); |
| 37 | } |
| 38 | |
| 39 | public PageIndicatorMarker(Context context, AttributeSet attrs) { |
| 40 | this(context, attrs, 0); |
| 41 | } |
| 42 | |
| 43 | public PageIndicatorMarker(Context context, AttributeSet attrs, int defStyle) { |
| 44 | super(context, attrs, defStyle); |
| 45 | } |
| 46 | |
| 47 | protected void onFinishInflate() { |
Winson Chung | 7819a56 | 2013-09-19 15:55:45 -0700 | [diff] [blame] | 48 | mActiveMarker = (ImageView) findViewById(R.id.active); |
| 49 | mInactiveMarker = (ImageView) findViewById(R.id.inactive); |
| 50 | } |
| 51 | |
| 52 | void setMarkerDrawables(int activeResId, int inactiveResId) { |
| 53 | Resources r = getResources(); |
| 54 | mActiveMarker.setImageDrawable(r.getDrawable(activeResId)); |
| 55 | mInactiveMarker.setImageDrawable(r.getDrawable(inactiveResId)); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Winson Chung | 5bc865e | 2013-07-18 15:18:25 -0700 | [diff] [blame] | 58 | void activate(boolean immediate) { |
| 59 | if (immediate) { |
| 60 | mActiveMarker.animate().cancel(); |
| 61 | mActiveMarker.setAlpha(1f); |
Daniel Sandler | bb701aa | 2013-09-19 15:03:00 -0400 | [diff] [blame] | 62 | mActiveMarker.setScaleX(1f); |
| 63 | mActiveMarker.setScaleY(1f); |
Winson Chung | 5bc865e | 2013-07-18 15:18:25 -0700 | [diff] [blame] | 64 | mInactiveMarker.animate().cancel(); |
| 65 | mInactiveMarker.setAlpha(0f); |
| 66 | } else { |
Daniel Sandler | bb701aa | 2013-09-19 15:03:00 -0400 | [diff] [blame] | 67 | mActiveMarker.animate() |
| 68 | .alpha(1f) |
| 69 | .scaleX(1f) |
| 70 | .scaleY(1f) |
Winson Chung | 5bc865e | 2013-07-18 15:18:25 -0700 | [diff] [blame] | 71 | .setDuration(MARKER_FADE_DURATION).start(); |
Daniel Sandler | bb701aa | 2013-09-19 15:03:00 -0400 | [diff] [blame] | 72 | mInactiveMarker.animate() |
| 73 | .alpha(0f) |
Winson Chung | 5bc865e | 2013-07-18 15:18:25 -0700 | [diff] [blame] | 74 | .setDuration(MARKER_FADE_DURATION).start(); |
| 75 | } |
| 76 | mIsActive = true; |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 77 | } |
Daniel Sandler | bb701aa | 2013-09-19 15:03:00 -0400 | [diff] [blame] | 78 | |
Winson Chung | 5bc865e | 2013-07-18 15:18:25 -0700 | [diff] [blame] | 79 | void inactivate(boolean immediate) { |
| 80 | if (immediate) { |
| 81 | mInactiveMarker.animate().cancel(); |
| 82 | mInactiveMarker.setAlpha(1f); |
| 83 | mActiveMarker.animate().cancel(); |
| 84 | mActiveMarker.setAlpha(0f); |
Daniel Sandler | bb701aa | 2013-09-19 15:03:00 -0400 | [diff] [blame] | 85 | mActiveMarker.setScaleX(0.5f); |
| 86 | mActiveMarker.setScaleY(0.5f); |
Winson Chung | 5bc865e | 2013-07-18 15:18:25 -0700 | [diff] [blame] | 87 | } else { |
| 88 | mInactiveMarker.animate().alpha(1f) |
| 89 | .setDuration(MARKER_FADE_DURATION).start(); |
Daniel Sandler | bb701aa | 2013-09-19 15:03:00 -0400 | [diff] [blame] | 90 | mActiveMarker.animate() |
| 91 | .alpha(0f) |
| 92 | .scaleX(0.5f) |
| 93 | .scaleY(0.5f) |
Winson Chung | 5bc865e | 2013-07-18 15:18:25 -0700 | [diff] [blame] | 94 | .setDuration(MARKER_FADE_DURATION).start(); |
| 95 | } |
| 96 | mIsActive = false; |
| 97 | } |
| 98 | |
| 99 | boolean isActive() { |
| 100 | return mIsActive; |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 101 | } |
| 102 | } |