Skip to content

Commit

Permalink
Updates and Apis
Browse files Browse the repository at this point in the history
  • Loading branch information
EudyContrerasRosario committed Feb 6, 2019
1 parent 93535eb commit 1b03186
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 39 deletions.
10 changes: 7 additions & 3 deletions app/src/main/java/com/eudycontreras/rippleeffect/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class MainActivity : AppCompatActivity() {
ripple.rippleRepeats = RippleView.INFINITE_REPEATS
ripple.rippleDuration = 2000
ripple.rippleStrokeWidth = 10f
ripple.offsetY = DimensionUtility.convertPixelsToDp(this,10f)
}

override fun onResume() {
Expand All @@ -55,16 +56,18 @@ class MainActivity : AppCompatActivity() {
Handler().postDelayed({
someElementContainer.animate()
.setInterpolator(interpolatorOut)
.translationZ(DimensionUtility.convertDpToPixel(this,DimensionUtility.convertPixelsToDp(this,8f)))
.translationZ(DimensionUtility.convertDpToPixel(this, DimensionUtility.convertPixelsToDp(this,8f)))
.scaleY(1f)
.scaleX(1f)
.setDuration(150L)
.setListener(object: Animator.AnimatorListener {
override fun onAnimationRepeat(p0: Animator?) { }

override fun onAnimationEnd(p0: Animator?) {
ripple.setTarget(someElementContainer, 2.45f, 0.43f)
ripple.startRippleAnimation(1000)
if(!ripple.isAnimationRunning){
ripple.setTarget(someElementContainer, 2.45f, 0.43f)
ripple.startRippleAnimation(1000)
}
}

override fun onAnimationCancel(p0: Animator?) {}
Expand Down Expand Up @@ -116,6 +119,7 @@ class MainActivity : AppCompatActivity() {
MotionEvent.ACTION_CANCEL -> {
someElementContainer.animate()
.setInterpolator(interpolatorOut)
.setListener(null)
.translationZ(DimensionUtility.convertDpToPixel(this,8f))
.scaleY(1f)
.scaleX(1f)
Expand Down
12 changes: 11 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,26 @@
android:id="@+id/root"
tools:context=".MainActivity">

<Space
android:id="@+id/spaceOne"
android:layout_width="wrap_content"
android:layout_height="40dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
/>

<FrameLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="1dp"
android:translationZ="12dp"
android:background="@drawable/shape_circle"
android:id="@+id/someElementContainer"
android:layout_marginTop="24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
app:layout_constraintTop_toBottomOf="@id/spaceOne">
<View
android:id="@+id/someElement"
android:background="@drawable/circle"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.eudycontreras.rippleeffectlib;

/**
* <b>Note:</b> Unlicensed private property of the author and creator
* unauthorized use of this class outside of the Ripple Effect project
* by the author may result on legal prosecution.
* <p>
* Created by <B>Eudy Contreras</B>
*
* @author Eudy Contreras
* @version 1.0
* @since 2018-03-31
*/
public class Property<T> {
private T value;

public Property(T value) {
this.value = value;
}

public T getValue() {
return value;
}

public void setValue(T value) {
this.value = value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public abstract class Particle {
protected boolean fade = true;
protected boolean shrink = true;
protected boolean checkBounds = false;
protected boolean alwaysAlive = false;

protected Bounds bounds;
protected Paint paint;
Expand Down Expand Up @@ -130,10 +131,14 @@ public float checkDistanceTo(Particle particle) {
}

public boolean isAlive() {
if(bounds != null) {
return (!checkBounds || bounds.inRange(centerX, centerY, (radius * 2))) && (lifeSpan > 0) && (radius > 0) && (opacity > 0);
}
return lifeSpan > 0;
if(alwaysAlive){
return true;
}else{
if(bounds != null) {
return (!checkBounds || bounds.inRange(centerX, centerY, (radius * 2))) && (lifeSpan > 0) && (radius > 0) && (opacity > 0);
}
return lifeSpan > 0;
}
}

public Paint getPaint() {
Expand Down Expand Up @@ -313,6 +318,14 @@ public void setCheckBounds(boolean checkBounds) {
this.checkBounds = checkBounds;
}

public boolean isAlwaysAlive() {
return alwaysAlive;
}

public void setAlwaysAlive(boolean alwaysAlive) {
this.alwaysAlive = alwaysAlive;
}

public void setSpacing(float spacing) {
this.spacing = spacing;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void update(float duration, float time) {

@Override
public boolean isAlive() {
return opacity > 0f && (radius > 0 || width > 0 || height > 0);
return (opacity > 0f && (radius > 0 || width > 0 || height > 0)) || alwaysAlive;
}

public void draw(Canvas canvas){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import com.eudycontreras.rippleeffectlib.Bounds;
import com.eudycontreras.rippleeffectlib.Property;
import com.eudycontreras.rippleeffectlib.R;
import com.eudycontreras.rippleeffectlib.particles.ParticleRipple;
import com.eudycontreras.rippleeffectlib.utilities.ColorUtility;
Expand Down Expand Up @@ -75,6 +80,9 @@ public class RippleView extends View {
private float centerX = Integer.MIN_VALUE;
private float centerY = Integer.MIN_VALUE;

private float offsetX = 0f;
private float offsetY = 0f;

private float rippleMinWidth;
private float rippleMinHeight;

Expand All @@ -95,11 +103,11 @@ public class RippleView extends View {
private long revealDuration = 300;
private long concealDuration = 300;

private boolean useColorInterpolation;
private boolean showBorderStroke;
private boolean animationRunning;
private boolean autoStartRipple;
private boolean cleanUpAfter;
private boolean useColorInterpolation = false;
private boolean showBorderStroke = false;
private boolean animationRunning = false;
private boolean autoStartRipple = false;
private boolean cleanUpAfter = false;

private ParticleRipple[] ripples;
private ArrayList<Animator> animators;
Expand All @@ -109,6 +117,7 @@ public class RippleView extends View {
private Runnable runLater;
private ViewDrawListener listener;

private Handler handler;
private AnimatorSet animatorSet;
private ViewGroup parent;
private Bounds bounds;
Expand Down Expand Up @@ -176,6 +185,8 @@ private void initialize() {

color = new ColorUtility.SoulColor();

handler = new Handler();

animatorSet = new AnimatorSet();
animators = new ArrayList<>();
}
Expand Down Expand Up @@ -267,9 +278,12 @@ private void initializeRipple() {
ripple.setColorStart(colorStart);
ripple.setColorEnd(colorEnd);
ripple.setType(rippleType);
ripple.setCenterX(centerX);
ripple.setCenterY(centerY);
ripple.setX(rippleX + offsetX);
ripple.setY(rippleY + offsetY);
ripple.setCenterX(centerX + offsetX);
ripple.setCenterY(centerY + offsetY);
ripple.setVisible(true);
ripple.setAlwaysAlive(true);
ripple.init();

ripples[i] = ripple;
Expand Down Expand Up @@ -297,10 +311,10 @@ private void initializeRipple() {
ripples[index].setMaxRadius(rippleMaxRadius);
ripples[index].setClipRadius(rippleClipRadius);
ripples[index].setStrokeWidth(rippleStrokeWidth);
ripples[index].setCenterX(centerX);
ripples[index].setCenterY(centerY);
ripples[index].setX(rippleX);
ripples[index].setY(rippleY);
ripples[index].setCenterX(centerX + offsetX);
ripples[index].setCenterY(centerY + offsetY);
ripples[index].setX(rippleX + offsetX);
ripples[index].setY(rippleY + offsetY);
ripples[index].setMinWidth(rippleMinWidth);
ripples[index].setMinHeight(rippleMinHeight);
ripples[index].setMaxWidth(rippleMaxWidth);
Expand Down Expand Up @@ -336,16 +350,15 @@ public void startRippleAnimation(int rippleDelay) {
}

public void startRippleAnimation(int rippleDelay, Interpolator interpolator) {
new Handler().postDelayed(()-> {
handler.postDelayed(()-> {
animatorSet.end();
animatorSet.cancel();
if(!animationRunning){
animatorSet = new AnimatorSet();
animatorSet.setInterpolator(interpolator);
animatorSet.playTogether(animators);
animatorSet.start();
animationRunning=true;
}
animatorSet = new AnimatorSet();
animatorSet.setInterpolator(interpolator);
animatorSet.playTogether(animators);
animatorSet.start();
animationRunning=true;

}, rippleDelay);
}

Expand Down Expand Up @@ -473,10 +486,10 @@ protected void onDraw(Canvas canvas) {
ripple.setMaxRadius(rippleMaxRadius);
ripple.setClipRadius(rippleClipRadius);
ripple.setStrokeWidth(rippleStrokeWidth);
ripple.setCenterX(centerX);
ripple.setCenterY(centerY);
ripple.setX(rippleX);
ripple.setY(rippleY);
ripple.setX(rippleX + offsetX);
ripple.setY(rippleY + offsetY);
ripple.setCenterX(centerX + offsetX);
ripple.setCenterY(centerY + offsetY);
ripple.setMaxWidth(rippleMaxWidth);
ripple.setMaxHeight(rippleMaxHeight);
ripple.setMinWidth(rippleMinWidth);
Expand Down Expand Up @@ -530,16 +543,32 @@ public void setTarget(View view, float widthRatio, float heightRatio, float radi
parent.removeView(this);
parent.addView(this);

setLayoutParams(new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.MATCH_PARENT));
if(getLayoutParams() instanceof ConstraintLayout.LayoutParams){
setLayoutParams(new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.MATCH_PARENT));
}else if (getLayoutParams() instanceof LinearLayout.LayoutParams){
setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
}else if (getLayoutParams() instanceof FrameLayout.LayoutParams){
setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
}else if (getLayoutParams() instanceof RelativeLayout.LayoutParams){
setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
}else{
setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
}

int[] locationView = new int[2];
int[] locationParent = new int[2];

view.getLocationOnScreen(locationView);
parent.getLocationOnScreen(locationParent);

int[] location = new int[2];
view.getLocationOnScreen(location);
locationView[0] = locationView[0] - getCalculatedOffsetX(parent);
locationView[1] = locationView[1] - getCalculatedOffsetY(parent);

centerX = (location[0] + view.getWidth() / 2f);
centerY = (location[1] + view.getHeight() / 2f) - DimensionUtility.convertDpToPixel(getContext(), 24) ;
centerX = (locationView[0] + view.getWidth() / 2f);
centerY = (locationView[1] + view.getHeight() / 2f) - DimensionUtility.convertDpToPixel(getContext(), 24) ;

rippleX = (location[0] + view.getWidth() / 2f);
rippleY = (location[1] + view.getHeight() / 2f) - DimensionUtility.convertDpToPixel(getContext(), 24) ;
rippleX = (locationView[0] + view.getWidth() / 2f);
rippleY = (locationView[1] + view.getHeight() / 2f) - DimensionUtility.convertDpToPixel(getContext(), 24) ;

rippleMinWidth = view.getWidth();
rippleMinHeight = view.getHeight();
Expand All @@ -555,6 +584,58 @@ public void setTarget(View view, float widthRatio, float heightRatio, float radi
setTranslationZ(view.getTranslationZ()-1f);
}

private int getCalculatedOffsetY(ViewGroup parent) {
Property<Integer> property = new Property<>(parent.getTop());

//getCalculatedOffsetY(parent.getParent(), property);

return property.getValue();
}

private int getCalculatedOffsetX(ViewGroup parent) {
Property<Integer> property = new Property<>(parent.getLeft());

//getCalculatedOffsetX(parent.getParent(), property);

return property.getValue();
}

private void getCalculatedOffsetY(ViewParent parent, Property<Integer> offset) {
if(parent instanceof ViewGroup){
ViewGroup group = (ViewGroup) parent;
offset.setValue(offset.getValue()+group.getTop());
if(group.getParent() != null){
getCalculatedOffsetY(group.getParent(), offset);
}
}
}

private void getCalculatedOffsetX(ViewParent parent, Property<Integer> offset) {
if(parent instanceof ViewGroup){
ViewGroup group = (ViewGroup) parent;
offset.setValue(offset.getValue()+group.getLeft());
if(group.getParent() != null){
getCalculatedOffsetX(group.getParent(), offset);
}
}
}

public void setOffsetX(float offsetX){
this.offsetX = offsetX;
}

public void setOffsetY(float offsetY){
this.offsetY = offsetY;
}

public float getOffsetX() {
return offsetX;
}

public float getOffsetY() {
return offsetY;
}

public void setOnEnd(Runnable onEnd) {
this.onEnd = onEnd;
}
Expand Down

0 comments on commit 1b03186

Please sign in to comment.