Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customized smiley - fix callback handling #54

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

</activity>
<activity android:name=".RecyclerViewExample" />
<activity android:name=".CustomSmileyActivity" />

</application>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.example.smilyrating;

import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.hsalf.smileyrating.SmileyRating;

/**
* This class is used for ...
*
* @autor MAO Hieng 3/30/2020
*/
public class CustomSmileyActivity extends AppCompatActivity {

private static final String TAG = "CustomSmileyActivity";

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customized_smiley);
SmileyRating rating = findViewById(R.id.smiley);

rating.setMaxSmiley(3);
rating.setReverseSmiley(true);
rating.setSmileySelectedListener(new SmileyRating.OnSmileySelectedListener() {
@Override
public void onSmileySelected(SmileyRating.Type type) {
Log.i(TAG, "onSmileySelected: "+type);
}
});
rating.setRating(1);
}
}
4 changes: 4 additions & 0 deletions app/src/main/java/com/example/smilyrating/MenuActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ public void onClick(View v) {
}
startActivity(intent);
}

public void clickStartCustomizedSmiley(View view) {
startActivity(new Intent(this, CustomSmileyActivity.class));
}
}
12 changes: 12 additions & 0 deletions app/src/main/res/layout/activity_customized_smiley.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<com.hsalf.smileyrating.SmileyRating
android:id="@+id/smiley"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>
11 changes: 9 additions & 2 deletions app/src/main/res/layout/activity_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
android:orientation="vertical">
android:orientation="vertical"
android:padding="20dp">

<Button
android:id="@+id/smiley_rating"
Expand Down Expand Up @@ -37,6 +37,13 @@
android:gravity="center"
android:text="Smiley Rating 2 list" />

<Button
android:layout_width="match_parent"
android:layout_height="70dp"
android:gravity="center"
android:onClick="clickStartCustomizedSmiley"
android:text="Customized Smiley" />

</LinearLayout>

</ScrollView>
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
classpath 'com.android.tools.build:gradle:3.6.1'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
56 changes: 51 additions & 5 deletions smilerating/src/main/java/com/hsalf/smileyrating/SmileyRating.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import android.graphics.Typeface;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
Expand All @@ -29,6 +30,10 @@
import com.hsalf.smileyrating.smileys.Terrible;
import com.hsalf.smileyrating.smileys.base.Smiley;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class SmileyRating extends View implements TouchActiveIndicator {

private static final String TAG = "SmileyRating";
Expand All @@ -45,10 +50,13 @@ public class SmileyRating extends View implements TouchActiveIndicator {
private static final FloatEvaluator FLOAT_EVALUATOR = new FloatEvaluator();
private static final FractionEvaluator FRACTION_EVALUATOR = new FractionEvaluator();

private Smiley[] mSmileys = new Smiley[]{
private static final Smiley[] sDefaultSmileys = new Smiley[]{
new Terrible(), new Bad(), new Okay(), new Good(), new Great()
};

private Smiley[] mSmileys = sDefaultSmileys;
private Smiley[] mOriginalSmileys;

private Text[] mTitlePoints = new Text[]{
new Text(), new Text(), new Text(), new Text(), new Text()
};
Expand Down Expand Up @@ -282,6 +290,9 @@ private void setSmileyPosition(float pointX) {

int index = (int) Math.floor(fraction / .202f);

if (index >= mSmileys.length)
index = mSmileys.length - 1;

RectF holder = mPlaceHolders[index];

int endIndex;
Expand Down Expand Up @@ -492,10 +503,13 @@ public boolean onTouchEvent(MotionEvent event) {
}

private void animateAppearance(int index) {
Type type = Type.values()[index];
// Type type = Type.values()[index];
Type type = mSmileys[index].getType();

if (mSelectedSmiley == type) {
return;
}

mSelectedSmiley = type;
setSmileyPosition(mPlaceHolders[index].centerX());
clearAppearAnimation();
Expand Down Expand Up @@ -531,15 +545,15 @@ private void moveFaceToNearestPlace() {
index = i;
}
}
mSelectedSmiley = Type.values()[index];
mSelectedSmiley = mSmileys[index].getType();//Type.values()[index];
animateSmileyTo(mPlaceHolders[index]);
}

private void animateSmileyTo(float x, float y) {
for (int i = 0; i < mPlaceHolders.length; i++) {
RectF holder = mPlaceHolders[i];
if (inFaceBounds(x, y, holder)) {
mSelectedSmiley = Type.values()[i];
mSelectedSmiley = mSmileys[i].getType();//Type.values()[i];
animateSmileyTo(holder);
break;
}
Expand All @@ -552,6 +566,38 @@ private void animateSmileyTo(RectF rectF) {
mSlideAnimator.start();
}

public void setSmileys(Smiley[] mSmileys) {
this.mSmileys = mSmileys;
mPlaceHolders = new RectF[mSmileys.length];
mPlaceHolderPaths = new Path[mSmileys.length];

invalidate();
}

public final void setMaxSmiley(int maxSmiley) {
if (maxSmiley > 0 && maxSmiley <= sDefaultSmileys.length) {
Smiley[] smilies = Arrays.copyOfRange(sDefaultSmileys, 0, maxSmiley);
setSmileys(smilies);
}
}

public final void setReverseSmiley(boolean reverse) {
if (mOriginalSmileys == null)
mOriginalSmileys = this.mSmileys;

Smiley[] smilies;
List<Smiley> newSmileys;
if (reverse) {
newSmileys = Arrays.asList(mOriginalSmileys);
Collections.reverse(newSmileys);
smilies = newSmileys.toArray(new Smiley[newSmileys.size()]);
} else {
smilies = mOriginalSmileys;
}

setSmileys(smilies);
}

public void setRating(Type type) {
setRating(type, false);
}
Expand All @@ -572,7 +618,7 @@ public void setRating(int rating, boolean animate) {
resetSmiley();
return;
}
mSelectedSmiley = Type.values()[rating - 1];
mSelectedSmiley = mSmileys[rating-1].getType();//Type.values()[rating - 1];
if (mInflationDone) {
if (!animate) {
setSmileyPosition(mPlaceHolders[rating - 1].centerX());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import android.graphics.Color;

import com.hsalf.smilerating.Point;
import com.hsalf.smileyrating.SmileyRating;
import com.hsalf.smileyrating.smileys.base.Smiley;

public class Bad extends Smiley {

public Bad() {
super(-90, 270);
super(-90, 270, SmileyRating.Type.BAD);

float div = 0.20f;
FloatEvaluator f = new FloatEvaluator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import android.graphics.Color;

import com.hsalf.smilerating.Point;
import com.hsalf.smileyrating.SmileyRating;
import com.hsalf.smileyrating.smileys.base.Smiley;

public class Good extends Smiley {

public Good() {
super(-135, 360);
super(-135, 360, SmileyRating.Type.GOOD);

float div = 0.20f;
FloatEvaluator f = new FloatEvaluator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import android.graphics.Color;

import com.hsalf.smilerating.Point;
import com.hsalf.smileyrating.SmileyRating;
import com.hsalf.smileyrating.smileys.base.Smiley;

public class Great extends Smiley {

public Great() {
super(-135, 360);
super(-135, 360, SmileyRating.Type.GREAT);

// Create mouth
float div = 0.10f;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import android.graphics.Color;

import com.hsalf.smilerating.Point;
import com.hsalf.smileyrating.SmileyRating;
import com.hsalf.smileyrating.smileys.base.Smiley;

public class Okay extends Smiley {

public Okay() {
super(-135, 360);
super(-135, 360, SmileyRating.Type.OKAY);
createStraightSmile(new Point(CENTER_SMILE, MOUTH_CENTER_Y), (CENTER_SMILE * 0.1f), 350f, (CENTER_SMILE * 0.9f));
setup(
getClass().getSimpleName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import android.graphics.Color;

import com.hsalf.smilerating.Point;
import com.hsalf.smileyrating.SmileyRating;
import com.hsalf.smileyrating.smileys.base.Smiley;

public class Terrible extends Smiley {

public Terrible() {
super(-35, 280);
super(-35, 280, SmileyRating.Type.TERRIBLE);

float div = 0.20f;
FloatEvaluator f = new FloatEvaluator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import android.graphics.Path;
import android.graphics.RectF;

import androidx.annotation.NonNull;

import com.hsalf.smilerating.Point;
import com.hsalf.smileyrating.SmileyRating;

public abstract class Smiley {

Expand All @@ -29,6 +32,8 @@ public abstract class Smiley {

private Points mScaledPoints;

private final SmileyRating.Type mType;

private static final FloatEvaluator evaluator = new FloatEvaluator();

public String getName() {
Expand All @@ -43,6 +48,11 @@ public int getDrawingColor() {
return mDrawingColor;
}

@NonNull
public SmileyRating.Type getType() {
return mType;
}

public void setName(String name) {
mName = name;
}
Expand All @@ -61,9 +71,10 @@ protected void setup(String name, int faceColor, int drawingColor) {
mDrawingColor = drawingColor;
}

public Smiley(float eyeLeftStartAngle, float eyeLeftSweepAngle) {
public Smiley(float eyeLeftStartAngle, float eyeLeftSweepAngle, SmileyRating.Type type) {
mLeftEye = new Eye(Eye.Side.LEFT, eyeLeftStartAngle, eyeLeftSweepAngle);
mRightEye = new Eye(Eye.Side.RIGHT, eyeLeftStartAngle, eyeLeftSweepAngle);
mType = type == null ? SmileyRating.Type.NONE : type;
}

public void drawFace(Path path) {
Expand Down