Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CardStackViewにCustom Animationを追加 #1

Merged
merged 1 commit into from
May 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package com.yuyakaido.android.cardstackview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.view.MotionEvent;
Expand All @@ -12,11 +18,6 @@
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CardAnimator {
private Context context;
private List<ViewGroup> containers;
Expand Down Expand Up @@ -60,12 +61,36 @@ public void initCards() {
LayoutParams params = CardUtil.cloneParams(baseParams);
v.setLayoutParams(params);

clearAlpha(v);
clearScale(v);
clearTranslation(v);

CardUtil.scale(v, -(size - index - 1) * 5);
CardUtil.move(v, index * 20, 0);
v.setRotation(0);
}
}

public void clearTranslation(ViewGroup viewGroup) {
if (viewGroup != null) {
viewGroup.setTranslationX(0);
viewGroup.setTranslationY(0);
}
}

public void clearScale(ViewGroup viewGroup) {
if (viewGroup != null) {
viewGroup.setScaleX(1.0f);
viewGroup.setScaleY(1.0f);
}
}

public void clearAlpha(ViewGroup viewGroup) {
if (viewGroup != null) {
viewGroup.setAlpha(1.0f);
}
}

public void initRemoteParams() {
int width = CardUtil.getDisplayWidth(context);
int height = CardUtil.getDisplayHeight(context);
Expand Down Expand Up @@ -220,6 +245,46 @@ public void onAnimationEnd(Animator animation) {
animatorSet.start();
}

public void discard(Direction direction, ObjectAnimator topAnimator, final AnimatorListener listener) {
AnimatorSet animatorSet = new AnimatorSet();
List<Animator> animators = new ArrayList<>();
animators.add(topAnimator);

for (int i = 0, size = containers.size(); i < size - 1; i++) {
final View currentView = containers.get(i);
View nextView = containers.get(i + 1);
LayoutParams beginParams = CardUtil.cloneParams((LayoutParams) currentView.getLayoutParams());
LayoutParams endParams = cardParams.get(nextView);
ValueAnimator animator = ValueAnimator.ofObject(
new LayoutParamsEvaluator(), beginParams, endParams);
animator.setDuration(250);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator value) {
currentView.setLayoutParams((LayoutParams) value.getAnimatedValue());
}
});
animators.add(animator);
}

animatorSet.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
reorderForDiscard();
if (listener != null) {
listener.onAnimationEnd(animation);
}
cardParams = new HashMap<>();
for (View v : containers) {
cardParams.put(v, CardUtil.cloneParams((LayoutParams) v.getLayoutParams()));
}
}
});

animatorSet.playTogether(animators);
animatorSet.start();
}

public void moveToOrigin() {
final View topView = getTopView();
ValueAnimator topAnimator = ValueAnimator.ofFloat(rotation, 0f);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.yuyakaido.android.cardstackview;

import java.util.ArrayList;
import java.util.List;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.database.DataSetObserver;
import android.util.AttributeSet;
Expand All @@ -12,9 +16,6 @@
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;

public class CardStackView extends RelativeLayout {
private int topIndex = 0;
private int visibleCount = 4;
Expand Down Expand Up @@ -199,6 +200,30 @@ public void onAnimationEnd(Animator arg0) {
});
}

public void discard(ObjectAnimator topAnimator) {
cardAnimator.discard(Direction.BottomRight, topAnimator, new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator arg0) {
lastDirection = Direction.BottomRight;

cardAnimator.initCards();

if (cardStackEventListener != null) {
cardStackEventListener.onDiscarded(topIndex, Direction.BottomRight);
}

topIndex++;

loadNextView();

containers.get(0).setOnTouchListener(null);
containers.get(containers.size() - 1)
.setOnTouchListener(onTouchListener);
}

});
}

public void reverse() {
if (lastDirection != null) {
topIndex--;
Expand All @@ -222,4 +247,8 @@ public int getTopIndex() {
return topIndex;
}

public ViewGroup getTopView() {
return cardAnimator.getTopView();
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.yuyakaido.android.cardstackview.sample;

import com.yuyakaido.android.cardstackview.CardStackView;

import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.yuyakaido.android.cardstackview.CardStackView;
import android.view.ViewGroup;

public class MainActivity extends AppCompatActivity {

Expand All @@ -20,13 +23,29 @@ protected void onCreate(Bundle savedInstanceState) {
final CardStackView cardStackView = (CardStackView) findViewById(R.id.activity_main_card_stack);
cardStackView.setAdapter(adapter);

View button = findViewById(R.id.activity_main_reverse_button);
button.setOnClickListener(new View.OnClickListener() {
View reverseButton = findViewById(R.id.activity_main_reverse_button);
reverseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cardStackView.reverse();
}
});

View customAnimationButton = findViewById(R.id.activity_main_custom_animation_button);
customAnimationButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ViewGroup target = cardStackView.getTopView();
PropertyValuesHolder holderY = PropertyValuesHolder.ofFloat("translationY", 0.f, 600.f);
PropertyValuesHolder holderAlpha = PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.8f);
PropertyValuesHolder holderScaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f, 0.3f);
PropertyValuesHolder holderScaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f, 0.3f);
ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(target, holderY, holderScaleY, holderScaleX, holderAlpha);

animator.setDuration(500);
cardStackView.discard(animator);
}
});
}

}
27 changes: 22 additions & 5 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,29 @@

</RelativeLayout>

<Button
android:id="@+id/activity_main_reverse_button"
android:layout_width="wrap_content"
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_gravity="center_horizontal"
android:text="Reverse"/>
android:gravity="center_horizontal">

<Button
android:id="@+id/activity_main_reverse_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reverse"/>

<android.support.v4.widget.Space
android:layout_width="10dp"
android:layout_height="wrap_content"/>

<Button
android:id="@+id/activity_main_custom_animation_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Custom Animation"/>

</LinearLayout>

</LinearLayout>