Skip to content

Commit

Permalink
commit for lazy developers
Browse files Browse the repository at this point in the history
  • Loading branch information
flavienlaurent committed Jan 27, 2014
1 parent 5c732e4 commit 70ede0d
Show file tree
Hide file tree
Showing 11 changed files with 673 additions and 40 deletions.
125 changes: 125 additions & 0 deletions README.md
Expand Up @@ -21,6 +21,131 @@ Usage

Using the library is simple, just look at the source code of the provided sample [here][3]

### build.gradle

```
compile 'com.github.flavienlaurent.discrollview:library:0.0.2@aar'
```

### The main layout

You must use the DiscrollViewContent view.

```xml
<com.flavienlaurent.discrollview.lib.DiscrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:discrollve="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.flavienlaurent.discrollview.lib.DiscrollViewContent
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- here you put discrollvable views -->

</com.flavienlaurent.discrollview.lib.DiscrollViewContent>

</com.flavienlaurent.discrollview.lib.DiscrollView>
```

### Discrollvable views

You can apply some transformation on discroll:

- alpha
- scale
- translation (fromLeft, fromBottom, fromRight, fromTop)
fromLeft+fromRight and fromBottom+fromTop are forbidden couples.
- bgcolor

```xml
discrollve:discrollve_alpha="true"
discrollve:discrollve_translation="fromLeft|fromBottom"
discrollve:discrollve_scaleX="true"
discrollve:discrollve_scaleY="true"
discrollve:discrollve_fromBgColor="#88EE66"
discrollve:discrollve_toBgColor="#000000"
discrollve:discrollve_threshold="0.3"
```

The threshold attribute is used to trigger the discrollve at a specified ratio. For example, if threshold=0.3, the discrollve starts when the ratio >= 0.3.

### A simple example

```xml
<com.flavienlaurent.discrollview.lib.DiscrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:discrollve="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.flavienlaurent.discrollview.lib.DiscrollViewContent
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_width="match_parent"
android:layout_height="600dp"
android:background="@android:color/white"
android:textColor="@android:color/black"
android:padding="25dp"
android:textSize="72sp"
android:gravity="center"
android:fontFamily="serif"
android:text="Do you love cheese?" />

<View
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#007788"
discrollve:discrollve_alpha="true"
discrollve:discrollve_threshold="0.3" />

<ImageView
android:layout_width="200dp"
android:layout_height="120dp"
discrollve:discrollve_alpha="true"
discrollve:discrollve_translation="fromLeft|fromBottom"
android:src="@drawable/cheese1" />

<View
android:layout_width="match_parent"
android:layout_height="200dp"
discrollve:discrollve_fromColor="#88EE66"
discrollve:discrollve_toColor="#000000" />

<ImageView
android:layout_width="220dp"
android:layout_height="110dp"
android:layout_gravity="right"
android:src="@drawable/cheese2"
discrollve:discrollve_translation="fromRight" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:fontFamily="serif"
android:gravity="center"
android:text="When the cheese comes out everybody's happy pecorino red leicester"
android:textSize="18sp"
discrollve:discrollve_alpha="true"
discrollve:discrollve_translation="fromBottom" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_gravity="center"
android:src="@drawable/ilovecheese_heart"
discrollve:discrollve_scaleX="true"
discrollve:discrollve_scaleY="true" />

</com.flavienlaurent.discrollview.lib.DiscrollViewContent>
</com.flavienlaurent.discrollview.lib.DiscrollView>
```

License
-----------

Expand Down
32 changes: 14 additions & 18 deletions gradle.properties
@@ -1,18 +1,14 @@
# Project-wide Gradle settings.

# IDE (e.g. Android Studio) users:
# Settings specified in this file will override any Gradle settings
# configured through the IDE.

# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html

# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx10248m -XX:MaxPermSize=256m
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
VERSION_NAME=0.0.2
VERSION_CODE=2
GROUP=com.github.flavienlaurent.discrollview

POM_DESCRIPTION=Android library to import the discrollver pattern on Android
POM_URL=https://github.com/flavienlaurent/discrollview
POM_SCM_URL=https://github.com/flavienlaurent/discrollview
POM_SCM_CONNECTION=scm:git@github.com:flavienlaurent/discrollview.git
POM_SCM_DEV_CONNECTION=scm:git@github.com:flavienlaurent/discrollview.git
POM_LICENCE_NAME=Apache License, Version 2.0
POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0
POM_LICENCE_DIST=repo
POM_DEVELOPER_ID=flavienlaurent
POM_DEVELOPER_NAME=Flavien Laurent
3 changes: 1 addition & 2 deletions lib/build.gradle
Expand Up @@ -27,5 +27,4 @@ android {
}
}

dependencies {
}
apply from: '../maven_push.gradle'
3 changes: 3 additions & 0 deletions lib/gradle.properties
@@ -0,0 +1,3 @@
POM_NAME=Discrollview Library
POM_ARTIFACT_ID=library
POM_PACKAGING=aar
Expand Up @@ -13,11 +13,12 @@
* else, the discrollvables starts to be discrollved when its top reaches the bottom of the DiscrollView
*
* @see com.flavienlaurent.discrollview.lib.Discrollvable
* @see com.flavienlaurent.discrollview.lib.DiscrollViewContent
*
*/
public class DiscrollView extends ScrollView {

private ViewGroup mContent;
private DiscrollViewContent mContent;

public DiscrollView(Context context) {
super(context);
Expand All @@ -31,8 +32,11 @@ public DiscrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

public static float clamp(float value, float max, float min) {
return Math.max(Math.min(value, min), max);
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
setupFirstView();

}

@Override
Expand All @@ -42,22 +46,15 @@ protected void onFinishInflate() {
throw new IllegalStateException("Discrollview must host one child.");
}
View content = getChildAt(0);
if(!(content instanceof ViewGroup)) {
throw new IllegalStateException("Discrollview must host a ViewGroup.");
if(!(content instanceof DiscrollViewContent)) {
throw new IllegalStateException("Discrollview must host a DiscrollViewContent.");
}
mContent = (ViewGroup) content;
mContent = (DiscrollViewContent) content;
if(mContent.getChildCount() < 2) {
throw new IllegalStateException("Discrollview must have at least 2 children.");
}
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
setupFirstView();

}

private void setupFirstView() {
//first first take all the parent height
View first = mContent.getChildAt(0);
Expand All @@ -72,6 +69,10 @@ protected void onScrollChanged(int l, int t, int oldl, int oldt) {
onScrollChanged(t);
}

public static float clamp(float value, float max, float min) {
return Math.max(Math.min(value, min), max);
}

private int getAbsoluteBottom() {
View last = getChildAt(getChildCount() - 1);
if(last == null) {
Expand Down
@@ -0,0 +1,109 @@
package com.flavienlaurent.discrollview.lib;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

/**
*
*/
public class DiscrollViewContent extends LinearLayout {

public DiscrollViewContent(Context context) {
super(context);
setOrientation(VERTICAL);
}

public DiscrollViewContent(Context context, AttributeSet attrs) {
super(context, attrs);
setOrientation(VERTICAL);
}

public DiscrollViewContent(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setOrientation(VERTICAL);
}

@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
super.addView(asDiscrollvable(child, (LayoutParams) params), index, params);
}

private View asDiscrollvable(View child, LayoutParams lp) {
if(! isDiscrollvable(lp)) {
return child;
}
DiscrollvableView discrollvableChild = new DiscrollvableView(getContext());
discrollvableChild.setDiscrollveAlpha(lp.mDiscrollveAlpha);
discrollvableChild.setDiscrollveTranslation(lp.mDiscrollveTranslation);
discrollvableChild.setDiscrollveScaleX(lp.mDiscrollveScaleX);
discrollvableChild.setDiscrollveScaleY(lp.mDiscrollveScaleY);
discrollvableChild.setDiscrollveThreshold(lp.mDiscrollveThreshold);
discrollvableChild.setDiscrollveFromBgColor(lp.mDiscrollveFromBgColor);
discrollvableChild.setDiscrollveToBgColor(lp.mDiscrollveToBgColor);
discrollvableChild.addView(child);
return discrollvableChild;
}

private boolean isDiscrollvable(LayoutParams lp) {
return lp.mDiscrollveAlpha ||
lp.mDiscrollveTranslation != -1 ||
lp.mDiscrollveScaleX ||
lp.mDiscrollveScaleY ||
(lp.mDiscrollveFromBgColor != -1 && lp.mDiscrollveToBgColor != -1);
}

@Override
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
return p instanceof LayoutParams;
}

@Override
protected LinearLayout.LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
}

@Override
public LinearLayout.LayoutParams generateLayoutParams(AttributeSet attrs) {
return new LayoutParams(getContext(), attrs);
}

@Override
protected LinearLayout.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
return new LayoutParams(p.width, p.height);
}

public static class LayoutParams extends LinearLayout.LayoutParams {

private int mDiscrollveFromBgColor;
private int mDiscrollveToBgColor;
private float mDiscrollveThreshold;
public boolean mDiscrollveAlpha;
public boolean mDiscrollveScaleX;
public boolean mDiscrollveScaleY;
private int mDiscrollveTranslation;

public LayoutParams(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DiscrollView_LayoutParams);
try {
mDiscrollveAlpha = a.getBoolean(R.styleable.DiscrollView_LayoutParams_discrollve_alpha, false);
mDiscrollveScaleX = a.getBoolean(R.styleable.DiscrollView_LayoutParams_discrollve_scaleX, false);
mDiscrollveScaleY = a.getBoolean(R.styleable.DiscrollView_LayoutParams_discrollve_scaleY, false);
mDiscrollveTranslation = a.getInt(R.styleable.DiscrollView_LayoutParams_discrollve_translation, -1);
mDiscrollveThreshold = a.getFloat(R.styleable.DiscrollView_LayoutParams_discrollve_threshold, 0.0f);
mDiscrollveFromBgColor = a.getColor(R.styleable.DiscrollView_LayoutParams_discrollve_fromBgColor, -1);
mDiscrollveToBgColor = a.getColor(R.styleable.DiscrollView_LayoutParams_discrollve_toBgColor, -1);
} finally {
a.recycle();
}
}

public LayoutParams(int w, int h) {
super(w, h);
}
}
}

0 comments on commit 70ede0d

Please sign in to comment.