Skip to content

Commit

Permalink
onEnd animation
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaGordia committed May 26, 2018
1 parent 90e3d55 commit 91016ff
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 24 deletions.
3 changes: 3 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ allprojects {

build.gradle (Module)
```groovy
implementation 'com.github.NikitaGordia:Cosin:1.0.0'
implementation 'com.github.NikitaGordia:Cosin:1.1.0'
```

# Quick start guide
Expand All @@ -50,12 +50,13 @@ implementation 'com.github.NikitaGordia:Cosin:1.0.0'
| --------------------------------------|-----------------------------------------------------------------------------------------------------------|
| speed | Angle speed |
| isLoading | If 'true' shows symbols on rectangle |
| setEnd | Shows end animation (optionally you may pass onEnd(OnEnd))
| rectWidth | Width each of rectangle |
| period | Determine period of cosinusoidal function |
| colorAdapter | Define color changes by two parameters (position count, height percent) for each rectagle |
| offset | Moving bottom offset |
| directionRight | Determine movement side |
| textAdapter | Define text changes by position count for each rectagle |
| textAdapter | Define text changes by position count for each rectangle |

## There are 6 default ColorAdapter implementation in box :

Expand Down Expand Up @@ -84,8 +85,8 @@ public class ColorAdapterBR implements Cosin.ColorAdapter {
}

@Override
public int calcColor(int numOfRect, double percentOfHeght) {
return Color.argb(150, (int)(255 * (1d - percentOfHeght)), 0, (int)(255 * percentOfHeght));
public int calcColor(int numOfRect, double percentOfHeight) {
return Color.argb(150, (int)(255 * (1d - percentOfHeight)), 0, (int)(255 * percentOfHeight));
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
Expand Down Expand Up @@ -230,6 +231,13 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
bind.cosin.setDirectionRight(isChecked);
}
});

bind.stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bind.cosin.setEnd();
}
});
}

private int part(int x, Cosin.Limit<Integer> lim) {
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,12 @@

</android.support.v7.widget.CardView>

<Button
android:id="@+id/stop"
android:text="STOP"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>

</ScrollView>
Expand Down
38 changes: 30 additions & 8 deletions cosin/src/main/java/com/nikitagordia/cosin/Cosin.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;

import com.nikitagordia.cosin.colorAdapters.DefaultColorAdapterGB;
import com.nikitagordia.cosin.textAdapters.DefaultBinaryTextAdapter;


/**
* Created by nikitagordia on 04.03.18.
*/
Expand All @@ -22,21 +22,22 @@ public class Cosin extends View {

private ColorAdapter colorAdapter;
private TextAdapter textAdapter;
private OnEnd callback;

private int count, alphaText;
private double angle, part, intervalRad, heightMid;
private double angle, part, intervalRad, heightMid, endingPart;
private Paint paint, paintBack, paintText;
private Rect bounds;
private double[] cosBuff;
private char[] textBuff;
private float[] widthBuff;
private float[] peekBuff;
private long tm;

private int rectWidth = 60;
private double period = Math.PI;
private double speed = 0.005d;
private double offset = 1.5d;
private double endingSpeed = 0.0008d;
private boolean directionRight = false;

public Limit<Integer> limRectWidth = new Limit<Integer>(5, 100);
Expand All @@ -47,18 +48,20 @@ public class Cosin extends View {
public Limit<Double> limOffset = new Limit<Double>(0d, 10d);

private boolean isLoadingData;
private boolean isEnding;

public Cosin(Context context, AttributeSet attributesSet) {
super(context, attributesSet);
angle = 0;
endingPart = 1d;
isEnding = false;
paint = new Paint();
paintBack = new Paint();
paintText = new Paint();
paintText.setTextSize(rectWidth / 2);
paintText.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
paintBack.setColor(Color.argb(255, 255, 255, 255));
paint.setColor(Color.GREEN);
bounds = new Rect();
colorAdapter = new DefaultColorAdapterGB();
textAdapter = new DefaultBinaryTextAdapter();
}
Expand Down Expand Up @@ -105,15 +108,21 @@ protected void onDraw(Canvas canvas) {
canvas.drawColor(colorAdapter.getBackgroundColor());

if (!directionRight) angle += ((double)System.currentTimeMillis() - tm) * speed;
else angle -= ((double)System.currentTimeMillis() - tm) * speed;
else angle -= ((double)System.currentTimeMillis() - tm) * speed;
if (angle > DOUBLE_PI) angle -= DOUBLE_PI;
if (angle < -DOUBLE_PI) angle += DOUBLE_PI;

if (isEnding) {
endingPart -= ((double)System.currentTimeMillis() - tm) * endingSpeed;
if (endingPart < 0 && callback != null) callback.onEnd();
}

tm = System.currentTimeMillis();

for (int i = 0; i < count; i++) {
part = (Math.cos(cosBuff[i] + angle) + offset) / (offset + 1d);
paint.setColor(colorAdapter.calcColor(i, part));
canvas.drawRect(peekBuff[i], (float)(getHeight() * (1d - part)), peekBuff[i + 1], getHeight(), paint);
canvas.drawRect(peekBuff[i], (float)(getHeight() * (1d - part * endingPart)), peekBuff[i + 1], getHeight(), paint);
if (isLoadingData) {
alphaText = (int)(Math.max(0, 1000d * part - 750d));
paintText.setColor(Color.argb(alphaText, 255, 255, 255));
Expand All @@ -124,6 +133,15 @@ protected void onDraw(Canvas canvas) {
invalidate();
}

public void setEnd(OnEnd callback) {
isEnding = true;
this.callback = callback;
}

public void setEnd() {
isEnding = true;
}

public double getSpeed() {
return speed;
}
Expand Down Expand Up @@ -238,11 +256,15 @@ public String toString() {
}
}

public interface OnEnd {
void onEnd();
}

public interface ColorAdapter {

int getBackgroundColor();

int calcColor(int numOfRect, double percentOfHeght);
int calcColor(int numOfRect, double percentOfHeight);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public int getBackgroundColor() {
}

@Override
public int calcColor(int numOfRect, double percentOfHeght) {
return Color.argb(150, 0, (int)(255 * (1d - percentOfHeght)), (int)(255 * percentOfHeght));
public int calcColor(int numOfRect, double percentOfHeight) {
return Color.argb(150, 0, (int)(255 * (1d - percentOfHeight)), (int)(255 * percentOfHeight));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public int getBackgroundColor() {
}

@Override
public int calcColor(int numOfRect, double percentOfHeght) {
return Color.argb(150, (int)(255 * (1d - percentOfHeght)), 0, (int)(255 * percentOfHeght));
public int calcColor(int numOfRect, double percentOfHeight) {
return Color.argb(150, (int)(255 * (1d - percentOfHeight)), 0, (int)(255 * percentOfHeight));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public int getBackgroundColor() {
}

@Override
public int calcColor(int numOfRect, double percentOfHeght) {
return Color.argb(150, (int)(255 * (1d - percentOfHeght)), (int)(255 * percentOfHeght), 0);
public int calcColor(int numOfRect, double percentOfHeight) {
return Color.argb(150, (int)(255 * (1d - percentOfHeight)), (int)(255 * percentOfHeight), 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public int getBackgroundColor() {
}

@Override
public int calcColor(int numOfRect, double percentOfHeght) {
return Color.argb(150, (int)(255 * percentOfHeght), 0, (int)(255 * (1d - percentOfHeght)));
public int calcColor(int numOfRect, double percentOfHeight) {
return Color.argb(150, (int)(255 * percentOfHeight), 0, (int)(255 * (1d - percentOfHeight)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public int getBackgroundColor() {
}

@Override
public int calcColor(int numOfRect, double percentOfHeght) {
return Color.argb(150, (int)(255 * percentOfHeght), (int)(255 * (1d - percentOfHeght)), 0);
public int calcColor(int numOfRect, double percentOfHeight) {
return Color.argb(150, (int)(255 * percentOfHeight), (int)(255 * (1d - percentOfHeight)), 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public int getBackgroundColor() {
}

@Override
public int calcColor(int numOfRect, double percentOfHeght) {
return Color.argb(150, 0, (int)(255 * percentOfHeght), (int)(255 * (1d - percentOfHeght)));
public int calcColor(int numOfRect, double percentOfHeight) {
return Color.argb(150, 0, (int)(255 * percentOfHeight), (int)(255 * (1d - percentOfHeight)));
}
}

0 comments on commit 91016ff

Please sign in to comment.