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

Customizable Smiley: setMaxSmiley(from 1 to 5), setReverseSmiley(bool… #53

Open
wants to merge 1 commit 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,27 @@
package com.example.smilyrating;

import android.os.Bundle;

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 {

@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.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
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 @@ -552,6 +563,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 Down