Last active
June 9, 2016 00:29
-
-
Save TiagoGouvea/5931d4aca56195683a21 to your computer and use it in GitHub Desktop.
Like and Unlike alpha view effect - kikoso/Swipeable-Cards
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public boolean onTouchEvent(MotionEvent event) { | |
if (mTopCard == null) { | |
return false; | |
} | |
if (mGestureDetector.onTouchEvent(event)) { | |
return true; | |
} | |
Log.d("Touch Event", MotionEvent.actionToString(event.getActionMasked()) + " "); | |
final int pointerIndex; | |
final float x, y; | |
final float dx, dy; | |
View alphaView; | |
switch (event.getActionMasked()) { | |
case MotionEvent.ACTION_DOWN: | |
mTopCard.getHitRect(childRect); | |
pointerIndex = event.getActionIndex(); | |
x = event.getX(pointerIndex); | |
y = event.getY(pointerIndex); | |
if (!childRect.contains((int) x, (int) y)) { | |
return false; | |
} | |
mLastTouchX = x; | |
mLastTouchY = y; | |
mActivePointerId = event.getPointerId(pointerIndex); | |
float[] points = new float[]{x - mTopCard.getLeft(), y - mTopCard.getTop()}; | |
mTopCard.getMatrix().invert(mMatrix); | |
mMatrix.mapPoints(points); | |
mTopCard.setPivotX(points[0]); | |
mTopCard.setPivotY(points[1]); | |
break; | |
case MotionEvent.ACTION_MOVE: | |
pointerIndex = event.findPointerIndex(mActivePointerId); | |
x = event.getX(pointerIndex); | |
y = event.getY(pointerIndex); | |
dx = x - mLastTouchX; | |
dy = y - mLastTouchY; | |
if (Math.abs(dx) > mTouchSlop || Math.abs(dy) > mTouchSlop) { | |
mDragging = true; | |
} | |
if (!mDragging) { | |
return true; | |
} | |
mTopCard.setTranslationX(mTopCard.getTranslationX() + dx); | |
mTopCard.setTranslationY(mTopCard.getTranslationY() + dy); | |
mTopCard.setRotation(40 * mTopCard.getTranslationX() / (getWidth() / 2.f)); | |
// Alpha like or dislike view | |
float distance = Math.abs(mTopCard.getTranslationX()); | |
float alpha = (distance / 100); | |
if (mTopCard.getTranslationX() > 0) | |
alphaView = ((View) getChildAt(getChildCount() - 1).findViewById(R.id.tvLike)); | |
else | |
alphaView = ((View) getChildAt(getChildCount() - 1).findViewById(R.id.tvDislike)); | |
alphaView.setAlpha(alpha); | |
mLastTouchX = x; | |
mLastTouchY = y; | |
break; | |
case MotionEvent.ACTION_UP: | |
case MotionEvent.ACTION_CANCEL: | |
if (!mDragging) { | |
return true; | |
} | |
mDragging = false; | |
mActivePointerId = INVALID_POINTER_ID; | |
ValueAnimator animator = ObjectAnimator.ofPropertyValuesHolder(mTopCard, | |
PropertyValuesHolder.ofFloat("translationX", 0), | |
PropertyValuesHolder.ofFloat("translationY", 0), | |
PropertyValuesHolder.ofFloat("rotation", (float) Math.toDegrees(mRandom.nextGaussian() * DISORDERED_MAX_ROTATION_RADIANS)), | |
PropertyValuesHolder.ofFloat("pivotX", mTopCard.getWidth() / 2.f), | |
PropertyValuesHolder.ofFloat("pivotY", mTopCard.getHeight() / 2.f) | |
).setDuration(250); | |
animator.setInterpolator(new AccelerateInterpolator()); | |
animator.start(); | |
// Animate to alpha 0 | |
if (mTopCard.getTranslationX() > 0) | |
alphaView = ((View) getChildAt(getChildCount() - 1).findViewById(R.id.tvLike)); | |
else | |
alphaView = ((View) getChildAt(getChildCount() - 1).findViewById(R.id.tvDislike)); | |
ValueAnimator animatorAlpha = ObjectAnimator.ofPropertyValuesHolder(alphaView, | |
PropertyValuesHolder.ofFloat("alpha", 0) | |
).setDuration(250); | |
animatorAlpha.setInterpolator(new AccelerateInterpolator()); | |
animatorAlpha.start(); | |
break; | |
case MotionEvent.ACTION_POINTER_UP: | |
pointerIndex = event.getActionIndex(); | |
final int pointerId = event.getPointerId(pointerIndex); | |
if (pointerId == mActivePointerId) { | |
final int newPointerIndex = pointerIndex == 0 ? 1 : 0; | |
mLastTouchX = event.getX(newPointerIndex); | |
mLastTouchY = event.getY(newPointerIndex); | |
mActivePointerId = event.getPointerId(newPointerIndex); | |
} | |
break; | |
} | |
return true; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<TextView | |
android:id="@+id/tvLike" | |
android:layout_width="match_parent" | |
android:layout_height="150dp" | |
android:text="Gostei" | |
android:textSize="30dp" | |
android:layout_margin="15dp" | |
android:textColor="#ffffff" | |
android:gravity="left" | |
android:alpha="0"/> | |
<TextView | |
android:id="@+id/tvDislike" | |
android:layout_width="match_parent" | |
android:layout_height="150dp" | |
android:text="Não gostei" | |
android:textSize="30dp" | |
android:layout_margin="15dp" | |
android:textColor="#ffffff" | |
android:gravity="right" | |
android:alpha="0"/> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment