Skip to content

Commit

Permalink
fixed issue #19
Browse files Browse the repository at this point in the history
  • Loading branch information
ybq authored and ybq committed Sep 20, 2016
1 parent dc40a82 commit 256ae67
Show file tree
Hide file tree
Showing 13 changed files with 157 additions and 65 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -24,7 +24,7 @@

``` gradle
dependencies {
compile 'com.github.ybq:Android-SpinKit:1.0.5'
compile 'com.github.ybq:Android-SpinKit:1.1.0'
}
```

Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Expand Up @@ -5,8 +5,8 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0-alpha3'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3' // Add this line
classpath 'com.android.tools.build:gradle:2.1.3'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1' // Add this line

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
@@ -1,6 +1,6 @@
#Fri Jun 17 17:17:22 CST 2016
#Sat Sep 17 16:46:17 CST 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
8 changes: 3 additions & 5 deletions library/build.gradle
Expand Up @@ -3,12 +3,10 @@ apply plugin: 'com.github.dcendents.android-maven'
group = 'com.github.ybq'
version = '1.0'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
compileSdkVersion 24
buildToolsVersion "24.0.2"
defaultConfig {
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
targetSdkVersion 24
}
}
Expand Up @@ -3,6 +3,7 @@
import android.animation.Keyframe;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.util.Log;
import android.util.Property;
import android.view.animation.Animation;
import android.view.animation.Interpolator;
Expand All @@ -11,110 +12,123 @@
import com.github.ybq.android.spinkit.animation.interpolator.KeyFrameInterpolator;
import com.github.ybq.android.spinkit.sprite.Sprite;

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

/**
* Created by ybq.
*/
public class SpriteAnimatorBuilder {

private static final String TAG = "SpriteAnimatorBuilder";
private Sprite sprite;
private List<PropertyValuesHolder> propertyValuesHolders = new ArrayList<>();
private Interpolator interpolator;
private int repeatCount = Animation.INFINITE;
private long duration = 2000;
private int startFrame = 0;
private Map<String, FrameData> fds = new HashMap<>();


class FrameData<T> {
public FrameData(float[] fractions, Property property, T[] values) {
this.fractions = fractions;
this.property = property;
this.values = values;
}

float[] fractions;
Property property;
T[] values;
}

class IntFrameData extends FrameData<Integer> {

public IntFrameData(float[] fractions, Property property, Integer[] values) {
super(fractions, property, values);
}
}

class FloatFrameData extends FrameData<Float> {

public FloatFrameData(float[] fractions, Property property, Float[] values) {
super(fractions, property, values);
}
}

public SpriteAnimatorBuilder(Sprite sprite) {
this.sprite = sprite;
}

public SpriteAnimatorBuilder scale(float fractions[], float... scale) {
public SpriteAnimatorBuilder scale(float fractions[], Float... scale) {
holder(fractions, Sprite.SCALE, scale);
return this;
}

public SpriteAnimatorBuilder alpha(float fractions[], int... alpha) {
public SpriteAnimatorBuilder alpha(float fractions[], Integer... alpha) {
holder(fractions, Sprite.ALPHA, alpha);
return this;
}

@SuppressWarnings("unused")
public SpriteAnimatorBuilder scaleX(float fractions[], float... scaleX) {
public SpriteAnimatorBuilder scaleX(float fractions[], Float... scaleX) {
holder(fractions, Sprite.SCALE, scaleX);
return this;
}

public SpriteAnimatorBuilder scaleY(float fractions[], float... scaleY) {
public SpriteAnimatorBuilder scaleY(float fractions[], Float... scaleY) {
holder(fractions, Sprite.SCALE_Y, scaleY);
return this;
}

public SpriteAnimatorBuilder rotateX(float fractions[], int... rotateX) {
public SpriteAnimatorBuilder rotateX(float fractions[], Integer... rotateX) {
holder(fractions, Sprite.ROTATE_X, rotateX);
return this;
}

public SpriteAnimatorBuilder rotateY(float fractions[], int... rotateY) {
public SpriteAnimatorBuilder rotateY(float fractions[], Integer... rotateY) {
holder(fractions, Sprite.ROTATE_Y, rotateY);
return this;
}

@SuppressWarnings("unused")
public SpriteAnimatorBuilder translateX(float fractions[], int... translateX) {
public SpriteAnimatorBuilder translateX(float fractions[], Integer... translateX) {
holder(fractions, Sprite.TRANSLATE_X, translateX);
return this;
}


@SuppressWarnings("unused")
public SpriteAnimatorBuilder translateY(float fractions[], int... translateY) {
public SpriteAnimatorBuilder translateY(float fractions[], Integer... translateY) {
holder(fractions, Sprite.TRANSLATE_Y, translateY);
return this;
}


public SpriteAnimatorBuilder rotate(float fractions[], int... rotate) {
public SpriteAnimatorBuilder rotate(float fractions[], Integer... rotate) {
holder(fractions, Sprite.ROTATE, rotate);
return this;
}

public SpriteAnimatorBuilder translateXPercentage(float fractions[], float... translateXPercentage) {
public SpriteAnimatorBuilder translateXPercentage(float fractions[], Float... translateXPercentage) {
holder(fractions, Sprite.TRANSLATE_X_PERCENTAGE, translateXPercentage);
return this;
}

public SpriteAnimatorBuilder translateYPercentage(float[] fractions, float... translateYPercentage) {
public SpriteAnimatorBuilder translateYPercentage(float[] fractions, Float... translateYPercentage) {
holder(fractions, Sprite.TRANSLATE_Y_PERCENTAGE, translateYPercentage);
return this;
}

private PropertyValuesHolder holder(float[] fractions, Property property, float[] values) {
private void holder(float[] fractions, Property property, Float[] values) {
ensurePair(fractions.length, values.length);
Keyframe[] keyframes = new Keyframe[fractions.length];
for (int i = 0; i < values.length; i++) {
keyframes[i] = Keyframe.ofFloat(fractions[i], values[i]);
}
PropertyValuesHolder valuesHolder = PropertyValuesHolder.
ofKeyframe(property
, keyframes
);
propertyValuesHolders.add(valuesHolder);
return valuesHolder;
fds.put(property.getName(), new FloatFrameData(fractions, property, values));
}

private PropertyValuesHolder holder(float[] fractions, Property property, int[] values) {

private void holder(float[] fractions, Property property, Integer[] values) {
ensurePair(fractions.length, values.length);
Keyframe[] keyframes = new Keyframe[fractions.length];
for (int i = 0; i < values.length; i++) {
keyframes[i] = Keyframe.ofInt(fractions[i], values[i]);
}
PropertyValuesHolder valuesHolder = PropertyValuesHolder.
ofKeyframe(property
, keyframes
);
propertyValuesHolders.add(valuesHolder);
return valuesHolder;
fds.put(property.getName(), new IntFrameData(fractions, property, values));
}

private void ensurePair(int fractionsLength, int valuesLength) {
Expand Down Expand Up @@ -153,11 +167,45 @@ public SpriteAnimatorBuilder repeatCount(int repeatCount) {
return this;
}

public SpriteAnimatorBuilder startFrame(int startFrame) {
if (startFrame < 0) {
Log.w(TAG, "startFrame should always be non-negative");
startFrame = 0;
}
this.startFrame = startFrame;
return this;
}

public ObjectAnimator build() {
PropertyValuesHolder[] holders = new PropertyValuesHolder[propertyValuesHolders.size()];

PropertyValuesHolder[] holders = new PropertyValuesHolder[fds.size()];
int i = 0;
for (Map.Entry<String, FrameData> fd : fds.entrySet()) {
FrameData data = fd.getValue();
Keyframe[] keyframes = new Keyframe[data.fractions.length];
float[] fractions = data.fractions;
float startF = fractions[startFrame];
for (int j = startFrame; j < (startFrame + data.values.length); j++) {
int key = j - startFrame;
int vk = j % data.values.length;
float fraction = fractions[vk] - startF;
if (fraction < 0) {
fraction = fractions[fractions.length - 1] + fraction;
}
if (data instanceof IntFrameData) {
keyframes[key] = Keyframe.ofInt(fraction, (Integer) data.values[vk]);
} else if (data instanceof FloatFrameData) {
keyframes[key] = Keyframe.ofFloat(fraction, (Float) data.values[vk]);
} else {
keyframes[key] = Keyframe.ofObject(fraction, data.values[vk]);
}
}
holders[i] = PropertyValuesHolder.ofKeyframe(data.property, keyframes);
i++;
}

ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(sprite,
propertyValuesHolders.toArray(holders));
holders);
animator.setDuration(duration);
animator.setRepeatCount(repeatCount);
animator.setInterpolator(interpolator);
Expand Down
Expand Up @@ -2,6 +2,7 @@

import android.animation.ValueAnimator;
import android.graphics.Rect;
import android.os.Build;
import android.view.animation.LinearInterpolator;

import com.github.ybq.android.spinkit.animation.SpriteAnimatorBuilder;
Expand All @@ -25,7 +26,11 @@ public Sprite[] onCreateChild() {
@Override
public void onChildCreated(Sprite... sprites) {
super.onChildCreated(sprites);
sprites[1].setAnimationDelay(-1000);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
sprites[1].setAnimationDelay(1000);
} else {
sprites[1].setAnimationDelay(-1000);
}
}

@Override
Expand Down
@@ -1,6 +1,7 @@
package com.github.ybq.android.spinkit.style;

import android.animation.ValueAnimator;
import android.os.Build;

import com.github.ybq.android.spinkit.animation.SpriteAnimatorBuilder;
import com.github.ybq.android.spinkit.sprite.CircleSprite;
Expand All @@ -17,7 +18,11 @@ public Sprite[] onCreateChild() {
Dot[] dots = new Dot[12];
for (int i = 0; i < dots.length; i++) {
dots[i] = new Dot();
dots[i].setAnimationDelay(1200 / 12 * i + -1200);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
dots[i].setAnimationDelay(1200 / 12 * i);
} else {
dots[i].setAnimationDelay(1200 / 12 * i + -1200);
}
}
return dots;
}
Expand Down
@@ -1,6 +1,7 @@
package com.github.ybq.android.spinkit.style;

import android.animation.ValueAnimator;
import android.os.Build;

import com.github.ybq.android.spinkit.animation.SpriteAnimatorBuilder;
import com.github.ybq.android.spinkit.sprite.CircleSprite;
Expand All @@ -22,7 +23,11 @@ public Sprite[] onCreateChild() {
@Override
public void onChildCreated(Sprite... sprites) {
super.onChildCreated(sprites);
sprites[1].setAnimationDelay(-1000);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
sprites[1].setAnimationDelay(1000);
} else {
sprites[1].setAnimationDelay(-1000);
}
}

private class Bounce extends CircleSprite {
Expand Down
@@ -1,6 +1,7 @@
package com.github.ybq.android.spinkit.style;

import android.animation.ValueAnimator;
import android.os.Build;

import com.github.ybq.android.spinkit.animation.SpriteAnimatorBuilder;
import com.github.ybq.android.spinkit.sprite.CircleSprite;
Expand All @@ -17,7 +18,11 @@ public Sprite[] onCreateChild() {
Dot[] dots = new Dot[12];
for (int i = 0; i < dots.length; i++) {
dots[i] = new Dot();
dots[i].setAnimationDelay(100 * i + -1200);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
dots[i].setAnimationDelay(1200 / 12 * i);
} else {
dots[i].setAnimationDelay(1200 / 12 * i + -1200);
}
}
return dots;
}
Expand Down
Expand Up @@ -3,6 +3,7 @@
import android.animation.ValueAnimator;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Build;
import android.view.animation.LinearInterpolator;

import com.github.ybq.android.spinkit.animation.SpriteAnimatorBuilder;
Expand All @@ -24,7 +25,12 @@ public Sprite[] onCreateChild() {
= new Cube[4];
for (int i = 0; i < cubes.length; i++) {
cubes[i] = new Cube();
cubes[i].setAnimationDelay(300 * i - 1200);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
cubes[i].setAnimationDelay(300 * i);
} else {
cubes[i].setAnimationDelay(300 * i - 1200);
}
}
return cubes;
}
Expand Down

0 comments on commit 256ae67

Please sign in to comment.