From 22c5b76fd5e5153521fb936c48afc273b19b5a13 Mon Sep 17 00:00:00 2001 From: Yellow5A5 Date: Sat, 20 Feb 2016 01:39:53 +0800 Subject: [PATCH] Update: Update letters Animation. --- .../coolanim/lib/PelletManager.java | 22 +++--- .../coolanim/lib/letter/ALetter.java | 51 +++++++++++-- .../coolanim/lib/letter/DLetter.java | 76 +++++++++++++++++++ .../coolanim/lib/letter/OLetter.java | 10 ++- 4 files changed, 142 insertions(+), 17 deletions(-) create mode 100644 lib/src/main/java/com/tomandjerry/coolanim/lib/letter/DLetter.java diff --git a/lib/src/main/java/com/tomandjerry/coolanim/lib/PelletManager.java b/lib/src/main/java/com/tomandjerry/coolanim/lib/PelletManager.java index 68c529c..e308e41 100644 --- a/lib/src/main/java/com/tomandjerry/coolanim/lib/PelletManager.java +++ b/lib/src/main/java/com/tomandjerry/coolanim/lib/PelletManager.java @@ -2,6 +2,8 @@ import android.graphics.Canvas; +import com.tomandjerry.coolanim.lib.letter.ALetter; +import com.tomandjerry.coolanim.lib.letter.DLetter; import com.tomandjerry.coolanim.lib.letter.ILetter; import com.tomandjerry.coolanim.lib.letter.LLetter; import com.tomandjerry.coolanim.lib.letter.Letter; @@ -31,13 +33,15 @@ public PelletManager() { } public void initPellets(){ - this.setPellet(new FirstPellet(200, 300)); - this.setPellet(new ThirdPellet(400, 300)); - this.setPellet(new SecondPellet(300, 300)); - this.setPellet(new ForthPellet(500, 300)); - this.setLetter(new LLetter(100, 300)); - this.setLetter(new OLetter(600, 300)); - this.setLetter(new ILetter(500, 500)); + this.addPellet(new FirstPellet(200, 300)); + this.addPellet(new ThirdPellet(400, 300)); + this.addPellet(new SecondPellet(300, 300)); + this.addPellet(new ForthPellet(500, 300)); + this.addLetter(new LLetter(100, 500)); + this.addLetter(new OLetter(220, 500)); + this.addLetter(new ALetter(340, 500)); + this.addLetter(new DLetter(460, 500)); + this.addLetter(new ILetter(580, 500)); mBall = SmallYellowBall.getInstance(); showText(); @@ -49,14 +53,14 @@ public void showText() { } } - public void setPellet(Pellet pellet){ + public void addPellet(Pellet pellet){ if(pellet != null){ mPellets.add(pellet); pellet.prepareAnim(); } } - public void setLetter(Letter letter) { + public void addLetter(Letter letter) { if (letter != null) { mLetters.add(letter); } diff --git a/lib/src/main/java/com/tomandjerry/coolanim/lib/letter/ALetter.java b/lib/src/main/java/com/tomandjerry/coolanim/lib/letter/ALetter.java index 4605a04..f50e7ee 100644 --- a/lib/src/main/java/com/tomandjerry/coolanim/lib/letter/ALetter.java +++ b/lib/src/main/java/com/tomandjerry/coolanim/lib/letter/ALetter.java @@ -2,35 +2,74 @@ import android.animation.ValueAnimator; import android.graphics.Canvas; -import android.graphics.Color; import android.graphics.Paint; +import android.graphics.Point; +import android.graphics.RectF; import com.tomandjerry.coolanim.lib.Config; /** * Created by Weiwu on 16/2/19. */ -public class ALetter extends Letter{ +public class ALetter extends Letter { private Paint mPaint; - private int mDegrees; - private ValueAnimator mFirAnimator; + private RectF mRectF; + //圆角度起点 + private int mStartAngle = 0; + //变化角度 + private int mSweepAngle = 0; + //字母边长 + private int mLength = 120; + //线粗 + private int mStrokeWidth = 20; + private ValueAnimator mAnimator; + + private Point mFirPoint; + private Point mSecPoint; public ALetter(int x, int y) { super(x, y); mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mPaint.setColor(Config.WHITE); + mPaint.setStrokeWidth(mStrokeWidth); + mPaint.setStyle(Paint.Style.STROKE); + + //除去线粗带来的偏差 + int offsetSub = mLength / 2 - mStrokeWidth / 2; + //对其圆右边内边位置 + mFirPoint = new Point(mCurX + offsetSub, mCurY + mLength / 2); + mSecPoint = new Point(mFirPoint); + //圆向内偏移 + mRectF = new RectF(mCurX - offsetSub, mCurY - offsetSub, mCurX + offsetSub, mCurY + offsetSub); } @Override public void startAnim() { - + mAnimator = ValueAnimator.ofFloat(0, 1).setDuration(1500); + mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { + @Override + public void onAnimationUpdate(ValueAnimator animation) { + float factor = (float) animation.getAnimatedValue(); + //竖线 + mSecPoint.y = (int) (mFirPoint.y - mLength * factor); + if (factor > 0.5f) { + //中点处开始画圆 + float zoroToOne = (factor - 0.5f) * 2; + mSweepAngle = -(int) (360 * zoroToOne); + } + } + }); + mAnimator.start(); } @Override public void drawSelf(Canvas canvas) { - super.drawSelf(canvas); + //竖线 + canvas.drawLine(mFirPoint.x, mFirPoint.y, mSecPoint.x, mSecPoint.y, mPaint); + //圆 + canvas.drawArc(mRectF, mStartAngle, mSweepAngle, false, mPaint); } } diff --git a/lib/src/main/java/com/tomandjerry/coolanim/lib/letter/DLetter.java b/lib/src/main/java/com/tomandjerry/coolanim/lib/letter/DLetter.java new file mode 100644 index 0000000..484fe86 --- /dev/null +++ b/lib/src/main/java/com/tomandjerry/coolanim/lib/letter/DLetter.java @@ -0,0 +1,76 @@ +package com.tomandjerry.coolanim.lib.letter; + +import android.animation.ValueAnimator; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.graphics.Point; +import android.graphics.RectF; + +import com.tomandjerry.coolanim.lib.Config; + +/** + * Created by Weiwu on 16/2/20. + */ +public class DLetter extends Letter { + + private Paint mPaint; + + private RectF mRectF; + //圆角度起点 + private int mStartAngle = 0; + //变化角度 + private int mSweepAngle = 0; + //字母边长 + private int mRadius = 120; + private int mLineLength = 180; + //线粗 + private int mStrokeWidth = 20; + private ValueAnimator mAnimator; + + private Point mFirPoint; + private Point mSecPoint; + + public DLetter(int x, int y) { + super(x, y); + mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); + mPaint.setColor(Config.WHITE); + mPaint.setStrokeWidth(mStrokeWidth); + mPaint.setStyle(Paint.Style.STROKE); + + //除去线粗带来的偏差 + int offsetSub = mRadius / 2 - mStrokeWidth / 2; + //对其圆右边内边位置 + mFirPoint = new Point(mCurX + offsetSub, mCurY + mRadius / 2); + mSecPoint = new Point(mFirPoint); + //圆向内偏移 + mRectF = new RectF(mCurX - offsetSub, mCurY - offsetSub, mCurX + offsetSub, mCurY + offsetSub); + + } + + @Override + public void startAnim() { + mAnimator = ValueAnimator.ofFloat(0, 1).setDuration(1500); + mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { + @Override + public void onAnimationUpdate(ValueAnimator animation) { + float factor = (float) animation.getAnimatedValue(); + //竖线 + mSecPoint.y = (int) (mFirPoint.y - mLineLength * factor); + if (factor > 0.333f) { + //2/3处开始画圆 + float zoroToOne = (factor - 0.333f) * 3 / 2; + mSweepAngle = -(int) (360 * zoroToOne); + } + } + }); + mAnimator.start(); + } + + @Override + public void drawSelf(Canvas canvas) { + //竖线 + canvas.drawLine(mFirPoint.x, mFirPoint.y, mSecPoint.x, mSecPoint.y, mPaint); + //圆 + canvas.drawArc(mRectF, mStartAngle, mSweepAngle, false, mPaint); + } +} diff --git a/lib/src/main/java/com/tomandjerry/coolanim/lib/letter/OLetter.java b/lib/src/main/java/com/tomandjerry/coolanim/lib/letter/OLetter.java index dccb598..256c279 100644 --- a/lib/src/main/java/com/tomandjerry/coolanim/lib/letter/OLetter.java +++ b/lib/src/main/java/com/tomandjerry/coolanim/lib/letter/OLetter.java @@ -35,8 +35,14 @@ public void startAnim() { @Override public void onAnimationUpdate(ValueAnimator animation) { float factor = (float) animation.getAnimatedValue(); - mStartAngle = (int) (180 * factor); - mSweepAngle = (int) (360*factor); + if(factor < 0.5f) { + mStartAngle = (int) (90 * factor); + mSweepAngle = (int) (180 * factor); + }else{ + float zoroToOne = (float) ((factor - 0.5) * 2); + mStartAngle = (int) (45 + 135 * zoroToOne); + mSweepAngle = (int) (90 + 270 * zoroToOne); + } } });