Android Open Source - android-cassowary-layout Parallax Scrolling Activity






From Project

Back to project page android-cassowary-layout.

License

The source code is released under:

Apache License

If you think the Android project android-cassowary-layout listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package no.agens.cassowarylayoutdemo;
//from   ww w.jav  a 2s  .  c  o m
import android.app.Activity;
import android.graphics.Point;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.ViewTreeObserver;
import android.widget.ScrollView;

import no.agens.cassowarylayout.CassowaryLayout;
import no.agens.cassowarylayout.Node;


public class ParallaxScrollingActivity extends Activity {

    private static final String LOG_TAG = "ParallaxScrolling";

    private ScrollView scrollView;
    private CassowaryLayout cassowaryLayout;

    private static final String SCROLL_POSITION = "scrollPosition";
    private static final String SCREEN_WIDTH = "screenWidth";
    private static final String SCREEN_HEIGHT = "screenHeight";
    private static final String SCROLL_Y = "scrollY";

    private int screenHeight;
    private int screenWidth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_parallax_scrolling);
        setScreenHeightAndWidth();
        scrollView = (ScrollView)findViewById(R.id.scroll_view);
        cassowaryLayout = (CassowaryLayout)findViewById(R.id.cassowary_layout);

        cassowaryLayout.addSetupCallback(new CassowaryLayout.CassowaryLayoutSetupCallback() {
            @Override
            public void onCassowaryLayoutSetupComplete(CassowaryLayout layout) {
                final Node containerNode = cassowaryLayout.getCassowaryModel().getContainerNode();

                scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {

                    @Override
                    public void onScrollChanged() {

                        int scrollY = scrollView.getScrollY();
                        containerNode.setVariableToValue(SCROLL_POSITION, getScrollPosition(scrollY));
                        containerNode.setVariableToValue(SCROLL_Y, scrollY);
                        cassowaryLayout.getCassowaryModel().solve();
                        cassowaryLayout.setChildPositionsFromCassowaryModel();
                    }
                });
                containerNode.setVariableToValue(SCROLL_POSITION, 0);
                containerNode.setVariableToValue(SCROLL_Y, 0);
                containerNode.setVariableToValue(SCREEN_HEIGHT, screenHeight);
                containerNode.setVariableToValue(SCREEN_WIDTH, screenWidth);
            }
        });

    }

    private void setScreenHeightAndWidth() {
        Display display = getWindowManager().getDefaultDisplay();
        Point point = new Point();
        display.getSize(point);
        screenHeight = point.y;
        screenWidth = point.x;
    }

    private double getScrollPosition(int scrollY) {
        double scrollPosition =  (double)scrollY / (double)(cassowaryLayout.getHeight() - screenHeight);
        Log.d(LOG_TAG, "scroll position " + scrollPosition);
        return scrollPosition;
    }

}




Java Source Code List

no.agens.cassowarylayout.ApplicationTest.java
no.agens.cassowarylayout.CassowaryLayout.java
no.agens.cassowarylayout.CassowaryModel.java
no.agens.cassowarylayout.ChildNode.java
no.agens.cassowarylayout.ConstraintParser.java
no.agens.cassowarylayout.ContainerNode.java
no.agens.cassowarylayout.DefaultViewIdResolver.java
no.agens.cassowarylayout.Node.java
no.agens.cassowarylayout.ViewIdResolver.java
no.agens.cassowarylayout.util.CassowaryUtil.java
no.agens.cassowarylayout.util.DimensionParserTest.java
no.agens.cassowarylayout.util.DimensionParser.java
no.agens.cassowarylayout.util.ExpressionTokenizerTest.java
no.agens.cassowarylayout.util.InfixToPostFixTest.java
no.agens.cassowarylayout.util.MeasureSpecUtils.java
no.agens.cassowarylayout.util.TimerUtil.java
no.agens.cassowarylayoutdemo.ApplicationTest.java
no.agens.cassowarylayoutdemo.CassowaryLayoutInLinearLayoutActivity.java
no.agens.cassowarylayoutdemo.CassowaryLayoutMatchParentActivity.java
no.agens.cassowarylayoutdemo.CassowaryLayoutWrapContentActivity.java
no.agens.cassowarylayoutdemo.CassowaryLayoutsInCassowaryLayoutActivity.java
no.agens.cassowarylayoutdemo.CassowaryLayoutsInListViewActivity.java
no.agens.cassowarylayoutdemo.CenterInParentActivity.java
no.agens.cassowarylayoutdemo.CenterViewRelativeToSiblingActivity.java
no.agens.cassowarylayoutdemo.ChessBoardActivity.java
no.agens.cassowarylayoutdemo.DynamicWidthActivity.java
no.agens.cassowarylayoutdemo.ExampleMenuActivity.java
no.agens.cassowarylayoutdemo.FixedAspectRatioDemoActivity.java
no.agens.cassowarylayoutdemo.IntrinsicHeightTextInWrapContentContainerActivity.java
no.agens.cassowarylayoutdemo.LayoutParamsSwitcherActivity.java
no.agens.cassowarylayoutdemo.LinearLayoutInCassowaryLayoutActivity.java
no.agens.cassowarylayoutdemo.ParallaxScrollingActivity.java
no.agens.cassowarylayoutdemo.PreSetupTestActivity.java
no.agens.cassowarylayoutdemo.ProgrammaticDemoActivity.java
no.agens.cassowarylayoutdemo.XmlLayoutDemoActivity.java
no.agens.cassowarylayoutdemo.util.EditRatioDialog.java