Skip to content

Commit

Permalink
Used a more material-like spinner animation
Browse files Browse the repository at this point in the history
  • Loading branch information
code-mc committed Jan 30, 2016
1 parent c5796a1 commit 39aa7fb
Show file tree
Hide file tree
Showing 12 changed files with 792 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .idea/gradle.xml

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

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ The default toasts are ugly and don't really provide much more than a short mess
#### Gradle
```groovy
dependencies {
compile 'net.steamcrafted:load-toast:1.0.9'
compile 'net.steamcrafted:load-toast:1.0.10'
}
```

Expand Down
15 changes: 5 additions & 10 deletions app/app.iml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_6" inherit-compiler-output="false">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/build/intermediates/classes/debug" />
<output-test url="file://$MODULE_DIR$/build/intermediates/classes/androidTest/debug" />
<exclude-output />
Expand Down Expand Up @@ -65,36 +65,31 @@
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/bundles" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/coverage-instrumented-classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dependency-cache" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dex" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dex-cache" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/appcompat-v7/22.2.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/support-v4/22.2.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jacoco" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/javaResources" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/libs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/lint" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/ndk" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/mockable-android-22.jar" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/pre-dexed" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/proguard" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/resources" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/tmp" />
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
<excludeFolder url="file://$MODULE_DIR$/build/reports" />
<excludeFolder url="file://$MODULE_DIR$/build/test-results" />
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
</content>
<orderEntry type="jdk" jdkName="Android API 22 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="library-2.4.0" level="project" />
<orderEntry type="library" exported="" name="support-v4-22.2.1" level="project" />
<orderEntry type="library" exported="" name="support-annotations-22.2.1" level="project" />
<orderEntry type="library" exported="" name="library-2.4.0" level="project" />
<orderEntry type="library" exported="" name="appcompat-v7-22.2.1" level="project" />
<orderEntry type="module" module-name="loadtoast" exported="" />
</component>
Expand Down
6 changes: 0 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ android {
abortOnError false
}


compileOptions {
sourceCompatibility JavaVersion.VERSION_1_6
targetCompatibility JavaVersion.VERSION_1_6
}

defaultConfig {
applicationId "net.steamcrafted.gesturetrackerlib"
minSdkVersion 8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import net.steamcrafted.loadtoast.LoadToast;
import net.steamcrafted.loadtoast.MaterialProgressDrawable;


public class MainActivity extends AppCompatActivity {
Expand All @@ -22,13 +24,13 @@ public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);

final String text = "";
final LoadToast lt = new LoadToast(this).setText(text).setTranslationY(100).show();
final LoadToast lt = new LoadToast(this).setProgressColor(Color.RED).setText(text).setTranslationY(100).show();
//lt.success();
final ViewGroup root = (ViewGroup) findViewById(android.R.id.content);

View v = new View(this);
v.setBackgroundColor(Color.RED);
root.addView(v, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 400));
//root.addView(v, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 400));
findViewById(R.id.show).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Expand All @@ -51,9 +53,12 @@ public void onClick(View view) {
@Override
public void onClick(View view) {
View v = new View(MainActivity.this);
v.setBackgroundColor(Color.rgb((int)(Math.random() * 255), (int)(Math.random() * 255), (int)(Math.random() * 255)));
root.addView(v, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 400));
v.setBackgroundColor(Color.rgb((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255)));
//root.addView(v, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 400));
}
});

ImageView progressView = ((ImageView) findViewById(R.id.progressdrawable));
MaterialProgressDrawable drawable = new MaterialProgressDrawable(this, progressView);
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:id="@+id/main_root">

<ImageView
android:layout_width="match_parent"
android:layout_height="72dp"
android:id="@+id/progressdrawable"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="android:colorAccent">@color/color_accent</item>
<item name="colorAccent">@color/color_accent</item>
<!-- Customize your theme here. -->
</style>

Expand Down
2 changes: 1 addition & 1 deletion gestureTrackerLib.iml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_6" inherit-compiler-output="true">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
Expand Down
6 changes: 3 additions & 3 deletions loadtoast/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ apply plugin: 'com.android.library'
ext {
PUBLISH_GROUP_ID = 'net.steamcrafted'
PUBLISH_ARTIFACT_ID = 'load-toast'
PUBLISH_VERSION = '1.0.9'
PUBLISH_VERSION = '1.0.10'
}

android {
compileSdkVersion 22
compileSdkVersion 23
buildToolsVersion "22.0.1"

defaultConfig {
minSdkVersion 8
targetSdkVersion 22
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
Expand Down
15 changes: 5 additions & 10 deletions loadtoast/loadtoast.iml
Original file line number Diff line number Diff line change
Expand Up @@ -69,31 +69,26 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/bundles" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/coverage-instrumented-classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dependency-cache" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dex" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dex-cache" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jacoco" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/javaResources" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/libs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/lint" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/ndk" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/pre-dexed" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/proguard" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/mockable-android-23.jar" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/resources" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
<excludeFolder url="file://$MODULE_DIR$/build/ivy.xml" />
<excludeFolder url="file://$MODULE_DIR$/build/libs" />
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
<excludeFolder url="file://$MODULE_DIR$/build/poms" />
<excludeFolder url="file://$MODULE_DIR$/build/release" />
<excludeFolder url="file://$MODULE_DIR$/build/release-1.0.10.zip" />
<excludeFolder url="file://$MODULE_DIR$/build/reports" />
<excludeFolder url="file://$MODULE_DIR$/build/test-results" />
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
</content>
<orderEntry type="jdk" jdkName="Android API 22 Platform" jdkType="Android SDK" />
<orderEntry type="jdk" jdkName="Android API 23 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="library-2.4.0" level="project" />
</component>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;

import com.nineoldandroids.animation.ValueAnimator;


/**
* Created by Wannes2 on 23/04/2015.
*/
public class LoadToastView extends View {
public class LoadToastView extends ImageView {

private String mText = "";

Expand All @@ -43,8 +44,9 @@ public class LoadToastView extends View {
private int BASE_TEXT_SIZE = 20;
private int IMAGE_WIDTH = 40;
private int TOAST_HEIGHT = 48;
private float WIDTH_SCALE = 0f;
private int MARQUE_STEP = 1;
private int LINE_WIDTH = 3;
private float WIDTH_SCALE = 0f;
private int MARQUE_STEP = 1;

private long prevUpdate = 0;

Expand All @@ -59,6 +61,7 @@ public class LoadToastView extends View {

private Path toastPath = new Path();
private AccelerateDecelerateInterpolator easeinterpol = new AccelerateDecelerateInterpolator();
private MaterialProgressDrawable spinnerDrawable;

public LoadToastView(Context context) {
super(context);
Expand Down Expand Up @@ -86,6 +89,7 @@ public LoadToastView(Context context) {
BASE_TEXT_SIZE = dpToPx(BASE_TEXT_SIZE);
IMAGE_WIDTH = dpToPx(IMAGE_WIDTH);
TOAST_HEIGHT = dpToPx(TOAST_HEIGHT);
LINE_WIDTH = dpToPx(LINE_WIDTH);
MARQUE_STEP = dpToPx(MARQUE_STEP);

int padding = (TOAST_HEIGHT - IMAGE_WIDTH)/2;
Expand All @@ -111,9 +115,36 @@ public void onAnimationUpdate(ValueAnimator valueAnimator) {
va.setInterpolator(new LinearInterpolator());
va.start();

initSpinner();

calculateBounds();
}

private void initSpinner(){
spinnerDrawable = new MaterialProgressDrawable(getContext(), this);

spinnerDrawable.setStartEndTrim(0, .5f);
spinnerDrawable.setProgressRotation(.5f);

int mDiameter = TOAST_HEIGHT;
int mProgressStokeWidth = LINE_WIDTH;
spinnerDrawable.setSizeParameters(mDiameter, mDiameter,
(mDiameter - mProgressStokeWidth * 2) / 4,
mProgressStokeWidth,
mProgressStokeWidth * 4,
mProgressStokeWidth * 2);

spinnerDrawable.setBackgroundColor(Color.TRANSPARENT);
spinnerDrawable.setColorSchemeColors(loaderPaint.getColor());
spinnerDrawable.setVisible(true, false);
spinnerDrawable.setAlpha(255);

setImageDrawable(null);
setImageDrawable(spinnerDrawable);

spinnerDrawable.start();
}

public void setTextColor(int color){
textPaint.setColor(color);
}
Expand All @@ -125,6 +156,7 @@ public void setBackgroundColor(int color){

public void setProgressColor(int color){
loaderPaint.setColor(color);
spinnerDrawable.setColorSchemeColors(color);
}

public void show(){
Expand Down Expand Up @@ -158,17 +190,17 @@ public void onAnimationUpdate(ValueAnimator valueAnimator) {
}

private int fetchPrimaryColor() {
int color = Color.rgb(155, 155, 155);

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
TypedValue typedValue = new TypedValue();

TypedArray a = getContext().obtainStyledAttributes(typedValue.data, new int[] { android.R.attr.colorAccent });
int color = a.getColor(0, 0);
TypedArray a = getContext().obtainStyledAttributes(typedValue.data, new int[]{android.R.attr.colorAccent});
color = a.getColor(0, color);

a.recycle();

return color;
}
return Color.rgb(155,155,155);
return color;
}

private int dpToPx(int dp){
Expand Down Expand Up @@ -208,8 +240,6 @@ private void calculateBounds() {
}
@Override
protected void onDraw(Canvas c){
super.onDraw(c);

float ws = Math.max(1f - WIDTH_SCALE, 0f);
// If there is nothing to display, just draw a circle
if(mText.length() == 0) ws = 0;
Expand All @@ -227,6 +257,8 @@ protected void onDraw(Canvas c){
int iconoffset = (int)(IMAGE_WIDTH*2*(Math.sqrt(2)-1)/3);
int iw = IMAGE_WIDTH;

float totalWidth = leftMargin * 2 + th + ws*(IMAGE_WIDTH + MAX_TEXT_WIDTH) - translateLoad;

toastPath.reset();
toastPath.moveTo(leftMargin + th / 2, 0);
toastPath.rLineTo(ws*(IMAGE_WIDTH + MAX_TEXT_WIDTH), 0);
Expand All @@ -245,8 +277,9 @@ protected void onDraw(Canvas c){
toastPath.rCubicTo(0, -circleOffset, -circleOffset + th / 2, -th / 2, th / 2, -th / 2);

c.drawCircle(spinnerRect.centerX(), spinnerRect.centerY(), iconBounds.height() / 1.9f, backPaint);
//loadicon.draw(c);

c.drawPath(toastPath, backPaint);
toastPath.reset();

float prog = va.getAnimatedFraction() * 6.0f;
float progrot = prog % 2.0f;
Expand All @@ -255,17 +288,17 @@ protected void onDraw(Canvas c){
proglength = .75f - (prog % 3f - 1.5f);
progrot += (prog % 3f - 1.5f)/1.5f * 2f;
}
//Log.d("spin", "rot " + progrot + " len " + proglength);

toastPath.reset();

if(mText.length() == 0){
ws = Math.max(1f - WIDTH_SCALE, 0f);
}

toastPath.arcTo(spinnerRect, 180 * progrot, Math.min((200 / .75f) * proglength + 1 + 560*(1f-ws),359.9999f));
loaderPaint.setAlpha((int)(255 * ws));
c.drawPath(toastPath, loaderPaint);
c.save();

c.translate((totalWidth - TOAST_HEIGHT)/2, 0);
super.onDraw(c);

c.restore();

if(WIDTH_SCALE > 1f){
Drawable icon = (success) ? completeicon : failedicon;
Expand Down Expand Up @@ -303,7 +336,6 @@ protected void onDraw(Canvas c){
}else{
c.drawText(mText, 0, mText.length(), th / 2 + (MAX_TEXT_WIDTH - mTextBounds.width()) / 2, yPos, textPaint);
}
//c.drawArc(spinnerRect, 360 * progrot, 200 * proglength + 1, true, loaderPaint);
}

@Override
Expand Down
Loading

0 comments on commit 39aa7fb

Please sign in to comment.