Migrate to gradle, appcompat as external dependency.
[mobilnebezpieczenstwo.git] / app / src / main / java / com / samsung / srpol / parallax / ParallaxedView.java
1 /*
2     The MIT License (MIT)
3     
4     Copyright (c) 2014 Nir Hartmann
5     
6     Permission is hereby granted, free of charge, to any person obtaining a copy
7     of this software and associated documentation files (the "Software"), to deal
8     in the Software without restriction, including without limitation the rights
9     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10     copies of the Software, and to permit persons to whom the Software is
11     furnished to do so, subject to the following conditions:
12     
13     The above copyright notice and this permission notice shall be included in all
14     copies or substantial portions of the Software.
15     
16     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22     SOFTWARE.
23  */
24 package com.samsung.srpol.parallax;
25
26 import java.lang.ref.WeakReference;
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import android.annotation.SuppressLint;
31 import android.os.Build;
32 import android.view.View;
33 import android.view.animation.AlphaAnimation;
34 import android.view.animation.Animation;
35 import android.view.animation.AnimationSet;
36
37 public abstract class ParallaxedView {
38     static public boolean isAPI11 = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
39     protected WeakReference<View> view;
40     protected int lastOffset;
41     protected List<Animation> animations;
42
43     abstract protected void translatePreICS(View view, float offset);
44
45     public ParallaxedView(View view) {
46         this.lastOffset = 0;
47         this.animations = new ArrayList<Animation>();
48         this.view = new WeakReference<View>(view);
49     }
50
51     public boolean is(View v) {
52         return (v != null && view != null && view.get() != null && view.get()
53                 .equals(v));
54     }
55
56     @SuppressLint("NewApi")
57     public void setOffset(float offset) {
58         View view = this.view.get();
59         if (view != null)
60             if (isAPI11) {
61                 view.setTranslationY(offset);
62             } else {
63                 translatePreICS(view, offset);
64             }
65     }
66
67     public void setAlpha(float alpha) {
68         View view = this.view.get();
69         if (view != null)
70             if (isAPI11) {
71                 view.setAlpha(alpha);
72             } else {
73                 alphaPreICS(view, alpha);
74             }
75     }
76
77     protected synchronized void addAnimation(Animation animation) {
78         animations.add(animation);
79     }
80
81     protected void alphaPreICS(View view, float alpha) {
82         addAnimation(new AlphaAnimation(alpha, alpha));
83     }
84
85     protected synchronized void animateNow() {
86         View view = this.view.get();
87         if (view != null) {
88             AnimationSet set = new AnimationSet(true);
89             for (Animation animation : animations)
90                 if (animation != null)
91                     set.addAnimation(animation);
92             set.setDuration(0);
93             set.setFillAfter(true);
94             view.setAnimation(set);
95             set.start();
96             animations.clear();
97         }
98     }
99
100     public void setView(View view) {
101         this.view = new WeakReference<View>(view);
102     }
103
104     public View getView() {
105         return this.view.get();
106     }
107 }