Initial commit.
[mobilnebezpieczenstwo.git] / src / com / samsung / srpol / parallax / ParallaxListViewHelper.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 com.samsung.srpol.R;
27
28 import android.content.Context;
29 import android.content.res.TypedArray;
30 import android.util.AttributeSet;
31 import android.view.View;
32 import android.view.animation.TranslateAnimation;
33 import android.widget.AbsListView;
34 import android.widget.AbsListView.OnScrollListener;
35 import android.widget.ListView;
36
37 public class ParallaxListViewHelper implements OnScrollListener {
38
39     private static final float DEFAULT_ALPHA_FACTOR = -1F;
40     private static final float DEFAULT_PARALLAX_FACTOR = 1.9F;
41     private static final boolean DEFAULT_IS_CIRCULAR = false;
42     private float parallaxFactor = DEFAULT_PARALLAX_FACTOR;
43     private float alphaFactor = DEFAULT_ALPHA_FACTOR;
44     private ParallaxedView parallaxedView;
45     private boolean isCircular;
46     private OnScrollListener listener = null;
47     private ListView listView;
48
49     protected ParallaxListViewHelper(Context context, AttributeSet attrs,
50             ListView listView) {
51         init(context, attrs, listView);
52     }
53
54     protected void init(Context context, AttributeSet attrs, ListView listView) {
55         this.listView = listView;
56         TypedArray typeArray = context.obtainStyledAttributes(attrs,
57                 R.styleable.ParallaxScroll);
58         this.parallaxFactor = typeArray.getFloat(
59                 R.styleable.ParallaxScroll_parallax_factor,
60                 DEFAULT_PARALLAX_FACTOR);
61         this.alphaFactor = typeArray.getFloat(
62                 R.styleable.ParallaxScroll_alpha_factor, DEFAULT_ALPHA_FACTOR);
63         this.isCircular = typeArray.getBoolean(
64                 R.styleable.ParallaxScroll_circular_parallax,
65                 DEFAULT_IS_CIRCULAR);
66         typeArray.recycle();
67     }
68
69     protected void setOnScrollListener(OnScrollListener l) {
70         this.listener = l;
71     }
72
73     protected void addParallaxedHeaderView(View v) {
74         addParallaxedView(v);
75     }
76
77     protected void addParallaxedHeaderView(View v, Object data,
78             boolean isSelectable) {
79         addParallaxedView(v);
80     }
81
82     protected void addParallaxedView(View v) {
83         this.parallaxedView = new ListViewParallaxedItem(v);
84     }
85
86     protected void parallaxScroll() {
87         if (isCircular)
88             circularParallax();
89         else
90             headerParallax();
91     }
92
93     private void circularParallax() {
94         if (listView.getChildCount() > 0) {
95             int top = -listView.getChildAt(0).getTop();
96             if (top >= 0) {
97                 fillParallaxedViews();
98                 setFilters(top);
99             }
100         }
101     }
102
103     private void headerParallax() {
104         if (parallaxedView != null) {
105             if (listView.getChildCount() > 0) {
106                 int top = -listView.getChildAt(0).getTop();
107                 if (top >= 0) {
108                     setFilters(top);
109                 }
110             }
111         }
112     }
113
114     private void setFilters(int top) {
115         parallaxedView.setOffset((float) top / parallaxFactor);
116         if (alphaFactor != DEFAULT_ALPHA_FACTOR) {
117             float alpha = (top <= 0) ? 1 : (100 / ((float) top * alphaFactor));
118             parallaxedView.setAlpha(alpha);
119         }
120         parallaxedView.animateNow();
121     }
122
123     private void fillParallaxedViews() {
124         if (parallaxedView == null
125                 || parallaxedView.is(listView.getChildAt(0)) == false) {
126             if (parallaxedView != null) {
127                 resetFilters();
128                 parallaxedView.setView(listView.getChildAt(0));
129             } else {
130                 parallaxedView = new ListViewParallaxedItem(
131                         listView.getChildAt(0));
132             }
133         }
134     }
135
136     private void resetFilters() {
137         parallaxedView.setOffset(0);
138         if (alphaFactor != DEFAULT_ALPHA_FACTOR)
139             parallaxedView.setAlpha(1F);
140         parallaxedView.animateNow();
141     }
142
143     @Override
144     public void onScroll(AbsListView view, int firstVisibleItem,
145             int visibleItemCount, int totalItemCount) {
146         parallaxScroll();
147         if (this.listener != null)
148             this.listener.onScroll(view, firstVisibleItem, visibleItemCount,
149                     totalItemCount);
150     }
151
152     @Override
153     public void onScrollStateChanged(AbsListView view, int scrollState) {
154         if (this.listener != null)
155             this.listener.onScrollStateChanged(view, scrollState);
156     }
157
158     protected class ListViewParallaxedItem extends ParallaxedView {
159
160         public ListViewParallaxedItem(View view) {
161             super(view);
162         }
163
164         @Override
165         protected void translatePreICS(View view, float offset) {
166             addAnimation(new TranslateAnimation(0, 0, offset, offset));
167         }
168     }
169 }