## Gradle Dependency
``` gradle
dependencies {
implementation 'com.github.ybq:Android-SpinKit:1.4.0'
}
```
## Usage
- Xml
```xml
DoubleBounce |
Wave |
WanderingCubes |
Pulse |
ChasingDots |
ThreeBounce |
Circle |
CubeGrid |
FadingCircle |
FoldingCube |
RotatingCircle |
## Acknowledgements
- [SpinKit](https://github.com/tobiasahlin/SpinKit).
================================================
FILE: README_zh-CN.md
================================================
# Android-SpinKit
> Android 加载动画([SpinKit](https://tobiasahlin.com/spinkit)的Android版本实现)
[Demo Apk](https://raw.githubusercontent.com/ybq/AndroidSpinKit/master/art/spinkit.apk)
## 效果
## Gradle 依赖
``` gradle
dependencies {
implementation 'com.github.ybq:Android-SpinKit:1.4.0'
}
```
## 使用
- Xml
```xml
DoubleBounce |
Wave |
WanderingCubes |
Pulse |
ChasingDots |
ThreeBounce |
Circle |
CubeGrid |
FadingCircle |
FoldingCube |
RotatingCircle |
##感谢
- [SpinKit](https://github.com/tobiasahlin/SpinKit).
================================================
FILE: build.gradle
================================================
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
classpath 'com.novoda:bintray-release:0.8.1'
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
================================================
FILE: gradle/wrapper/gradle-wrapper.properties
================================================
#Tue Nov 14 15:17:22 WET 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
================================================
FILE: gradle.properties
================================================
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx10248m -XX:MaxPermSize=256m
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
android.enableJetifier=true
android.useAndroidX=true
================================================
FILE: gradlew
================================================
#!/usr/bin/env bash
##############################################################################
##
## Gradle start up script for UN*X
##
##############################################################################
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS=""
APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"
warn ( ) {
echo "$*"
}
die ( ) {
echo
echo "$*"
echo
exit 1
}
# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
case "`uname`" in
CYGWIN* )
cygwin=true
;;
Darwin* )
darwin=true
;;
MINGW* )
msys=true
;;
esac
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
PRG="$0"
# Need this for relative symlinks.
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG=`dirname "$PRG"`"/$link"
fi
done
SAVED="`pwd`"
cd "`dirname \"$PRG\"`/" >/dev/null
APP_HOME="`pwd -P`"
cd "$SAVED" >/dev/null
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD="$JAVA_HOME/jre/sh/java"
else
JAVACMD="$JAVA_HOME/bin/java"
fi
if [ ! -x "$JAVACMD" ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
else
JAVACMD="java"
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
# Increase the maximum file descriptors if we can.
if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
MAX_FD_LIMIT=`ulimit -H -n`
if [ $? -eq 0 ] ; then
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
MAX_FD="$MAX_FD_LIMIT"
fi
ulimit -n $MAX_FD
if [ $? -ne 0 ] ; then
warn "Could not set maximum file descriptor limit: $MAX_FD"
fi
else
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
fi
fi
# For Darwin, add options to specify how the application appears in the dock
if $darwin; then
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
fi
# For Cygwin, switch paths to Windows format before running java
if $cygwin ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
JAVACMD=`cygpath --unix "$JAVACMD"`
# We build the pattern for arguments to be converted via cygpath
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
SEP=""
for dir in $ROOTDIRSRAW ; do
ROOTDIRS="$ROOTDIRS$SEP$dir"
SEP="|"
done
OURCYGPATTERN="(^($ROOTDIRS))"
# Add a user-defined pattern to the cygpath arguments
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
fi
# Now convert the arguments - kludge to limit ourselves to /bin/sh
i=0
for arg in "$@" ; do
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
else
eval `echo args$i`="\"$arg\""
fi
i=$((i+1))
done
case $i in
(0) set -- ;;
(1) set -- "$args0" ;;
(2) set -- "$args0" "$args1" ;;
(3) set -- "$args0" "$args1" "$args2" ;;
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
esac
fi
# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
function splitJvmOpts() {
JVM_OPTS=("$@")
}
eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
================================================
FILE: gradlew.bat
================================================
@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS=
set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto init
echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto init
echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:init
@rem Get command-line arguments, handling Windowz variants
if not "%OS%" == "Windows_NT" goto win9xME_args
if "%@eval[2+2]" == "4" goto 4NT_args
:win9xME_args
@rem Slurp the command line arguments.
set CMD_LINE_ARGS=
set _SKIP=2
:win9xME_args_slurp
if "x%~1" == "x" goto execute
set CMD_LINE_ARGS=%*
goto execute
:4NT_args
@rem Get arguments from the 4NT Shell from JP Software
set CMD_LINE_ARGS=%$
:execute
@rem Setup the command line
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd
:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega
================================================
FILE: library/.gitignore
================================================
/build
================================================
FILE: library/build.gradle
================================================
apply plugin: 'com.android.library'
apply plugin: 'com.novoda.bintray-release'
android {
compileSdkVersion 28
buildToolsVersion '28.0.3'
defaultConfig {
minSdkVersion 14
targetSdkVersion 28
}
lintOptions {
abortOnError false
}
}
publish {
userOrg = 'ybq'
groupId = 'com.github.ybq'
artifactId = 'Android-SpinKit'
publishVersion = '1.4.0'
desc = 'Android loading animations'
website = 'https://github.com/ybq/Android-SpinKit'
licences = ['MIT']
}
================================================
FILE: library/gradle.properties
================================================
================================================
FILE: library/proguard-rules.pro
================================================
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/ybq/Library/Android/sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# Add any project specific keep options here:
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
================================================
FILE: library/src/main/AndroidManifest.xml
================================================
float. This type-specific subclass enables performance benefit by allowing
* calls to a {@link #set(Object, Float) set()} function that takes the primitive
* float type and avoids autoboxing and other overhead associated with the
* Float class.
*
* @param float.
*/
public abstract void setValue(T object, float value);
@Override
final public void set(T object, Float value) {
setValue(object, value);
}
}
================================================
FILE: library/src/main/java/com/github/ybq/android/spinkit/animation/IntProperty.java
================================================
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.ybq.android.spinkit.animation;
import android.util.Property;
/**
* An implementation of {@link android.util.Property} to be used specifically with fields of type
* int. This type-specific subclass enables performance benefit by allowing
* calls to a {@link #set(Object, Integer) set()} function that takes the primitive
* int type and avoids autoboxing and other overhead associated with the
* Integer class.
*
* @param int.
*/
public abstract void setValue(T object, int value);
@Override
final public void set(T object, Integer value) {
setValue(object, value);
}
}
================================================
FILE: library/src/main/java/com/github/ybq/android/spinkit/animation/SpriteAnimatorBuilder.java
================================================
package com.github.ybq.android.spinkit.animation;
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;
import com.github.ybq.android.spinkit.animation.interpolator.KeyFrameInterpolator;
import com.github.ybq.android.spinkit.sprite.Sprite;
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 Interpolator interpolator;
private int repeatCount = Animation.INFINITE;
private long duration = 2000;
private int startFrame = 0;
private Map
* The {@link Path} must not have gaps in the x direction and must not
* loop back on itself such that there can be two points sharing the same x coordinate.
*
* @param path the {@link Path} to use to make the line representing the {@link Interpolator}
* @return the {@link Interpolator} representing the {@link Path}
*/
@SuppressWarnings("unused")
public static Interpolator create(Path path) {
if (Build.VERSION.SDK_INT >= 21) {
return PathInterpolatorCompatApi21.create(path);
}
return PathInterpolatorCompatBase.create(path);
}
/**
* Create an {@link Interpolator} for a quadratic Bezier curve. The end points
* {@code (0, 0)} and {@code (1, 1)} are assumed.
*
* @param controlX the x coordinate of the quadratic Bezier control point
* @param controlY the y coordinate of the quadratic Bezier control point
* @return the {@link Interpolator} representing the quadratic Bezier curve
*/
@SuppressWarnings("unused")
public static Interpolator create(float controlX, float controlY) {
if (Build.VERSION.SDK_INT >= 21) {
return PathInterpolatorCompatApi21.create(controlX, controlY);
}
return PathInterpolatorCompatBase.create(controlX, controlY);
}
/**
* Create an {@link Interpolator} for a cubic Bezier curve. The end points
* {@code (0, 0)} and {@code (1, 1)} are assumed.
*
* @param controlX1 the x coordinate of the first control point of the cubic Bezier
* @param controlY1 the y coordinate of the first control point of the cubic Bezier
* @param controlX2 the x coordinate of the second control point of the cubic Bezier
* @param controlY2 the y coordinate of the second control point of the cubic Bezier
* @return the {@link Interpolator} representing the cubic Bezier curve
*/
public static Interpolator create(float controlX1, float controlY1,
float controlX2, float controlY2) {
if (Build.VERSION.SDK_INT >= 21) {
return PathInterpolatorCompatApi21.create(controlX1, controlY1, controlX2, controlY2);
}
return PathInterpolatorCompatBase.create(controlX1, controlY1, controlX2, controlY2);
}
}
================================================
FILE: library/src/main/java/com/github/ybq/android/spinkit/animation/interpolator/PathInterpolatorCompatApi21.java
================================================
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.ybq.android.spinkit.animation.interpolator;
import android.annotation.TargetApi;
import android.graphics.Path;
import android.os.Build;
import android.view.animation.Interpolator;
import android.view.animation.PathInterpolator;
/**
* API 21+ implementation for path interpolator compatibility.
*/
class PathInterpolatorCompatApi21 {
private PathInterpolatorCompatApi21() {
// prevent instantiation
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static Interpolator create(Path path) {
return new PathInterpolator(path);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static Interpolator create(float controlX, float controlY) {
return new PathInterpolator(controlX, controlY);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static Interpolator create(float controlX1, float controlY1,
float controlX2, float controlY2) {
return new PathInterpolator(controlX1, controlY1, controlX2, controlY2);
}
}
================================================
FILE: library/src/main/java/com/github/ybq/android/spinkit/animation/interpolator/PathInterpolatorCompatBase.java
================================================
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.ybq.android.spinkit.animation.interpolator;
import android.graphics.Path;
import android.view.animation.Interpolator;
/**
* Base implementation for path interpolator compatibility.
*/
class PathInterpolatorCompatBase {
private PathInterpolatorCompatBase() {
// prevent instantiation
}
public static Interpolator create(Path path) {
return new PathInterpolatorDonut(path);
}
public static Interpolator create(float controlX, float controlY) {
return new PathInterpolatorDonut(controlX, controlY);
}
public static Interpolator create(float controlX1, float controlY1,
float controlX2, float controlY2) {
return new PathInterpolatorDonut(controlX1, controlY1, controlX2, controlY2);
}
}
================================================
FILE: library/src/main/java/com/github/ybq/android/spinkit/animation/interpolator/PathInterpolatorDonut.java
================================================
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.ybq.android.spinkit.animation.interpolator;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.view.animation.Interpolator;
/**
* A path interpolator implementation compatible with API 4+.
*/
class PathInterpolatorDonut implements Interpolator {
/**
* Governs the accuracy of the approximation of the {@link Path}.
*/
private static final float PRECISION = 0.002f;
private final float[] mX;
private final float[] mY;
public PathInterpolatorDonut(Path path) {
final PathMeasure pathMeasure = new PathMeasure(path, false /* forceClosed */);
final float pathLength = pathMeasure.getLength();
final int numPoints = (int) (pathLength / PRECISION) + 1;
mX = new float[numPoints];
mY = new float[numPoints];
final float[] position = new float[2];
for (int i = 0; i < numPoints; ++i) {
final float distance = (i * pathLength) / (numPoints - 1);
pathMeasure.getPosTan(distance, position, null /* tangent */);
mX[i] = position[0];
mY[i] = position[1];
}
}
public PathInterpolatorDonut(float controlX, float controlY) {
this(createQuad(controlX, controlY));
}
public PathInterpolatorDonut(float controlX1, float controlY1,
float controlX2, float controlY2) {
this(createCubic(controlX1, controlY1, controlX2, controlY2));
}
@Override
public float getInterpolation(float t) {
if (t <= 0.0f) {
return 0.0f;
} else if (t >= 1.0f) {
return 1.0f;
}
// Do a binary search for the correct x to interpolate between.
int startIndex = 0;
int endIndex = mX.length - 1;
while (endIndex - startIndex > 1) {
int midIndex = (startIndex + endIndex) / 2;
if (t < mX[midIndex]) {
endIndex = midIndex;
} else {
startIndex = midIndex;
}
}
final float xRange = mX[endIndex] - mX[startIndex];
if (xRange == 0) {
return mY[startIndex];
}
final float tInRange = t - mX[startIndex];
final float fraction = tInRange / xRange;
final float startY = mY[startIndex];
final float endY = mY[endIndex];
return startY + (fraction * (endY - startY));
}
private static Path createQuad(float controlX, float controlY) {
final Path path = new Path();
path.moveTo(0.0f, 0.0f);
path.quadTo(controlX, controlY, 1.0f, 1.0f);
return path;
}
private static Path createCubic(float controlX1, float controlY1,
float controlX2, float controlY2) {
final Path path = new Path();
path.moveTo(0.0f, 0.0f);
path.cubicTo(controlX1, controlY1, controlX2, controlY2, 1.0f, 1.0f);
return path;
}
}
================================================
FILE: library/src/main/java/com/github/ybq/android/spinkit/sprite/CircleLayoutContainer.java
================================================
package com.github.ybq.android.spinkit.sprite;
import android.graphics.Canvas;
import android.graphics.Rect;
/**
* Created by ybq.
*/
public abstract class CircleLayoutContainer extends SpriteContainer {
@Override
public void drawChild(Canvas canvas) {
for (int i = 0; i < getChildCount(); i++) {
Sprite sprite = getChildAt(i);
int count = canvas.save();
canvas.rotate(i * 360 / getChildCount(),
getBounds().centerX(),
getBounds().centerY());
sprite.draw(canvas);
canvas.restoreToCount(count);
}
}
@Override
protected void onBoundsChange(Rect bounds) {
super.onBoundsChange(bounds);
bounds = clipSquare(bounds);
int radius = (int) (bounds.width() * Math.PI / 3.6f / getChildCount());
int left = bounds.centerX() - radius;
int right = bounds.centerX() + radius;
for (int i = 0; i < getChildCount(); i++) {
Sprite sprite = getChildAt(i);
sprite.setDrawBounds(left, bounds.top, right, bounds.top + radius * 2);
}
}
}
================================================
FILE: library/src/main/java/com/github/ybq/android/spinkit/sprite/CircleSprite.java
================================================
package com.github.ybq.android.spinkit.sprite;
import android.animation.ValueAnimator;
import android.graphics.Canvas;
import android.graphics.Paint;
/**
* Created by ybq.
*/
public class CircleSprite extends ShapeSprite {
@Override
public ValueAnimator onCreateAnimation() {
return null;
}
@Override
public void drawShape(Canvas canvas, Paint paint) {
if (getDrawBounds() != null) {
int radius = Math.min(getDrawBounds().width(), getDrawBounds().height()) / 2;
canvas.drawCircle(getDrawBounds().centerX(),
getDrawBounds().centerY(),
radius, paint);
}
}
}
================================================
FILE: library/src/main/java/com/github/ybq/android/spinkit/sprite/RectSprite.java
================================================
package com.github.ybq.android.spinkit.sprite;
import android.animation.ValueAnimator;
import android.graphics.Canvas;
import android.graphics.Paint;
/**
* Created by ybq.
*/
public class RectSprite extends ShapeSprite {
@Override
public ValueAnimator onCreateAnimation() {
return null;
}
@Override
public void drawShape(Canvas canvas, Paint paint) {
if (getDrawBounds() != null) {
canvas.drawRect(getDrawBounds(), paint);
}
}
}
================================================
FILE: library/src/main/java/com/github/ybq/android/spinkit/sprite/RingSprite.java
================================================
package com.github.ybq.android.spinkit.sprite;
import android.animation.ValueAnimator;
import android.graphics.Canvas;
import android.graphics.Paint;
/**
* Created by ybq.
*/
public class RingSprite extends ShapeSprite {
@Override
public void drawShape(Canvas canvas, Paint paint) {
if (getDrawBounds() != null) {
paint.setStyle(Paint.Style.STROKE);
int radius = Math.min(getDrawBounds().width(), getDrawBounds().height()) / 2;
paint.setStrokeWidth(radius / 12);
canvas.drawCircle(getDrawBounds().centerX(),
getDrawBounds().centerY(),
radius, paint);
}
}
@Override
public ValueAnimator onCreateAnimation() {
return null;
}
}
================================================
FILE: library/src/main/java/com/github/ybq/android/spinkit/sprite/ShapeSprite.java
================================================
package com.github.ybq.android.spinkit.sprite;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
/**
* Created by ybq.
*/
public abstract class ShapeSprite extends Sprite {
private Paint mPaint;
private int mUseColor;
private int mBaseColor;
public ShapeSprite() {
setColor(Color.WHITE);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(mUseColor);
}
@Override
public void setColor(int color) {
mBaseColor = color;
updateUseColor();
}
@Override
public int getColor() {
return mBaseColor;
}
@SuppressWarnings("unused")
public int getUseColor() {
return mUseColor;
}
@Override
public void setAlpha(int alpha) {
super.setAlpha(alpha);
updateUseColor();
}
private void updateUseColor() {
int alpha = getAlpha();
alpha += alpha >> 7;
final int baseAlpha = mBaseColor >>> 24;
final int useAlpha = baseAlpha * alpha >> 8;
mUseColor = (mBaseColor << 8 >>> 8) | (useAlpha << 24);
}
@Override
public void setColorFilter(ColorFilter colorFilter) {
mPaint.setColorFilter(colorFilter);
}
@Override
protected final void drawSelf(Canvas canvas) {
mPaint.setColor(mUseColor);
drawShape(canvas, mPaint);
}
public abstract void drawShape(Canvas canvas, Paint paint);
}
================================================
FILE: library/src/main/java/com/github/ybq/android/spinkit/sprite/Sprite.java
================================================
package com.github.ybq.android.spinkit.sprite;
import android.animation.ValueAnimator;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.Drawable;
import android.util.Property;
import com.github.ybq.android.spinkit.animation.AnimationUtils;
import com.github.ybq.android.spinkit.animation.FloatProperty;
import com.github.ybq.android.spinkit.animation.IntProperty;
/**
* Created by ybq.
*/
public abstract class Sprite extends Drawable implements
ValueAnimator.AnimatorUpdateListener
, Animatable
, Drawable.Callback {
private float scale = 1;
private float scaleX = 1;
private float scaleY = 1;
private float pivotX;
private float pivotY;
private int animationDelay;
private int rotateX;
private int rotateY;
private int translateX;
private int translateY;
private int rotate;
private float translateXPercentage;
private float translateYPercentage;
private ValueAnimator animator;
private int alpha = 255;
private static final Rect ZERO_BOUNDS_RECT = new Rect();
protected Rect drawBounds = ZERO_BOUNDS_RECT;
private Camera mCamera;
private Matrix mMatrix;
public Sprite() {
mCamera = new Camera();
mMatrix = new Matrix();
}
public abstract int getColor();
public abstract void setColor(int color);
@Override
public void setAlpha(int alpha) {
this.alpha = alpha;
}
@Override
public int getAlpha() {
return alpha;
}
@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
public float getTranslateXPercentage() {
return translateXPercentage;
}
public void setTranslateXPercentage(float translateXPercentage) {
this.translateXPercentage = translateXPercentage;
}
public float getTranslateYPercentage() {
return translateYPercentage;
}
public void setTranslateYPercentage(float translateYPercentage) {
this.translateYPercentage = translateYPercentage;
}
public int getTranslateX() {
return translateX;
}
public void setTranslateX(int translateX) {
this.translateX = translateX;
}
public int getTranslateY() {
return translateY;
}
public void setTranslateY(int translateY) {
this.translateY = translateY;
}
public int getRotate() {
return rotate;
}
public void setRotate(int rotate) {
this.rotate = rotate;
}
public float getScale() {
return scale;
}
public void setScale(float scale) {
this.scale = scale;
setScaleX(scale);
setScaleY(scale);
}
public float getScaleX() {
return scaleX;
}
public void setScaleX(float scaleX) {
this.scaleX = scaleX;
}
public float getScaleY() {
return scaleY;
}
public void setScaleY(float scaleY) {
this.scaleY = scaleY;
}
public int getRotateX() {
return rotateX;
}
public void setRotateX(int rotateX) {
this.rotateX = rotateX;
}
public int getRotateY() {
return rotateY;
}
public void setRotateY(int rotateY) {
this.rotateY = rotateY;
}
public float getPivotX() {
return pivotX;
}
public void setPivotX(float pivotX) {
this.pivotX = pivotX;
}
public float getPivotY() {
return pivotY;
}
public void setPivotY(float pivotY) {
this.pivotY = pivotY;
}
@SuppressWarnings("unused")
public int getAnimationDelay() {
return animationDelay;
}
public Sprite setAnimationDelay(int animationDelay) {
this.animationDelay = animationDelay;
return this;
}
@Override
public void setColorFilter(ColorFilter colorFilter) {
}
public abstract ValueAnimator onCreateAnimation();
@Override
public void start() {
if (AnimationUtils.isStarted(animator)) {
return;
}
animator = obtainAnimation();
if (animator == null) {
return;
}
AnimationUtils.start(animator);
invalidateSelf();
}
public ValueAnimator obtainAnimation() {
if (animator == null) {
animator = onCreateAnimation();
}
if (animator != null) {
animator.addUpdateListener(this);
animator.setStartDelay(animationDelay);
}
return animator;
}
@Override
public void stop() {
if (AnimationUtils.isStarted(animator)) {
animator.removeAllUpdateListeners();
animator.end();
reset();
}
}
protected abstract void drawSelf(Canvas canvas);
public void reset() {
scale = 1;
rotateX = 0;
rotateY = 0;
translateX = 0;
translateY = 0;
rotate = 0;
translateXPercentage = 0f;
translateYPercentage = 0f;
}
@Override
public boolean isRunning() {
return AnimationUtils.isRunning(animator);
}
@Override
protected void onBoundsChange(Rect bounds) {
super.onBoundsChange(bounds);
setDrawBounds(bounds);
}
public void setDrawBounds(Rect drawBounds) {
setDrawBounds(drawBounds.left, drawBounds.top, drawBounds.right, drawBounds.bottom);
}
public void setDrawBounds(int left, int top, int right, int bottom) {
this.drawBounds = new Rect(left, top, right, bottom);
setPivotX(getDrawBounds().centerX());
setPivotY(getDrawBounds().centerY());
}
@Override
public void invalidateDrawable(Drawable who) {
invalidateSelf();
}
@Override
public void scheduleDrawable(Drawable who, Runnable what, long when) {
}
@Override
public void unscheduleDrawable(Drawable who, Runnable what) {
}
@Override
public void onAnimationUpdate(ValueAnimator animation) {
final Callback callback = getCallback();
if (callback != null) {
callback.invalidateDrawable(this);
}
}
public Rect getDrawBounds() {
return drawBounds;
}
@Override
public void draw(Canvas canvas) {
int tx = getTranslateX();
tx = tx == 0 ? (int) (getBounds().width() * getTranslateXPercentage()) : tx;
int ty = getTranslateY();
ty = ty == 0 ? (int) (getBounds().height() * getTranslateYPercentage()) : ty;
canvas.translate(tx, ty);
canvas.scale(getScaleX(), getScaleY(), getPivotX(), getPivotY());
canvas.rotate(getRotate(), getPivotX(), getPivotY());
if (getRotateX() != 0 || getRotateY() != 0) {
mCamera.save();
mCamera.rotateX(getRotateX());
mCamera.rotateY(getRotateY());
mCamera.getMatrix(mMatrix);
mMatrix.preTranslate(-getPivotX(), -getPivotY());
mMatrix.postTranslate(getPivotX(), getPivotY());
mCamera.restore();
canvas.concat(mMatrix);
}
drawSelf(canvas);
}
public Rect clipSquare(Rect rect) {
int w = rect.width();
int h = rect.height();
int min = Math.min(w, h);
int cx = rect.centerX();
int cy = rect.centerY();
int r = min / 2;
return new Rect(
cx - r,
cy - r,
cx + r,
cy + r
);
}
public static final PropertyArgbEvaluator that may be used in
* {@link android.animation.ValueAnimator#setEvaluator(TypeEvaluator)}. The same instance may
* be used in multiple Animators because it holds no state.
*
* @return An instance of ArgbEvalutor.
*/
public static ArgbEvaluator getInstance() {
return sInstance;
}
/**
* This function returns the calculated in-between value for a color
* given integers that represent the start and end values in the four
* bytes of the 32-bit int. Each channel is separately linearly interpolated
* and the resulting calculated values are recombined into the return value.
*
* @param fraction The fraction from the starting to the ending values
* @param startValue A 32-bit int value representing colors in the
* separate bytes of the parameter
* @param endValue A 32-bit int value representing colors in the
* separate bytes of the parameter
* @return A value that is calculated to be the linearly interpolated
* result, derived by separating the start and end values into separate
* color channels and interpolating each one separately, recombining the
* resulting values in the same way.
*/
public Object evaluate(float fraction, Object startValue, Object endValue) {
int startInt = (Integer) startValue;
int startA = (startInt >> 24) & 0xff;
int startR = (startInt >> 16) & 0xff;
int startG = (startInt >> 8) & 0xff;
int startB = startInt & 0xff;
int endInt = (Integer) endValue;
int endA = (endInt >> 24) & 0xff;
int endR = (endInt >> 16) & 0xff;
int endG = (endInt >> 8) & 0xff;
int endB = endInt & 0xff;
return (startA + (int) (fraction * (endA - startA))) << 24 |
(startR + (int) (fraction * (endR - startR))) << 16 |
(startG + (int) (fraction * (endG - startG))) << 8 |
(startB + (int) (fraction * (endB - startB)));
}
}
================================================
FILE: sample/src/main/java/com/github/ybq/android/loading/Colors.java
================================================
package com.github.ybq.android.loading;
/**
* Created by ybq.
*/
public interface Colors {
int[] colors = new int[]{
0XFFD55400,
0XFF2B3E51,
0XFF00BD9C,
0XFF227FBB,
0XFF7F8C8D,
0XFFFFCC5C,
0XFFD55400,
0XFF1AAF5D,
};
}
================================================
FILE: sample/src/main/java/com/github/ybq/android/loading/DetailActivity.java
================================================
package com.github.ybq.android.loading;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.github.ybq.android.spinkit.SpinKitView;
import com.github.ybq.android.spinkit.SpriteFactory;
import com.github.ybq.android.spinkit.Style;
import com.github.ybq.android.spinkit.sprite.Sprite;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
public class DetailActivity extends AppCompatActivity implements Colors {
public static void start(Context context, int position) {
Intent intent = new Intent(context, DetailActivity.class);
intent.putExtra("position", position);
context.startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
viewPager.setOffscreenPageLimit(0);
viewPager.setAdapter(new PagerAdapter() {
@Override
public int getCount() {
return Style.values().length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
@SuppressLint("InflateParams") View view = LayoutInflater.from(container.getContext()).inflate(R.layout.item_pager, null);
SpinKitView spinKitView = (SpinKitView) view.findViewById(R.id.spin_kit);
TextView name = (TextView) view.findViewById(R.id.name);
Style style = Style.values()[position];
name.setText(style.name().toLowerCase());
Sprite drawable = SpriteFactory.create(style);
spinKitView.setIndeterminateDrawable(drawable);
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
});
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
int color = (int) ArgbEvaluator.getInstance().evaluate(positionOffset,
colors[position % colors.length],
colors[(position + 1) % colors.length]);
getWindow().getDecorView().setBackgroundColor(color);
}
@Override
public void onPageSelected(int position) {
getWindow().getDecorView().setBackgroundColor(colors[position % colors.length]);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
viewPager.setCurrentItem(getIntent().getIntExtra("position", 0));
}
}
================================================
FILE: sample/src/main/java/com/github/ybq/android/loading/MainActivity.java
================================================
package com.github.ybq.android.loading;
import android.os.Bundle;
import com.google.android.material.tabs.TabLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
public class MainActivity extends AppCompatActivity {
TabLayout mTabLayout;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabLayout = (TabLayout) findViewById(R.id.tabs);
mViewPager = (ViewPager) findViewById(R.id.viewpager);
mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
String[] titles = new String[]{
"Style", "Widget"
};
@Override
public Fragment getItem(int position) {
if (position == 0) {
return StyleFragment.newInstance();
} else {
return WidgetFragment.newInstance();
}
}
@Override
public int getCount() {
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
});
mTabLayout.setupWithViewPager(mViewPager);
}
}
================================================
FILE: sample/src/main/java/com/github/ybq/android/loading/SquareFrameLayout.java
================================================
package com.github.ybq.android.loading;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.FrameLayout;
/**
* Created by ybq.
*/
public class SquareFrameLayout extends FrameLayout {
public SquareFrameLayout(Context context) {
super(context);
}
public SquareFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@SuppressWarnings("unused")
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public SquareFrameLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//noinspection SuspiciousNameCombination
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}
}
================================================
FILE: sample/src/main/java/com/github/ybq/android/loading/StyleFragment.java
================================================
package com.github.ybq.android.loading;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.github.ybq.android.spinkit.SpinKitView;
import com.github.ybq.android.spinkit.SpriteFactory;
import com.github.ybq.android.spinkit.Style;
import com.github.ybq.android.spinkit.sprite.Sprite;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
/**
* Created by ybq.
*/
public class StyleFragment extends Fragment implements Colors {
public static StyleFragment newInstance() {
return new StyleFragment();
}
@SuppressLint("InflateParams")
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_style, null);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.list);
GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 4);
layoutManager.setOrientation(RecyclerView.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(new RecyclerView.Adapter