Skip to content

Commit

Permalink
Merge pull request #4 from Rezwan-dev/master
Browse files Browse the repository at this point in the history
Support for API 16.
  • Loading branch information
GadgetCheck committed Feb 1, 2016
2 parents e3f91c0 + c67d06f commit 7152d2a
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.tinderview.cardstatck.animation;

import android.animation.TypeEvaluator;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

/*
Expand All @@ -14,12 +15,31 @@ public class RelativeLayoutParamsEvaluator implements TypeEvaluator<LayoutParams
public LayoutParams evaluate(float fraction, LayoutParams start,
LayoutParams end) {

LayoutParams result = new LayoutParams(start);
LayoutParams result = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
result = new LayoutParams(start);
}else{
result = LayoutParamsALT(start);
}
result.leftMargin += ((end.leftMargin - start.leftMargin) * fraction);
result.rightMargin += ((end.rightMargin - start.rightMargin) * fraction);
result.topMargin += ((end.topMargin - start.topMargin) * fraction);
result.bottomMargin += ((end.bottomMargin - start.bottomMargin) * fraction);
return result;
}
public LayoutParams LayoutParamsALT(LayoutParams source) {
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(source.width, source.height);
layoutParams.width = source.width;
layoutParams.height = source.height;

layoutParams.leftMargin = source.leftMargin;
layoutParams.topMargin = source.topMargin;
layoutParams.rightMargin = source.rightMargin;
layoutParams.bottomMargin = source.bottomMargin;
layoutParams.alignWithParent = source.alignWithParent;

System.arraycopy(source.getRules(), 0, layoutParams.getRules(), 0, 22);
return layoutParams;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ValueAnimator;
import android.os.Build;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

Expand All @@ -33,9 +35,9 @@ public class CardAnimator {
private static final int REMOTE_DISTANCE = 1000;
public ArrayList<View> mCardCollection;
private float mRotation; //dislike rotation value
private HashMap<View, RelativeLayout.LayoutParams> mLayoutsMap;//used to store the list of card's view
private RelativeLayout.LayoutParams[] mRemoteLayouts = new RelativeLayout.LayoutParams[4];//creating the four list of stack
private RelativeLayout.LayoutParams baseLayout;
private HashMap<View, LayoutParams> mLayoutsMap;//used to store the list of card's view
private LayoutParams[] mRemoteLayouts = new LayoutParams[4];//creating the four list of stack
private LayoutParams baseLayout;
private int mStackMargin = 21;//set the margin of cardstack


Expand All @@ -53,7 +55,7 @@ private void setup() {

for (View v : mCardCollection) {
//setup basic card layout
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) v.getLayoutParams();
LayoutParams params = (LayoutParams) v.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params.width = LayoutParams.MATCH_PARENT;
params.height = LayoutParams.MATCH_PARENT;
Expand All @@ -62,21 +64,45 @@ private void setup() {

}

baseLayout = (RelativeLayout.LayoutParams) mCardCollection.get(0).getLayoutParams();
baseLayout = new RelativeLayout.LayoutParams(baseLayout);
baseLayout = (LayoutParams) mCardCollection.get(0).getLayoutParams();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
baseLayout = new LayoutParams(baseLayout);
}else{
baseLayout = LayoutParamsALT(baseLayout);
}



initLayout();//calling this method to arrange all the card based on StackView

for (View v : mCardCollection) {
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) v.getLayoutParams();
RelativeLayout.LayoutParams paramsCopy = new RelativeLayout.LayoutParams(params);
LayoutParams params = (LayoutParams) v.getLayoutParams();
LayoutParams paramsCopy = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
paramsCopy = new LayoutParams(params);
}else {
paramsCopy = LayoutParamsALT(params);
}
mLayoutsMap.put(v, paramsCopy);
}

setupRemotes();

}

public LayoutParams LayoutParamsALT(LayoutParams source) {
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(source.width, source.height);
layoutParams.width = source.width;
layoutParams.height = source.height;

layoutParams.leftMargin = source.leftMargin;
layoutParams.topMargin = source.topMargin;
layoutParams.rightMargin = source.rightMargin;
layoutParams.bottomMargin = source.bottomMargin;
layoutParams.alignWithParent = source.alignWithParent;

System.arraycopy(source.getRules(), 0, layoutParams.getRules(), 0, 22);
return layoutParams;
}
/* finding the index of view and creating layoutParams using baseLayout object and those height and width setting to view
* using method of v.setLayoutParams
* and setting card scale size to give curve shape to card
Expand All @@ -89,7 +115,12 @@ public void initLayout() {
if (index != 0) {
index -= 1;
}
LayoutParams params = new LayoutParams(baseLayout);
LayoutParams params = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
params = new LayoutParams(baseLayout);
}else{
params = LayoutParamsALT(baseLayout);
}
v.setLayoutParams(params);

//calling scale method of CardUtils class to set the margin and than put layout into v
Expand Down Expand Up @@ -165,8 +196,13 @@ public void discard(int direction, final AnimatorListener al) {


final View topView = getTopView();
RelativeLayout.LayoutParams topParams = (RelativeLayout.LayoutParams) topView.getLayoutParams();
RelativeLayout.LayoutParams layout = new RelativeLayout.LayoutParams(topParams);
LayoutParams topParams = (LayoutParams) topView.getLayoutParams();
LayoutParams layout = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
layout = new LayoutParams(topParams);
}else{
layout = LayoutParamsALT(topParams);
}
ValueAnimator discardAnim = ValueAnimator.ofObject(new RelativeLayoutParamsEvaluator(), layout, mRemoteLayouts[direction]);

discardAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
Expand All @@ -184,8 +220,13 @@ public void onAnimationUpdate(ValueAnimator value) {

if (v == topView) continue;
final View nv = mCardCollection.get(i + 1);
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
RelativeLayout.LayoutParams endLayout = new RelativeLayout.LayoutParams(layoutParams);
LayoutParams layoutParams = (LayoutParams) v.getLayoutParams();
LayoutParams endLayout = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
endLayout = new LayoutParams(layoutParams);
}else{
endLayout = LayoutParamsALT(layoutParams);
}
ValueAnimator layoutAnim = ValueAnimator.ofObject(new RelativeLayoutParamsEvaluator(), endLayout, mLayoutsMap.get(nv));
layoutAnim.setDuration(100);
layoutAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
Expand All @@ -206,10 +247,15 @@ public void onAnimationEnd(Animator animation) {
if (al != null) {
al.onAnimationEnd(animation);
}
mLayoutsMap = new HashMap<View, RelativeLayout.LayoutParams>();
mLayoutsMap = new HashMap<View, LayoutParams>();
for (View v : mCardCollection) {
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) v.getLayoutParams();
RelativeLayout.LayoutParams paramsCopy = new RelativeLayout.LayoutParams(params);
LayoutParams params = (LayoutParams) v.getLayoutParams();
LayoutParams paramsCopy = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
paramsCopy = new LayoutParams(params);
}else{
paramsCopy = LayoutParamsALT(params);
}
mLayoutsMap.put(v, paramsCopy);
}

Expand Down Expand Up @@ -243,8 +289,13 @@ public void onAnimationUpdate(ValueAnimator v) {
rotationAnim.start();//start the animation

for (final View v : mCardCollection) {
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
RelativeLayout.LayoutParams endLayout = new RelativeLayout.LayoutParams(layoutParams);
LayoutParams layoutParams = (LayoutParams) v.getLayoutParams();
LayoutParams endLayout = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
endLayout = new LayoutParams(layoutParams);
}else {
endLayout = LayoutParamsALT(layoutParams);
}
ValueAnimator layoutAnim = ValueAnimator.ofObject(new RelativeLayoutParamsEvaluator(), endLayout, mLayoutsMap.get(v));
layoutAnim.setDuration(250);
layoutAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
Expand All @@ -267,8 +318,8 @@ public void drag(MotionEvent e1, MotionEvent e2, float distanceX,

float rotation_coefficient = 20f;//

RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) topView.getLayoutParams();
RelativeLayout.LayoutParams topViewLayouts = mLayoutsMap.get(topView);
LayoutParams layoutParams = (LayoutParams) topView.getLayoutParams();
LayoutParams topViewLayouts = mLayoutsMap.get(topView);
int x_diff = (int) ((e2.getRawX() - e1.getRawX()));
int y_diff = (int) ((e2.getRawY() - e1.getRawY()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ public void setListener(CardEventListener cel) {

private void setupAnimation() {
final View cardView = viewCollection.get(viewCollection.size() - 1);
// Log.e("REZWAN", ""+viewCollection.size());
mCardAnimator = new CardAnimator(viewCollection); //creating an object of cardAnimator
mCardAnimator.initLayout(); //initialize the cardAnimator using object

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.tinderview.cardstatck.cardstack;

import android.os.Build;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
Expand All @@ -20,7 +21,7 @@ public class CardUtils {

//it remove some pixels from the each sides of given view and then set that layout to view using setLayoutParams method.
public static void scale(View v, int pixel) {
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) v.getLayoutParams();
LayoutParams params = (LayoutParams) v.getLayoutParams();
params.leftMargin -= pixel;
params.rightMargin -= pixel;
params.topMargin -= pixel;
Expand All @@ -31,9 +32,9 @@ public static void scale(View v, int pixel) {

//setting the layout margin dynamically to make it as stack view and returning the layoutParams object.
public static LayoutParams getMoveParams(View v, int upDown, int leftRight) {
RelativeLayout.LayoutParams original = (RelativeLayout.LayoutParams) v.getLayoutParams();
LayoutParams original = (LayoutParams) v.getLayoutParams();
//RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(original);
RelativeLayout.LayoutParams params = cloneParams(original);
LayoutParams params = cloneParams(original);
params.leftMargin += leftRight;
params.rightMargin -= leftRight;
Log.d("moving...", params.leftMargin + "");
Expand All @@ -45,7 +46,7 @@ public static LayoutParams getMoveParams(View v, int upDown, int leftRight) {

//used to move the card based on parameter, from this method again it's calling the getMoveParams(v,upDown,leftRight) methods
public static void move(View v, int upDown, int leftRight) {
RelativeLayout.LayoutParams params = getMoveParams(v, upDown, leftRight);
LayoutParams params = getMoveParams(v, upDown, leftRight);
v.setLayoutParams(params);
}

Expand Down Expand Up @@ -80,8 +81,8 @@ public static LayoutParams moveFrom(View v, LayoutParams params, int leftRight,
}

//a copy method for RelativeLayout.LayoutParams for backward compatibility
public static RelativeLayout.LayoutParams cloneParams(RelativeLayout.LayoutParams params) {
RelativeLayout.LayoutParams copy = new RelativeLayout.LayoutParams(params.width, params.height);
public static LayoutParams cloneParams(LayoutParams params) {
LayoutParams copy = new LayoutParams(params.width, params.height);
copy.leftMargin = params.leftMargin;
copy.topMargin = params.topMargin;
copy.rightMargin = params.rightMargin;
Expand All @@ -90,8 +91,11 @@ public static RelativeLayout.LayoutParams cloneParams(RelativeLayout.LayoutParam
for (int i = 0; i < rules.length; i++) {
copy.addRule(i, rules[i]);
}
copy.setMarginStart(params.getMarginStart());
copy.setMarginEnd(params.getMarginEnd());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
copy.setMarginStart(params.getMarginStart());
copy.setMarginEnd(params.getMarginEnd());
}


return copy;
}
Expand Down

0 comments on commit 7152d2a

Please sign in to comment.