4 Copyright (c) 2014 Nir Hartmann
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:
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
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
24 package com.samsung.srpol.parallax;
26 import com.samsung.srpol.R;
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;
37 public class ParallaxListViewHelper implements OnScrollListener {
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;
49 protected ParallaxListViewHelper(Context context, AttributeSet attrs,
51 init(context, attrs, listView);
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,
69 protected void setOnScrollListener(OnScrollListener l) {
73 protected void addParallaxedHeaderView(View v) {
77 protected void addParallaxedHeaderView(View v, Object data,
78 boolean isSelectable) {
82 protected void addParallaxedView(View v) {
83 this.parallaxedView = new ListViewParallaxedItem(v);
86 protected void parallaxScroll() {
93 private void circularParallax() {
94 if (listView.getChildCount() > 0) {
95 int top = -listView.getChildAt(0).getTop();
97 fillParallaxedViews();
103 private void headerParallax() {
104 if (parallaxedView != null) {
105 if (listView.getChildCount() > 0) {
106 int top = -listView.getChildAt(0).getTop();
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);
120 parallaxedView.animateNow();
123 private void fillParallaxedViews() {
124 if (parallaxedView == null
125 || parallaxedView.is(listView.getChildAt(0)) == false) {
126 if (parallaxedView != null) {
128 parallaxedView.setView(listView.getChildAt(0));
130 parallaxedView = new ListViewParallaxedItem(
131 listView.getChildAt(0));
136 private void resetFilters() {
137 parallaxedView.setOffset(0);
138 if (alphaFactor != DEFAULT_ALPHA_FACTOR)
139 parallaxedView.setAlpha(1F);
140 parallaxedView.animateNow();
144 public void onScroll(AbsListView view, int firstVisibleItem,
145 int visibleItemCount, int totalItemCount) {
147 if (this.listener != null)
148 this.listener.onScroll(view, firstVisibleItem, visibleItemCount,
153 public void onScrollStateChanged(AbsListView view, int scrollState) {
154 if (this.listener != null)
155 this.listener.onScrollStateChanged(view, scrollState);
158 protected class ListViewParallaxedItem extends ParallaxedView {
160 public ListViewParallaxedItem(View view) {
165 protected void translatePreICS(View view, float offset) {
166 addAnimation(new TranslateAnimation(0, 0, offset, offset));