Skip to content

Commit

Permalink
Absolute center and dynamic color
Browse files Browse the repository at this point in the history
  • Loading branch information
sujithkanna committed Nov 13, 2016
1 parent 2a383e1 commit 1a3505d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 31 deletions.
38 changes: 17 additions & 21 deletions app/src/main/java/com/msapps/smilyrating/BaseRating.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ protected static class Smileys {
private Smileys(int w, int h) {
mWidth = w;
mHeight = h;
mCenterY = w / 2f;
mCenterY = (h / 2f) + 75;
createGreatSmile();
createGoodSmile();
createOkaySmile();
Expand Down Expand Up @@ -223,25 +223,21 @@ private void createGreatSmile() {
float div = 0.10f;
FloatEvaluator f = new FloatEvaluator();
createSmile(new Point(PADDING, mCenterY),
/*new Point(50, 500),
new Point(50, 525),
new Point(100, 500),
new Point(100, 560),*/
new Point(f.evaluate(div, 50, PADDING), f.evaluate(div, 500, mCenterY)), // Top control
new Point(f.evaluate(div, 50, PADDING), f.evaluate(div, 525, mCenterY)), // Bottom control
new Point(f.evaluate(div, 100, PADDING), f.evaluate(div, 500, mCenterY)), // Top Point
new Point(f.evaluate(div, 100, PADDING), f.evaluate(div, 560, mCenterY)), // Bottom point
new Point(f.evaluate(div, 50, PADDING), f.evaluate(div, mCenterY - 40, mCenterY)), // Top control
new Point(f.evaluate(div, 50, PADDING), f.evaluate(div, mCenterY - 15, mCenterY)), // Bottom control
new Point(f.evaluate(div, 100, PADDING), f.evaluate(div, mCenterY - 40, mCenterY)), // Top Point
new Point(f.evaluate(div, 100, PADDING), f.evaluate(div, mCenterY + 20, mCenterY)), // Bottom point
Smile.MIRROR, GREAT, -1f, -1f, -1f);
}

private void createGoodSmile() {
float div = 0.20f;
FloatEvaluator f = new FloatEvaluator();
createSmile(new Point(PADDING, mCenterY),
new Point(f.evaluate(div, 70, PADDING), f.evaluate(div, 500, mCenterY)), // Top control
new Point(f.evaluate(div, 60, PADDING), f.evaluate(div, 535, mCenterY)), // Bottom control
new Point(f.evaluate(div, 110, PADDING), f.evaluate(div, 520, mCenterY)), // Top Point
new Point(f.evaluate(div, 100, PADDING), f.evaluate(div, 560, mCenterY)), // Bottom point
new Point(f.evaluate(div, 70, PADDING), f.evaluate(div, mCenterY - 40, mCenterY)), // Top control
new Point(f.evaluate(div, 60, PADDING), f.evaluate(div, mCenterY - 5, mCenterY)), // Bottom control
new Point(f.evaluate(div, 110, PADDING), f.evaluate(div, mCenterY - 20, mCenterY)), // Top Point
new Point(f.evaluate(div, 100, PADDING), f.evaluate(div, mCenterY + 20, mCenterY)), // Bottom point
Smile.MIRROR, GOOD, -1f, -1f, -1f);
}

Expand All @@ -254,21 +250,21 @@ private void createBadSmile() {
float div = 0.20f;
FloatEvaluator f = new FloatEvaluator();
createSmile(new Point(PADDING, mCenterY),
new Point(f.evaluate(div, 70, PADDING), f.evaluate(div, 500, mCenterY)), // Top control
new Point(f.evaluate(div, 60, PADDING), f.evaluate(div, 535, mCenterY)), // Bottom control
new Point(f.evaluate(div, 110, PADDING), f.evaluate(div, 520, mCenterY)), // Top Point
new Point(f.evaluate(div, 100, PADDING), f.evaluate(div, 560, mCenterY)), // Bottom point
new Point(f.evaluate(div, 70, PADDING), f.evaluate(div, mCenterY - 40, mCenterY)), // Top control
new Point(f.evaluate(div, 60, PADDING), f.evaluate(div, mCenterY - 5, mCenterY)), // Bottom control
new Point(f.evaluate(div, 110, PADDING), f.evaluate(div, mCenterY - 20, mCenterY)), // Top Point
new Point(f.evaluate(div, 100, PADDING), f.evaluate(div, mCenterY + 20, mCenterY)), // Bottom point
Smile.MIRROR_INVERSE, BAD, -1f, -1f, -1f);
}

private void createTerribleSmile() {
float div = 0.20f;
FloatEvaluator f = new FloatEvaluator();
createSmile(new Point(PADDING, mCenterY),
new Point(f.evaluate(div, 70, PADDING), f.evaluate(div, 500, mCenterY)), // Top control
new Point(f.evaluate(div, 60, PADDING), f.evaluate(div, 535, mCenterY)), // Bottom control
new Point(f.evaluate(div, 110, PADDING), f.evaluate(div, 520, mCenterY)), // Top Point
new Point(f.evaluate(div, 100, PADDING), f.evaluate(div, 560, mCenterY)), // Bottom point
new Point(f.evaluate(div, 70, PADDING), f.evaluate(div, mCenterY - 40, mCenterY)), // Top control
new Point(f.evaluate(div, 60, PADDING), f.evaluate(div, mCenterY - 5, mCenterY)), // Bottom control
new Point(f.evaluate(div, 110, PADDING), f.evaluate(div, mCenterY - 20, mCenterY)), // Top Point
new Point(f.evaluate(div, 100, PADDING), f.evaluate(div, mCenterY + 20, mCenterY)), // Bottom point
Smile.MIRROR_INVERSE, TERRIBLE, -1f, -1f, -1f);
}

Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/msapps/smilyrating/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.msapps.smilyrating;

import android.animation.ArgbEvaluator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
Expand Down
27 changes: 17 additions & 10 deletions app/src/main/java/com/msapps/smilyrating/RatingView.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.msapps.smilyrating;

import android.animation.ArgbEvaluator;
import android.animation.FloatEvaluator;
import android.animation.ValueAnimator;
import android.content.Context;
Expand All @@ -17,10 +18,14 @@ public class RatingView extends BaseRating implements ValueAnimator.AnimatorUpda

private static final String TAG = "RatingView";

private static final int ANGRY_COLOR = Color.parseColor("#f29a68");
private static final int NORMAL_COLOR = Color.parseColor("#f2dd68");
private static final int PAINT_COLOR = Color.parseColor("#353431");

private boolean mShowLines = false;
private boolean mShowPoints = false;

private float mEyeRadius = 22f;
private float mEyeRadius = 25f;
private Paint mPathPaint = new Paint();
private Paint mBackgroundPaint = new Paint();

Expand All @@ -31,6 +36,7 @@ public class RatingView extends BaseRating implements ValueAnimator.AnimatorUpda
private Path mSmilePath = new Path();
private ValueAnimator mValueAnimator = new ValueAnimator();
private FloatEvaluator mFloatEvaluator = new FloatEvaluator();
private ArgbEvaluator mColorEvaluator = new ArgbEvaluator();

@BaseRating.Smiley
private int mode = RatingView.GOOD;
Expand All @@ -39,6 +45,7 @@ public class RatingView extends BaseRating implements ValueAnimator.AnimatorUpda
private float mTranslation = 0;
private int mWidth;
private int mHeight;
private float mCenterY;

public RatingView(Context context) {
super(context);
Expand All @@ -58,7 +65,7 @@ public RatingView(Context context, AttributeSet attrs, int defStyleAttr) {
private void init() {
mPathPaint.setAntiAlias(true);
mPathPaint.setStrokeWidth(3);
mPathPaint.setColor(Color.parseColor("#232323"));
mPathPaint.setColor(PAINT_COLOR);
mPathPaint.setStyle(Paint.Style.FILL);

mPointPaint1.setColor(Color.RED);
Expand All @@ -67,10 +74,6 @@ private void init() {
mPointPaint2.setColor(Color.BLUE);
mPointPaint2.setStyle(Paint.Style.STROKE);


int color = Color.BLACK;
mBackgroundPaint.setColor(Color.argb(50, Color.red(color),
Color.green(color), Color.blue(color)));
mBackgroundPaint.setStyle(Paint.Style.FILL);

mValueAnimator.setDuration(1000);
Expand All @@ -95,14 +98,15 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mWidth = getMeasuredWidth();
mHeight = getMeasuredHeight();
mCenterY = mHeight / 2f;
mSmileys = Smileys.newInstance(mWidth, mHeight);
setFraction(0);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(PADDING + mTranslation, 475, 160, mBackgroundPaint);
canvas.drawCircle(PADDING + mTranslation, mHeight / 2f, 160, mBackgroundPaint);
if (!mSmilePath.isEmpty()) {
canvas.drawPath(mSmilePath, mPathPaint);
canvas.drawPath(mEyePathLeft, mPathPaint);
Expand Down Expand Up @@ -147,24 +151,27 @@ public void setFraction(float fraction) {
if (fraction > 0.75f) {
fraction -= 0.75f;
fraction *= 4;

mBackgroundPaint.setColor(NORMAL_COLOR);
transformSmile(mTranslation, fraction, mSmilePath,
mSmileys.getSmile(GOOD), mSmileys.getSmile(GREAT), mFloatEvaluator);
createEyeLocation(fraction, GREAT);
} else if (fraction > 0.50f) {
fraction -= 0.50f;
fraction *= 4;
mBackgroundPaint.setColor(NORMAL_COLOR);
transformSmile(mTranslation, fraction, mSmilePath,
mSmileys.getSmile(OKAY), mSmileys.getSmile(GOOD), mFloatEvaluator);
createEyeLocation(fraction, GOOD);
} else if (fraction > 0.25f) {
fraction -= 0.25f;
fraction *= 4;
mBackgroundPaint.setColor(NORMAL_COLOR);
transformSmile(mTranslation, fraction, mSmilePath,
mSmileys.getSmile(BAD), mSmileys.getSmile(OKAY), mFloatEvaluator);
createEyeLocation(fraction, BAD);
} else {
fraction *= 4;
mBackgroundPaint.setColor((Integer) mColorEvaluator.evaluate(fraction, ANGRY_COLOR, NORMAL_COLOR));
transformSmile(mTranslation, fraction, mSmilePath,
mSmileys.getSmile(TERRIBLE), mSmileys.getSmile(BAD), mFloatEvaluator);
createEyeLocation(fraction, TERRIBLE);
Expand All @@ -179,9 +186,9 @@ private void createEyeLocation(float fraction, @Smiley int smile) {
eyeLeft.radius = mEyeRadius;
eyeRight.radius = mEyeRadius;
eyeLeft.center.x = 120 + mTranslation;
eyeLeft.center.y = 435;
eyeLeft.center.y = mCenterY - 50;
eyeRight.center.x = 220 + mTranslation;
eyeRight.center.y = 435;
eyeRight.center.y = mCenterY - 45;
mEyePathLeft = eyeLeft.fillPath(mEyePathLeft);
mEyePathRight = eyeRight.fillPath(mEyePathRight);
}
Expand Down

0 comments on commit 1a3505d

Please sign in to comment.