Skip to content

Commit

Permalink
Merge pull request #21 from Tibolte/feature/animated_vector_compat
Browse files Browse the repository at this point in the history
Feature/animated vector compat
  • Loading branch information
Tibolte committed May 30, 2015
2 parents 49ad6de + 9bc2946 commit 65d81bd
Show file tree
Hide file tree
Showing 25 changed files with 3,555 additions and 133 deletions.
11 changes: 6 additions & 5 deletions elasticdownload/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
compileSdkVersion 22
buildToolsVersion "22.0.1"

defaultConfig {
minSdkVersion 21
targetSdkVersion 21
minSdkVersion 8
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
Expand All @@ -22,5 +22,6 @@ apply from: 'https://raw.github.com/chrisbanes/gradle-mvn-push/master/gradle-mvn

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.android.support:appcompat-v7:22.1.1'
compile 'com.nineoldandroids:library:2.4.0+'
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ public class AVDWrapper {

@Override
public void run() {
if (mCallback != null)
if (mCallback != null) {
mCallback.onAnimationDone();
}
}
};

public interface Callback {
public void onAnimationDone();

public void onAnimationStopped();
}

Expand All @@ -42,8 +44,7 @@ public void stop() {
mDrawable.stop();
mHandler.removeCallbacks(mAnimationDoneRunnable);

if (mCallback != null)
mCallback.onAnimationStopped();
if (mCallback != null) mCallback.onAnimationStopped();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.ViewTreeObserver;
Expand All @@ -10,7 +11,7 @@
/**
* Created by thibaultguegan on 15/03/15.
*/
public class ElasticDownloadView extends FrameLayout implements IntroView.EnterAnimationListener{
public class ElasticDownloadView extends FrameLayout implements IntroView.EnterAnimationListener {

private static final String LOG_TAG = ElasticDownloadView.class.getSimpleName();

Expand Down Expand Up @@ -51,7 +52,11 @@ protected void onFinishInflate() {
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mProgressDownloadView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
mProgressDownloadView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
mProgressDownloadView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
mIntroView.getLayoutParams().width = mProgressDownloadView.getWidth();
mIntroView.getLayoutParams().height = mProgressDownloadView.getHeight();

Expand Down
13 changes: 11 additions & 2 deletions elasticdownload/src/main/java/is/arontibo/library/IntroView.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import android.content.Context;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;

import is.arontibo.library.VectorCompat.AnimatedVectorDrawable;

/**
* Created by thibaultguegan on 15/03/15.
*/
Expand All @@ -27,8 +30,12 @@ public interface EnterAnimationListener {

public IntroView(Context context, AttributeSet attrs) {
super(context, attrs);

setImageResource(R.drawable.avd_start);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setImageResource(R.drawable.avd_start);
} else {
AnimatedVectorDrawable drawable = AnimatedVectorDrawable.getDrawable(context, R.drawable.avd_start);
setImageDrawable(drawable);
}
}

/**
Expand All @@ -44,6 +51,7 @@ public void setListener(EnterAnimationListener listener) {
*/

public void startAnimation() {

Drawable drawable = getDrawable();
Animatable animatable = (Animatable) drawable;

Expand All @@ -63,4 +71,5 @@ public void onAnimationStopped() {
AVDWrapper wrapper = new AVDWrapper(animatable, new Handler(), callback);
wrapper.start(getContext().getResources().getInteger(R.integer.enter_animation_duration));
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package is.arontibo.library;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.CornerPathEffect;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
Expand All @@ -19,6 +15,10 @@
import android.view.animation.DecelerateInterpolator;
import android.view.animation.OvershootInterpolator;

import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.AnimatorSet;
import com.nineoldandroids.animation.ObjectAnimator;

/**
* Created by thibaultguegan on 15/02/15.
*/
Expand Down Expand Up @@ -48,22 +48,22 @@ private enum State {
public ProgressDownloadView(Context context, AttributeSet attrs) {
super(context, attrs);

mPadding = (int) (30*mDensity);
mBubbleWidth = (int) (45*mDensity);
mBubbleHeight = (int) (35*mDensity);
mPadding = (int) (30 * mDensity);
mBubbleWidth = (int) (45 * mDensity);
mBubbleHeight = (int) (35 * mDensity);

setPadding(mPadding, 0, mPadding, 0);

mPaintBlack = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaintBlack.setStyle(Paint.Style.STROKE);
mPaintBlack.setStrokeWidth(5*mDensity);
mPaintBlack.setStrokeWidth(5 * mDensity);
mPaintBlack.setColor(getResources().getColor(R.color.red_wood));
mPaintBlack.setStrokeCap(Paint.Cap.ROUND);
//mPaintBlack.setPathEffect(new CornerPathEffect(5*mDensity));

mPaintWhite = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaintWhite.setStyle(Paint.Style.STROKE);
mPaintWhite.setStrokeWidth(5*mDensity);
mPaintWhite.setStrokeWidth(5 * mDensity);
mPaintWhite.setColor(Color.WHITE);
mPaintWhite.setStrokeCap(Paint.Cap.ROUND);
//mPaintWhite.setPathEffect(new CornerPathEffect(5*mDensity));
Expand All @@ -75,7 +75,7 @@ public ProgressDownloadView(Context context, AttributeSet attrs) {
mPaintText = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaintText.setColor(Color.BLACK);
mPaintText.setStyle(Paint.Style.FILL);
mPaintText.setTextSize(12*mDensity);
mPaintText.setTextSize(12 * mDensity);
}

/**
Expand All @@ -84,25 +84,25 @@ public ProgressDownloadView(Context context, AttributeSet attrs) {

@Override
protected void onDraw(Canvas canvas) {
if(mPathWhite != null && mPathBlack != null) {
if (mPathWhite != null && mPathBlack != null) {

float textX = Math.max(getPaddingLeft()-(int)(mBubbleWidth/4.0f), mProgress*mWidth/100-(int)(mBubbleWidth/4.0f));
float textY = mHeight/2-mBubbleHeight/2 + calculateDeltaY();
float textX = Math.max(getPaddingLeft() - (int) (mBubbleWidth / 4.0f), mProgress * mWidth / 100 - (int) (mBubbleWidth / 4.0f));
float textY = mHeight / 2 - mBubbleHeight / 2 + calculateDeltaY();

switch (mState) {
case STATE_WORKING:
//save and restore prevent the rest of the canvas to not be rotated
canvas.save();
float speed = (getProgress() - mTarget)/20;
mBubbleAngle += speed*10;
if(mBubbleAngle > 20) {
float speed = (getProgress() - mTarget) / 20;
mBubbleAngle += speed * 10;
if (mBubbleAngle > 20) {
mBubbleAngle = 20;
}
if(mBubbleAngle < -20) {
if (mBubbleAngle < -20) {
mBubbleAngle = -20;
}
if(Math.abs(speed) < 1) {
mSpeedAngle -= mBubbleAngle/20;
if (Math.abs(speed) < 1) {
mSpeedAngle -= mBubbleAngle / 20;
mSpeedAngle *= .9f;
}
mBubbleAngle += mSpeedAngle;
Expand All @@ -118,14 +118,14 @@ protected void onDraw(Canvas canvas) {
canvas.drawPath(mPathBubble, mPaintBubble);
canvas.rotate(mFailAngle, bubbleAnchorX, textY - mBubbleHeight / 7);
mPaintText.setColor(getResources().getColor(R.color.red_wine));
textX = Math.max(getPaddingLeft()-(int)(mBubbleWidth/3.2f), mProgress*mWidth/100-(int)(mBubbleWidth/3.2f));
textX = Math.max(getPaddingLeft() - (int) (mBubbleWidth / 3.2f), mProgress * mWidth / 100 - (int) (mBubbleWidth / 3.2f));
canvas.drawText(getResources().getString(R.string.failed), textX, textY, mPaintText);
canvas.restore();
break;
case STATE_SUCCESS:
canvas.save();
mPaintText.setColor(getResources().getColor(R.color.green_grass));
textX = Math.max(getPaddingLeft()-(int)(mBubbleWidth/3.2f), mProgress*mWidth/100-(int)(mBubbleWidth/3.2f));
textX = Math.max(getPaddingLeft() - (int) (mBubbleWidth / 3.2f), mProgress * mWidth / 100 - (int) (mBubbleWidth / 3.2f));
Matrix flipMatrix = new Matrix();
flipMatrix.setScale(mFlipFactor, 1, bubbleAnchorX, bubbleAnchorY);
canvas.concat(flipMatrix);
Expand Down Expand Up @@ -158,20 +158,20 @@ protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld) {

private void makePathBlack() {

if(mPathBlack ==null) {
if (mPathBlack == null) {
mPathBlack = new Path();
}

Path p = new Path();
p.moveTo(Math.max(getPaddingLeft(), mProgress*mWidth/100), mHeight/2 + calculateDeltaY());
p.lineTo(mWidth, mHeight/2);
Path p = new Path();
p.moveTo(Math.max(getPaddingLeft(), mProgress * mWidth / 100), mHeight / 2 + calculateDeltaY());
p.lineTo(mWidth, mHeight / 2);

mPathBlack.set(p);
}

private void makePathWhite() {

if(mPathWhite == null) {
if (mPathWhite == null) {
mPathWhite = new Path();
}

Expand All @@ -184,51 +184,51 @@ private void makePathWhite() {

private void makePathBubble() {

if(mPathBubble == null) {
if (mPathBubble == null) {
mPathBubble = new Path();
}

int width = mBubbleWidth;
int height = mBubbleHeight;
int arrowWidth = width/3;
int arrowWidth = width / 3;

//Rect r = new Rect(Math.max(getPaddingLeft()-width/2-arrowWidth/4, mProgress*mWidth/100-width/2-arrowWidth/4), mHeight/2-height + calculatedeltaY(), Math.max(getPaddingLeft()+width/2-arrowWidth/4, mProgress*mWidth/100+width/2-arrowWidth/4), mHeight/2+height-height + calculatedeltaY());
Rect r = new Rect((int) (Math.max(getPaddingLeft()-width/2, mProgress*mWidth/100-width/2)), (int) (mHeight/2-height + calculateDeltaY()), (int) (Math.max(getPaddingLeft()+width/2, mProgress*mWidth/100+width/2)), (int) (mHeight/2+height-height + calculateDeltaY()));
int arrowHeight = (int) (arrowWidth/1.5f);
Rect r = new Rect((int) (Math.max(getPaddingLeft() - width / 2, mProgress * mWidth / 100 - width / 2)), (int) (mHeight / 2 - height + calculateDeltaY()), (int) (Math.max(getPaddingLeft() + width / 2, mProgress * mWidth / 100 + width / 2)), (int) (mHeight / 2 + height - height + calculateDeltaY()));
int arrowHeight = (int) (arrowWidth / 1.5f);
int radius = 8;

Path path = new Path();

//down arrow
path.moveTo(r.left + r.width()/2-arrowWidth/2, r.top + r.height()-arrowHeight);
bubbleAnchorX = r.left + r.width()/2;
path.moveTo(r.left + r.width() / 2 - arrowWidth / 2, r.top + r.height() - arrowHeight);
bubbleAnchorX = r.left + r.width() / 2;
bubbleAnchorY = r.top + r.height();
path.lineTo(bubbleAnchorX, bubbleAnchorY);
path.lineTo(r.left + r.width()/2+arrowWidth/2, r.top + r.height()-arrowHeight);
path.lineTo(r.left + r.width() / 2 + arrowWidth / 2, r.top + r.height() - arrowHeight);

//go to bottom-right
path.lineTo(r.left + r.width()-radius, r.top + r.height()-arrowHeight);
path.lineTo(r.left + r.width() - radius, r.top + r.height() - arrowHeight);

//bottom-right arc
path.arcTo(new RectF(r.left + r.width()-2*radius, r.top + r.height()-arrowHeight-2*radius, r.left + r.width(), r.top + r.height()-arrowHeight), 90, -90);
path.arcTo(new RectF(r.left + r.width() - 2 * radius, r.top + r.height() - arrowHeight - 2 * radius, r.left + r.width(), r.top + r.height() - arrowHeight), 90, -90);

//go to upper-right
path.lineTo(r.left + r.width(), r.top + arrowHeight);

//upper-right arc
path.arcTo(new RectF(r.left + r.width()-2*radius, r.top, r.right, r.top + 2*radius), 0, -90);
path.arcTo(new RectF(r.left + r.width() - 2 * radius, r.top, r.right, r.top + 2 * radius), 0, -90);

//go to upper-left
path.lineTo(r.left + radius, r.top);

//upper-left arc
path.arcTo(new RectF(r.left, r.top, r.left + 2*radius, r.top + 2*radius), 270, -90);
path.arcTo(new RectF(r.left, r.top, r.left + 2 * radius, r.top + 2 * radius), 270, -90);

//go to bottom-left
path.lineTo(r.left, r.top + r.height()-arrowHeight-radius);
path.lineTo(r.left, r.top + r.height() - arrowHeight - radius);

//bottom-left arc
path.arcTo(new RectF(r.left, r.top + r.height()-arrowHeight-2*radius, r.left + 2*radius, r.top + r.height()-arrowHeight), 180, -90);
path.arcTo(new RectF(r.left, r.top + r.height() - arrowHeight - 2 * radius, r.left + 2 * radius, r.top + r.height() - arrowHeight), 180, -90);

path.close();

Expand All @@ -241,23 +241,23 @@ private void makePathBubble() {

private float calculateDeltaY() {
int wireTension = 15;
if(mProgress <= 50) {
return (mProgress * mWidth/wireTension)/50 + Math.abs((mTarget-getProgress())/wireTension) + Math.abs(mBubbleAngle);
if (mProgress <= 50) {
return (mProgress * mWidth / wireTension) / 50 + Math.abs((mTarget - getProgress()) / wireTension) + Math.abs(mBubbleAngle);
} else {
return ((100-mProgress) * mWidth/wireTension)/50 + Math.abs((mTarget-getProgress())/wireTension) + Math.abs(mBubbleAngle);
return ((100 - mProgress) * mWidth / wireTension) / 50 + Math.abs((mTarget - getProgress()) / wireTension) + Math.abs(mBubbleAngle);
}
}

public void setPercentage(float newProgress) {
if(newProgress < 0 || newProgress > 100)
if (newProgress < 0 || newProgress > 100)
throw new IllegalArgumentException("setPercentage not between 0 and 100");

mState = State.STATE_WORKING;
mTarget = newProgress;

ObjectAnimator anim = ObjectAnimator.ofFloat(this, "progress", getProgress(), mTarget);
anim.setInterpolator(new DecelerateInterpolator());
anim.setDuration((long) (ANIMATION_DURATION_BASE+Math.abs(mTarget*10-getProgress()*10)/2));
anim.setDuration((long) (ANIMATION_DURATION_BASE + Math.abs(mTarget * 10 - getProgress() * 10) / 2));
anim.start();
}

Expand Down Expand Up @@ -300,7 +300,7 @@ public void drawFail() {
anim.setInterpolator(new AccelerateInterpolator());

AnimatorSet set = new AnimatorSet();
set.setDuration((long) (ANIMATION_DURATION_BASE/1.7f));
set.setDuration((long) (ANIMATION_DURATION_BASE / 1.7f));
set.playTogether(
failAnim,
anim
Expand Down
Loading

0 comments on commit 65d81bd

Please sign in to comment.