├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── drawable
│ │ │ │ ├── fish.png
│ │ │ │ ├── fox.png
│ │ │ │ ├── icon.png
│ │ │ │ ├── lion.png
│ │ │ │ ├── test1.png
│ │ │ │ ├── test10.png
│ │ │ │ ├── test2.png
│ │ │ │ ├── test3.png
│ │ │ │ ├── test4.png
│ │ │ │ ├── test5.png
│ │ │ │ ├── test6.png
│ │ │ │ ├── test7.png
│ │ │ │ ├── test8.png
│ │ │ │ ├── test9.png
│ │ │ │ ├── wheel.png
│ │ │ │ ├── bulldog.png
│ │ │ │ ├── butterfly.png
│ │ │ │ ├── ic_cursor.png
│ │ │ │ └── ic_icon1.png
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── colors.xml
│ │ │ │ ├── dimens.xml
│ │ │ │ └── styles.xml
│ │ │ ├── drawable-xxhdpi
│ │ │ │ └── wheel2.png
│ │ │ ├── mipmap-hdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── values-w820dp
│ │ │ │ └── dimens.xml
│ │ │ └── layout
│ │ │ │ └── activity_main.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── ryan
│ │ │ └── luckywheel
│ │ │ └── MainActivity.java
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── ryan
│ │ │ └── luckywheel
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── ryan
│ │ └── luckywheel
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── library
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ └── attrs.xml
│ │ │ └── layout
│ │ │ │ └── lucky_wheel_layout.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── rubikstudio
│ │ │ └── library
│ │ │ ├── model
│ │ │ └── LuckyItem.java
│ │ │ ├── LuckyWheelUtils.java
│ │ │ ├── LuckyWheelView.java
│ │ │ └── PielView.java
│ ├── test
│ │ └── java
│ │ │ └── rubikstudio
│ │ │ └── library
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── rubikstudio
│ │ └── library
│ │ └── ExampleInstrumentedTest.java
├── build.gradle
└── proguard-rules.pro
├── settings.gradle
├── device-2016-11-05-214303.png
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── gradle.properties
├── README.md
├── gradlew.bat
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/library/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':library'
2 |
--------------------------------------------------------------------------------
/device-2016-11-05-214303.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thanhniencung/LuckyWheel/HEAD/device-2016-11-05-214303.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/fish.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thanhniencung/LuckyWheel/HEAD/app/src/main/res/drawable/fish.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/fox.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thanhniencung/LuckyWheel/HEAD/app/src/main/res/drawable/fox.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thanhniencung/LuckyWheel/HEAD/app/src/main/res/drawable/icon.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/lion.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thanhniencung/LuckyWheel/HEAD/app/src/main/res/drawable/lion.png
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | LuckyWheelView
3 |
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thanhniencung/LuckyWheel/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/drawable/test1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thanhniencung/LuckyWheel/HEAD/app/src/main/res/drawable/test1.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/test10.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thanhniencung/LuckyWheel/HEAD/app/src/main/res/drawable/test10.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/test2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thanhniencung/LuckyWheel/HEAD/app/src/main/res/drawable/test2.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/test3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thanhniencung/LuckyWheel/HEAD/app/src/main/res/drawable/test3.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/test4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thanhniencung/LuckyWheel/HEAD/app/src/main/res/drawable/test4.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/test5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thanhniencung/LuckyWheel/HEAD/app/src/main/res/drawable/test5.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/test6.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thanhniencung/LuckyWheel/HEAD/app/src/main/res/drawable/test6.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/test7.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thanhniencung/LuckyWheel/HEAD/app/src/main/res/drawable/test7.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/test8.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thanhniencung/LuckyWheel/HEAD/app/src/main/res/drawable/test8.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/test9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thanhniencung/LuckyWheel/HEAD/app/src/main/res/drawable/test9.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/wheel.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thanhniencung/LuckyWheel/HEAD/app/src/main/res/drawable/wheel.png
--------------------------------------------------------------------------------
/library/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Lucky Wheel Library
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/bulldog.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thanhniencung/LuckyWheel/HEAD/app/src/main/res/drawable/bulldog.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/butterfly.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thanhniencung/LuckyWheel/HEAD/app/src/main/res/drawable/butterfly.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_cursor.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thanhniencung/LuckyWheel/HEAD/app/src/main/res/drawable/ic_cursor.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_icon1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thanhniencung/LuckyWheel/HEAD/app/src/main/res/drawable/ic_icon1.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/wheel2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thanhniencung/LuckyWheel/HEAD/app/src/main/res/drawable-xxhdpi/wheel2.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thanhniencung/LuckyWheel/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thanhniencung/LuckyWheel/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thanhniencung/LuckyWheel/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thanhniencung/LuckyWheel/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thanhniencung/LuckyWheel/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | .DS_Store
5 | /build
6 | /captures
7 | .externalNativeBuild
8 | *.ipr
9 | *.iws
10 | .idea/
11 | .classpath
12 | .project
13 | .metadata
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Thu Jan 17 17:52:27 PHT 2019
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip
7 |
--------------------------------------------------------------------------------
/library/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/library/src/main/java/rubikstudio/library/model/LuckyItem.java:
--------------------------------------------------------------------------------
1 | package rubikstudio.library.model;
2 |
3 | /**
4 | * Created by kiennguyen on 11/5/16.
5 | */
6 |
7 | public class LuckyItem {
8 | public String topText;
9 | public String secondaryText;
10 | public int secondaryTextOrientation;
11 | public int icon;
12 | public int color;
13 | }
14 |
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/com/ryan/luckywheel/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.ryan.luckywheel;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/library/src/test/java/rubikstudio/library/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package rubikstudio.library;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/library/src/main/res/layout/lucky_wheel_layout.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
10 |
11 |
16 |
--------------------------------------------------------------------------------
/library/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 28
5 |
6 | defaultConfig {
7 | minSdkVersion 16
8 | targetSdkVersion 28
9 | versionCode 1
10 | versionName "1.0"
11 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
12 |
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | implementation 'androidx.core:core:1.1.0-alpha03'
24 | testImplementation 'junit:junit:4.12'
25 | }
26 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/kiennguyen/Library/Android/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/library/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/kiennguyen/Library/Android/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/library/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/ryan/luckywheel/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.ryan.luckywheel;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.ryan.luckywheel", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/library/src/androidTest/java/rubikstudio/library/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package rubikstudio.library;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("rubikstudio.library.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 28
5 | defaultConfig {
6 | applicationId "self.peterbetos.luckywheel"
7 | minSdkVersion 16
8 | targetSdkVersion 28
9 | versionCode 1
10 | versionName "1.0"
11 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | }
20 |
21 | dependencies {
22 | implementation fileTree(include: ['*.jar'], dir: 'libs')
23 | implementation 'androidx.appcompat:appcompat:1.1.0-alpha01'
24 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
25 | implementation project(path: ':library')
26 | testImplementation 'junit:junit:4.12'
27 | androidTestImplementation 'androidx.test:runner:1.1.1'
28 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
13 |
14 |
23 |
24 |
35 |
36 |
--------------------------------------------------------------------------------
/library/src/main/java/rubikstudio/library/LuckyWheelUtils.java:
--------------------------------------------------------------------------------
1 | package rubikstudio.library;
2 |
3 | import android.content.Context;
4 | import android.graphics.Bitmap;
5 | import android.graphics.Canvas;
6 | import android.graphics.drawable.BitmapDrawable;
7 | import android.graphics.drawable.Drawable;
8 | import android.util.TypedValue;
9 |
10 | /**
11 | * Created by kiennguyen on 11/5/16.
12 | */
13 |
14 | public class LuckyWheelUtils {
15 | public static Bitmap drawableToBitmap(Drawable drawable) {
16 | Bitmap bitmap;
17 |
18 | if (drawable instanceof BitmapDrawable) {
19 | BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
20 | if (bitmapDrawable.getBitmap() != null) {
21 | return bitmapDrawable.getBitmap();
22 | }
23 | }
24 |
25 | if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
26 | bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
27 | } else {
28 | bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
29 | }
30 |
31 | Canvas canvas = new Canvas(bitmap);
32 | drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
33 | drawable.draw(canvas);
34 | return bitmap;
35 | }
36 |
37 |
38 | public static float convertDpToPixel(float dp, Context context) {
39 | return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
40 | context.getResources().getDisplayMetrics());
41 | }
42 |
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Screenshot
2 |
3 | 
4 |
5 | # Usage
6 |
7 | Include LuckyWheel widget in your layout:
8 |
9 | ```xml
10 |
19 | ```
20 |
21 | You also have the option to add the following parameters:
22 |
23 | ```xml
24 | ...
25 | app:lkwEdgeColor="#e2e5eb"
26 | app:lkwEdgeWidth="25"
27 | app:lkwTopTextPadding="10dp"
28 | app:lkwTopTextSize="12sp"
29 | app:lkwSecondaryTextSize="25sp" />
30 | ```
31 |
32 | And in your code :
33 |
34 | ```java
35 | LuckyWheelView luckyWheelView = (LuckyWheelView) findViewById(R.id.luckyWheel);
36 | List data = new ArrayList<>();
37 | for (int i=0; i<12; i++) {
38 | LuckyItem luckyItem = new LuckyItem();
39 | luckyItem1.topText = "Item " + i;
40 | luckyItem1.icon = R.drawable.test;
41 | luckyItem1.color = 0xffFFF3E0;
42 | data.add(luckyItem);
43 | }
44 |
45 | luckyWheelView.setData(data);
46 | luckyWheelView.setRound(10);
47 |
48 | // start
49 | luckyWheelView.startLuckyWheelWithTargetIndex(index);
50 |
51 | // listener after finish lucky wheel
52 | luckyWheelView.setLuckyRoundItemSelectedListener(new LuckyWheelView.LuckyRoundItemSelectedListener() {
53 | @Override
54 | public void LuckyRoundItemSelected(int index) {
55 | // do something with index
56 | }
57 | });
58 | ```
59 |
60 |
61 | #Custom LuckyWheel
62 |
63 | You can setting these properties of the lucky wheel view with xml :
64 | ```xml
65 | app:lkwBackgroundColor="#FF9800"
66 | app:lkwTopTextColor="#263238"
67 | app:lkwCenterImage="@drawable/wheel2"
68 | app:lkwCursor="@drawable/ic_cursor"
69 | ```
70 |
71 | Or with code like:
72 | ```java
73 | luckyWheelView.setLuckyWheelBackgrouldColor(0xff0000ff);
74 | luckyWheelView.setLuckyWheelTextColor(0xffcc0000);
75 | luckyWheelView.setLuckyWheelCenterImage(getResources().getDrawable(R.drawable.icon));
76 | luckyWheelView.setLuckyWheelCursorImage(R.drawable.ic_cursor);
77 | ```
78 |
79 | #Sample
80 | Check out the [sample application](https://github.com/thanhniencung/LuckyWheel/blob/master/app/src/main/java/com/ryan/luckywheel/MainActivity.java) to see it in action!
81 |
82 | # Contributing
83 | Contributions welcome via Github pull requests.
84 |
85 |
86 |
87 |
88 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/app/src/main/java/com/ryan/luckywheel/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.ryan.luckywheel;
2 |
3 | import android.app.Activity;
4 | import android.os.Bundle;
5 | import android.view.View;
6 | import android.widget.Toast;
7 |
8 | import java.util.ArrayList;
9 | import java.util.List;
10 | import java.util.Random;
11 |
12 | import rubikstudio.library.LuckyWheelView;
13 | import rubikstudio.library.model.LuckyItem;
14 | import rubikstudio.library.PielView;
15 |
16 | public class MainActivity extends Activity {
17 | List data = new ArrayList<>();
18 |
19 | @Override
20 | protected void onCreate(Bundle savedInstanceState) {
21 | super.onCreate(savedInstanceState);
22 |
23 | setContentView(R.layout.activity_main);
24 |
25 | final LuckyWheelView luckyWheelView = (LuckyWheelView) findViewById(R.id.luckyWheel);
26 |
27 | LuckyItem luckyItem1 = new LuckyItem();
28 | luckyItem1.topText = "100";
29 | luckyItem1.icon = R.drawable.test1;
30 | luckyItem1.color = 0xffFFF3E0;
31 | data.add(luckyItem1);
32 |
33 | LuckyItem luckyItem2 = new LuckyItem();
34 | luckyItem2.topText = "200";
35 | luckyItem2.icon = R.drawable.test2;
36 | luckyItem2.color = 0xffFFE0B2;
37 | data.add(luckyItem2);
38 |
39 | LuckyItem luckyItem3 = new LuckyItem();
40 | luckyItem3.topText = "300";
41 | luckyItem3.icon = R.drawable.test3;
42 | luckyItem3.color = 0xffFFCC80;
43 | data.add(luckyItem3);
44 |
45 | //////////////////
46 | LuckyItem luckyItem4 = new LuckyItem();
47 | luckyItem4.topText = "400";
48 | luckyItem4.icon = R.drawable.test4;
49 | luckyItem4.color = 0xffFFF3E0;
50 | data.add(luckyItem4);
51 |
52 | LuckyItem luckyItem5 = new LuckyItem();
53 | luckyItem5.topText = "500";
54 | luckyItem5.icon = R.drawable.test5;
55 | luckyItem5.color = 0xffFFE0B2;
56 | data.add(luckyItem5);
57 |
58 | LuckyItem luckyItem6 = new LuckyItem();
59 | luckyItem6.topText = "600";
60 | luckyItem6.icon = R.drawable.test6;
61 | luckyItem6.color = 0xffFFCC80;
62 | data.add(luckyItem6);
63 | //////////////////
64 |
65 | //////////////////////
66 | LuckyItem luckyItem7 = new LuckyItem();
67 | luckyItem7.topText = "700";
68 | luckyItem7.icon = R.drawable.test7;
69 | luckyItem7.color = 0xffFFF3E0;
70 | data.add(luckyItem7);
71 |
72 | LuckyItem luckyItem8 = new LuckyItem();
73 | luckyItem8.topText = "800";
74 | luckyItem8.icon = R.drawable.test8;
75 | luckyItem8.color = 0xffFFE0B2;
76 | data.add(luckyItem8);
77 |
78 |
79 | LuckyItem luckyItem9 = new LuckyItem();
80 | luckyItem9.topText = "900";
81 | luckyItem9.icon = R.drawable.test9;
82 | luckyItem9.color = 0xffFFCC80;
83 | data.add(luckyItem9);
84 | ////////////////////////
85 |
86 | LuckyItem luckyItem10 = new LuckyItem();
87 | luckyItem10.topText = "1000";
88 | luckyItem10.icon = R.drawable.test10;
89 | luckyItem10.color = 0xffFFE0B2;
90 | data.add(luckyItem10);
91 |
92 | LuckyItem luckyItem11 = new LuckyItem();
93 | luckyItem11.topText = "2000";
94 | luckyItem11.icon = R.drawable.test10;
95 | luckyItem11.color = 0xffFFE0B2;
96 | data.add(luckyItem11);
97 |
98 | LuckyItem luckyItem12 = new LuckyItem();
99 | luckyItem12.topText = "3000";
100 | luckyItem12.icon = R.drawable.test10;
101 | luckyItem12.color = 0xffFFE0B2;
102 | data.add(luckyItem12);
103 |
104 | /////////////////////
105 |
106 | luckyWheelView.setData(data);
107 | luckyWheelView.setRound(5);
108 |
109 | /*luckyWheelView.setLuckyWheelBackgrouldColor(0xff0000ff);
110 | luckyWheelView.setLuckyWheelTextColor(0xffcc0000);
111 | luckyWheelView.setLuckyWheelCenterImage(getResources().getDrawable(R.drawable.icon));
112 | luckyWheelView.setLuckyWheelCursorImage(R.drawable.ic_cursor);*/
113 |
114 |
115 | findViewById(R.id.play).setOnClickListener(new View.OnClickListener() {
116 | @Override
117 | public void onClick(View view) {
118 | int index = getRandomIndex();
119 | luckyWheelView.startLuckyWheelWithTargetIndex(index);
120 | }
121 | });
122 |
123 | luckyWheelView.setLuckyRoundItemSelectedListener(new LuckyWheelView.LuckyRoundItemSelectedListener() {
124 | @Override
125 | public void LuckyRoundItemSelected(int index) {
126 | Toast.makeText(getApplicationContext(), data.get(index).topText, Toast.LENGTH_SHORT).show();
127 | }
128 | });
129 | }
130 |
131 | private int getRandomIndex() {
132 | Random rand = new Random();
133 | return rand.nextInt(data.size() - 1) + 0;
134 | }
135 |
136 | private int getRandomRound() {
137 | Random rand = new Random();
138 | return rand.nextInt(10) + 15;
139 | }
140 | }
141 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/library/src/main/java/rubikstudio/library/LuckyWheelView.java:
--------------------------------------------------------------------------------
1 | package rubikstudio.library;
2 |
3 | import android.content.Context;
4 | import android.content.res.TypedArray;
5 | import android.graphics.drawable.Drawable;
6 | import android.util.AttributeSet;
7 | import android.view.LayoutInflater;
8 | import android.view.MotionEvent;
9 | import android.view.View;
10 | import android.view.ViewGroup;
11 | import android.widget.FrameLayout;
12 | import android.widget.ImageView;
13 | import android.widget.RelativeLayout;
14 |
15 | import java.util.List;
16 |
17 | import rubikstudio.library.model.LuckyItem;
18 |
19 | /**
20 | * Created by kiennguyen on 11/5/16.
21 | */
22 |
23 | public class LuckyWheelView extends RelativeLayout implements PielView.PieRotateListener {
24 | private int mBackgroundColor;
25 | private int mTextColor;
26 | private int mTopTextSize;
27 | private int mSecondaryTextSize;
28 | private int mBorderColor;
29 | private int mTopTextPadding;
30 | private int mEdgeWidth;
31 | private Drawable mCenterImage;
32 | private Drawable mCursorImage;
33 |
34 | private PielView pielView;
35 | private ImageView ivCursorView;
36 |
37 | private LuckyRoundItemSelectedListener mLuckyRoundItemSelectedListener;
38 |
39 | @Override
40 | public void rotateDone(int index) {
41 | if (mLuckyRoundItemSelectedListener != null) {
42 | mLuckyRoundItemSelectedListener.LuckyRoundItemSelected(index);
43 | }
44 | }
45 |
46 | public interface LuckyRoundItemSelectedListener {
47 | void LuckyRoundItemSelected(int index);
48 | }
49 |
50 | public void setLuckyRoundItemSelectedListener(LuckyRoundItemSelectedListener listener) {
51 | this.mLuckyRoundItemSelectedListener = listener;
52 | }
53 |
54 | public LuckyWheelView(Context context) {
55 | super(context);
56 | init(context, null);
57 | }
58 |
59 | public LuckyWheelView(Context context, AttributeSet attrs) {
60 | super(context, attrs);
61 | init(context, attrs);
62 | }
63 |
64 | /**
65 | * @param ctx
66 | * @param attrs
67 | */
68 | private void init(Context ctx, AttributeSet attrs) {
69 | if (attrs != null) {
70 | TypedArray typedArray = ctx.obtainStyledAttributes(attrs, R.styleable.LuckyWheelView);
71 | mBackgroundColor = typedArray.getColor(R.styleable.LuckyWheelView_lkwBackgroundColor, 0xffcc0000);
72 | mTopTextSize = typedArray.getDimensionPixelSize(R.styleable.LuckyWheelView_lkwTopTextSize, (int) LuckyWheelUtils.convertDpToPixel(10f, getContext()));
73 | mSecondaryTextSize = typedArray.getDimensionPixelSize(R.styleable.LuckyWheelView_lkwSecondaryTextSize, (int) LuckyWheelUtils.convertDpToPixel(20f, getContext()));
74 | mTextColor = typedArray.getColor(R.styleable.LuckyWheelView_lkwTopTextColor, 0);
75 | mTopTextPadding = typedArray.getDimensionPixelSize(R.styleable.LuckyWheelView_lkwTopTextPadding, (int) LuckyWheelUtils.convertDpToPixel(10f, getContext())) + (int) LuckyWheelUtils.convertDpToPixel(10f, getContext());
76 | mCursorImage = typedArray.getDrawable(R.styleable.LuckyWheelView_lkwCursor);
77 | mCenterImage = typedArray.getDrawable(R.styleable.LuckyWheelView_lkwCenterImage);
78 | mEdgeWidth = typedArray.getInt(R.styleable.LuckyWheelView_lkwEdgeWidth, 10);
79 | mBorderColor = typedArray.getColor(R.styleable.LuckyWheelView_lkwEdgeColor, 0);
80 | typedArray.recycle();
81 | }
82 |
83 | LayoutInflater inflater = LayoutInflater.from(getContext());
84 | FrameLayout frameLayout = (FrameLayout) inflater.inflate(R.layout.lucky_wheel_layout, this, false);
85 |
86 | pielView = frameLayout.findViewById(R.id.pieView);
87 | ivCursorView = frameLayout.findViewById(R.id.cursorView);
88 |
89 | pielView.setPieRotateListener(this);
90 | pielView.setPieBackgroundColor(mBackgroundColor);
91 | pielView.setTopTextPadding(mTopTextPadding);
92 | pielView.setTopTextSize(mTopTextSize);
93 | pielView.setSecondaryTextSizeSize(mSecondaryTextSize);
94 | pielView.setPieCenterImage(mCenterImage);
95 | pielView.setBorderColor(mBorderColor);
96 | pielView.setBorderWidth(mEdgeWidth);
97 |
98 |
99 | if (mTextColor != 0)
100 | pielView.setPieTextColor(mTextColor);
101 |
102 | ivCursorView.setImageDrawable(mCursorImage);
103 |
104 | addView(frameLayout);
105 | }
106 |
107 |
108 | public boolean isTouchEnabled() {
109 | return pielView.isTouchEnabled();
110 | }
111 |
112 | public void setTouchEnabled(boolean touchEnabled) {
113 | pielView.setTouchEnabled(touchEnabled);
114 | }
115 |
116 |
117 | @Override
118 | public boolean dispatchTouchEvent(MotionEvent ev) {
119 | //This is to control that the touch events triggered are only going to the PieView
120 | for (int i = 0; i < getChildCount(); i++) {
121 | if (isPielView(getChildAt(i))) {
122 | return super.dispatchTouchEvent(ev);
123 | }
124 | }
125 | return false;
126 | }
127 |
128 | private boolean isPielView(View view) {
129 | if (view instanceof ViewGroup) {
130 | for (int i = 0; i < getChildCount(); i++) {
131 | if (isPielView(((ViewGroup) view).getChildAt(i))) {
132 | return true;
133 | }
134 | }
135 | }
136 | return view instanceof PielView;
137 | }
138 |
139 | public void setLuckyWheelBackgrouldColor(int color) {
140 | pielView.setPieBackgroundColor(color);
141 | }
142 |
143 | public void setLuckyWheelCursorImage(int drawable) {
144 | ivCursorView.setBackgroundResource(drawable);
145 | }
146 |
147 | public void setLuckyWheelCenterImage(Drawable drawable) {
148 | pielView.setPieCenterImage(drawable);
149 | }
150 |
151 | public void setBorderColor(int color) {
152 | pielView.setBorderColor(color);
153 | }
154 |
155 | public void setLuckyWheelTextColor(int color) {
156 | pielView.setPieTextColor(color);
157 | }
158 |
159 | /**
160 | * @param data
161 | */
162 | public void setData(List data) {
163 | pielView.setData(data);
164 | }
165 |
166 | /**
167 | * @param numberOfRound
168 | */
169 | public void setRound(int numberOfRound) {
170 | pielView.setRound(numberOfRound);
171 | }
172 |
173 | /**
174 | * @param fixedNumber
175 | */
176 | public void setPredeterminedNumber(int fixedNumber) {
177 | pielView.setPredeterminedNumber(fixedNumber);
178 | }
179 |
180 | public void startLuckyWheelWithTargetIndex(int index) {
181 | pielView.rotateTo(index);
182 | }
183 |
184 | public void startLuckyWheelWithRandomTarget() {
185 | Random r = new Random();
186 | pielView.rotateTo(r.nextInt(pielView.getLuckyItemListSize() - 1));
187 | }
188 | }
189 |
--------------------------------------------------------------------------------
/library/src/main/java/rubikstudio/library/PielView.java:
--------------------------------------------------------------------------------
1 | package rubikstudio.library;
2 |
3 | import android.animation.Animator;
4 | import android.animation.TimeInterpolator;
5 | import android.annotation.TargetApi;
6 | import android.content.Context;
7 | import android.graphics.Bitmap;
8 | import android.graphics.BitmapFactory;
9 | import android.graphics.Canvas;
10 | import android.graphics.Paint;
11 | import android.graphics.Path;
12 | import android.graphics.Rect;
13 | import android.graphics.RectF;
14 | import android.graphics.Typeface;
15 | import android.graphics.drawable.Drawable;
16 | import android.os.Build;
17 | import android.text.TextPaint;
18 | import android.text.TextUtils;
19 | import android.util.AttributeSet;
20 | import android.util.TypedValue;
21 | import android.view.MotionEvent;
22 | import android.view.View;
23 | import android.view.animation.AccelerateInterpolator;
24 | import android.view.animation.DecelerateInterpolator;
25 | import android.view.animation.LinearInterpolator;
26 |
27 | import java.util.List;
28 | import java.util.Random;
29 |
30 | import androidx.annotation.IntDef;
31 | import androidx.core.graphics.ColorUtils;
32 | import rubikstudio.library.model.LuckyItem;
33 |
34 | /**
35 | * Created by kiennguyen on 11/5/16.
36 | */
37 |
38 | public class PielView extends View {
39 | private RectF mRange = new RectF();
40 | private int mRadius;
41 |
42 | private Paint mArcPaint;
43 | private Paint mBackgroundPaint;
44 | private TextPaint mTextPaint;
45 |
46 | private float mStartAngle = 0;
47 | private int mCenter;
48 | private int mPadding;
49 | private int mTopTextPadding;
50 | private int mTopTextSize;
51 | private int mSecondaryTextSize;
52 | private int mRoundOfNumber = 4;
53 | private int mEdgeWidth = -1;
54 | private boolean isRunning = false;
55 |
56 | private int borderColor = 0;
57 | private int defaultBackgroundColor = 0;
58 | private Drawable drawableCenterImage;
59 | private int textColor = 0;
60 |
61 | private int predeterminedNumber = -1;
62 |
63 | float viewRotation;
64 | double fingerRotation;
65 | long downPressTime, upPressTime;
66 | double newRotationStore[] = new double[3];
67 |
68 |
69 | private List mLuckyItemList;
70 |
71 | private PieRotateListener mPieRotateListener;
72 |
73 | public interface PieRotateListener {
74 | void rotateDone(int index);
75 | }
76 |
77 | public PielView(Context context) {
78 | super(context);
79 | }
80 |
81 | public PielView(Context context, AttributeSet attrs) {
82 | super(context, attrs);
83 | }
84 |
85 | public void setPieRotateListener(PieRotateListener listener) {
86 | this.mPieRotateListener = listener;
87 | }
88 |
89 | private void init() {
90 | mArcPaint = new Paint();
91 | mArcPaint.setAntiAlias(true);
92 | mArcPaint.setDither(true);
93 |
94 | mTextPaint = new TextPaint();
95 | mTextPaint.setAntiAlias(true);
96 |
97 |
98 | if (textColor != 0) mTextPaint.setColor(textColor);
99 | mTextPaint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 14,
100 | getResources().getDisplayMetrics()));
101 |
102 | mRange = new RectF(mPadding, mPadding, mPadding + mRadius, mPadding + mRadius);
103 | }
104 |
105 | public int getLuckyItemListSize() {
106 | return mLuckyItemList.size();
107 | }
108 |
109 | public void setData(List luckyItemList) {
110 | this.mLuckyItemList = luckyItemList;
111 | invalidate();
112 | }
113 |
114 | public void setPieBackgroundColor(int color) {
115 | defaultBackgroundColor = color;
116 | invalidate();
117 | }
118 |
119 | public void setBorderColor(int color) {
120 | borderColor = color;
121 | invalidate();
122 | }
123 |
124 | public void setTopTextPadding(int padding) {
125 | mTopTextPadding = padding;
126 | invalidate();
127 | }
128 |
129 |
130 | public void setPieCenterImage(Drawable drawable) {
131 | drawableCenterImage = drawable;
132 | invalidate();
133 | }
134 |
135 | public void setTopTextSize(int size) {
136 | mTopTextSize = size;
137 | invalidate();
138 | }
139 |
140 | public void setSecondaryTextSizeSize(int size) {
141 | mSecondaryTextSize = size;
142 | invalidate();
143 | }
144 |
145 |
146 | public void setBorderWidth(int width) {
147 | mEdgeWidth = width;
148 | invalidate();
149 | }
150 |
151 | public void setPieTextColor(int color) {
152 | textColor = color;
153 | invalidate();
154 | }
155 |
156 | private void drawPieBackgroundWithBitmap(Canvas canvas, Bitmap bitmap) {
157 | canvas.drawBitmap(bitmap, null, new Rect(mPadding / 2, mPadding / 2,
158 | getMeasuredWidth() - mPadding / 2,
159 | getMeasuredHeight() - mPadding / 2), null);
160 | }
161 |
162 | /**
163 | * @param canvas
164 | */
165 | @Override
166 | protected void onDraw(Canvas canvas) {
167 | super.onDraw(canvas);
168 |
169 | if (mLuckyItemList == null) {
170 | return;
171 | }
172 |
173 | drawBackgroundColor(canvas, defaultBackgroundColor);
174 |
175 | init();
176 |
177 | float tmpAngle = mStartAngle;
178 | float sweepAngle = 360f / mLuckyItemList.size();
179 |
180 | for (int i = 0; i < mLuckyItemList.size(); i++) {
181 |
182 | if (mLuckyItemList.get(i).color != 0) {
183 | mArcPaint.setStyle(Paint.Style.FILL);
184 | mArcPaint.setColor(mLuckyItemList.get(i).color);
185 | canvas.drawArc(mRange, tmpAngle, sweepAngle, true, mArcPaint);
186 | }
187 |
188 | if (borderColor != 0 && mEdgeWidth > 0) {
189 | mArcPaint.setStyle(Paint.Style.STROKE);
190 | mArcPaint.setColor(borderColor);
191 | mArcPaint.setStrokeWidth(mEdgeWidth);
192 | canvas.drawArc(mRange, tmpAngle, sweepAngle, true, mArcPaint);
193 | }
194 |
195 | int sliceColor = mLuckyItemList.get(i).color != 0 ? mLuckyItemList.get(i).color : defaultBackgroundColor;
196 |
197 | if (!TextUtils.isEmpty(mLuckyItemList.get(i).topText))
198 | drawTopText(canvas, tmpAngle, sweepAngle, mLuckyItemList.get(i).topText, sliceColor);
199 | if (!TextUtils.isEmpty(mLuckyItemList.get(i).secondaryText))
200 | drawSecondaryText(canvas, tmpAngle, mLuckyItemList.get(i).secondaryText, sliceColor);
201 |
202 | if (mLuckyItemList.get(i).icon != 0)
203 | drawImage(canvas, tmpAngle, BitmapFactory.decodeResource(getResources(),
204 | mLuckyItemList.get(i).icon));
205 | tmpAngle += sweepAngle;
206 | }
207 |
208 | drawCenterImage(canvas, drawableCenterImage);
209 | }
210 |
211 | private void drawBackgroundColor(Canvas canvas, int color) {
212 | if (color == 0)
213 | return;
214 | mBackgroundPaint = new Paint();
215 | mBackgroundPaint.setColor(color);
216 | canvas.drawCircle(mCenter, mCenter, mCenter - 5, mBackgroundPaint);
217 | }
218 |
219 | /**
220 | * @param widthMeasureSpec
221 | * @param heightMeasureSpec
222 | */
223 | @Override
224 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
225 | super.onMeasure(widthMeasureSpec, heightMeasureSpec);
226 |
227 | int width = Math.min(getMeasuredWidth(), getMeasuredHeight());
228 |
229 | mPadding = getPaddingLeft() == 0 ? 10 : getPaddingLeft();
230 | mRadius = width - mPadding * 2;
231 |
232 | mCenter = width / 2;
233 |
234 | setMeasuredDimension(width, width);
235 | }
236 |
237 | /**
238 | * @param canvas
239 | * @param tmpAngle
240 | * @param bitmap
241 | */
242 | private void drawImage(Canvas canvas, float tmpAngle, Bitmap bitmap) {
243 | int imgWidth = mRadius / mLuckyItemList.size();
244 |
245 | float angle = (float) ((tmpAngle + 360f / mLuckyItemList.size() / 2) * Math.PI / 180);
246 |
247 | int x = (int) (mCenter + mRadius / 2 / 2 * Math.cos(angle));
248 | int y = (int) (mCenter + mRadius / 2 / 2 * Math.sin(angle));
249 |
250 | Rect rect = new Rect(x - imgWidth / 2, y - imgWidth / 2,
251 | x + imgWidth / 2, y + imgWidth / 2);
252 | canvas.drawBitmap(bitmap, null, rect, null);
253 | }
254 |
255 | private void drawCenterImage(Canvas canvas, Drawable drawable) {
256 | ;
257 | Bitmap bitmap = LuckyWheelUtils.drawableToBitmap(drawable);
258 | bitmap = Bitmap.createScaledBitmap(bitmap, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), false);
259 | canvas.drawBitmap(bitmap, getMeasuredWidth() / 2 - bitmap.getWidth() / 2,
260 | getMeasuredHeight() / 2 - bitmap.getHeight() / 2, null);
261 | }
262 |
263 | private boolean isColorDark(int color) {
264 | double colorValue = ColorUtils.calculateLuminance(color);
265 | double compareValue = 0.30d;
266 | return colorValue <= compareValue;
267 | }
268 |
269 |
270 | /**
271 | * @param canvas
272 | * @param tmpAngle
273 | * @param sweepAngle
274 | * @param mStr
275 | */
276 | private void drawTopText(Canvas canvas, float tmpAngle, float sweepAngle, String mStr, int backgroundColor) {
277 | Path path = new Path();
278 | path.addArc(mRange, tmpAngle, sweepAngle);
279 |
280 | if (textColor == 0)
281 | mTextPaint.setColor(isColorDark(backgroundColor) ? 0xffffffff : 0xff000000);
282 |
283 | Typeface typeface = Typeface.create(Typeface.SANS_SERIF, Typeface.NORMAL);
284 | mTextPaint.setTypeface(typeface);
285 | mTextPaint.setTextAlign(Paint.Align.LEFT);
286 | mTextPaint.setTextSize(mTopTextSize);
287 | float textWidth = mTextPaint.measureText(mStr);
288 | int hOffset = (int) (mRadius * Math.PI / mLuckyItemList.size() / 2 - textWidth / 2);
289 |
290 | int vOffset = mTopTextPadding;
291 |
292 | canvas.drawTextOnPath(mStr, path, hOffset, vOffset, mTextPaint);
293 | }
294 |
295 |
296 | /**
297 | * @param canvas
298 | * @param tmpAngle
299 | * @param mStr
300 | * @param backgroundColor
301 | */
302 | private void drawSecondaryText(Canvas canvas, float tmpAngle, String mStr, int backgroundColor) {
303 | canvas.save();
304 | int arraySize = mLuckyItemList.size();
305 |
306 | if (textColor == 0)
307 | mTextPaint.setColor(isColorDark(backgroundColor) ? 0xffffffff : 0xff000000);
308 |
309 | Typeface typeface = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
310 | mTextPaint.setTypeface(typeface);
311 | mTextPaint.setTextSize(mSecondaryTextSize);
312 | mTextPaint.setTextAlign(Paint.Align.LEFT);
313 |
314 | float textWidth = mTextPaint.measureText(mStr);
315 |
316 | float initFloat = (tmpAngle + 360f / arraySize / 2);
317 | float angle = (float) (initFloat * Math.PI / 180);
318 |
319 | int x = (int) (mCenter + mRadius / 2 / 2 * Math.cos(angle));
320 | int y = (int) (mCenter + mRadius / 2 / 2 * Math.sin(angle));
321 |
322 | RectF rect = new RectF(x + textWidth, y,
323 | x - textWidth, y);
324 |
325 | Path path = new Path();
326 | path.addRect(rect, Path.Direction.CW);
327 | path.close();
328 | canvas.rotate(initFloat + (arraySize / 18f), x, y);
329 | canvas.drawTextOnPath(mStr, path, mTopTextPadding / 7f, mTextPaint.getTextSize() / 2.75f, mTextPaint);
330 | canvas.restore();
331 | }
332 |
333 | /**
334 | * @return
335 | */
336 | private float getAngleOfIndexTarget(int index) {
337 | return (360f / mLuckyItemList.size()) * index;
338 | }
339 |
340 | /**
341 | * @param numberOfRound
342 | */
343 | public void setRound(int numberOfRound) {
344 | mRoundOfNumber = numberOfRound;
345 | }
346 |
347 |
348 | public void setPredeterminedNumber(int predeterminedNumber) {
349 | this.predeterminedNumber = predeterminedNumber;
350 | }
351 |
352 | public void rotateTo(final int index) {
353 | Random rand = new Random();
354 | rotateTo(index, (rand.nextInt() * 3) % 2, true);
355 | }
356 |
357 | /**
358 | * @param index
359 | * @param rotation, spin orientation of the wheel if clockwise or counterclockwise
360 | * @param startSlow, either animates a slow start or an immediate turn based on the trigger
361 | */
362 | @TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
363 | public void rotateTo(final int index, @SpinRotation final int rotation, boolean startSlow) {
364 | if (isRunning) {
365 | return;
366 | }
367 |
368 | int rotationAssess = rotation <= 0 ? 1 : -1;
369 |
370 | //If the staring position is already off 0 degrees, make an illusion that the rotation has smoothly been triggered.
371 | // But this inital animation will just reset the position of the circle to 0 degreees.
372 | if (getRotation() != 0.0f) {
373 | setRotation(getRotation() % 360f);
374 | TimeInterpolator animationStart = startSlow ? new AccelerateInterpolator() : new LinearInterpolator();
375 | //The multiplier is to do a big rotation again if the position is already near 360.
376 | float multiplier = getRotation() > 200f ? 2 : 1;
377 | animate()
378 | .setInterpolator(animationStart)
379 | .setDuration(500L)
380 | .setListener(new Animator.AnimatorListener() {
381 | @Override
382 | public void onAnimationStart(Animator animation) {
383 | isRunning = true;
384 | }
385 |
386 | @Override
387 | public void onAnimationEnd(Animator animation) {
388 | isRunning = false;
389 | setRotation(0);
390 | rotateTo(index, rotation, false);
391 | }
392 |
393 | @Override
394 | public void onAnimationCancel(Animator animation) {
395 | }
396 |
397 | @Override
398 | public void onAnimationRepeat(Animator animation) {
399 | }
400 | })
401 | .rotation(360f * multiplier * rotationAssess)
402 | .start();
403 | return;
404 | }
405 |
406 | // This addition of another round count for counterclockwise is to simulate the perception of the same number of spin
407 | // if you still need to reach the same outcome of a positive degrees rotation with the number of rounds reversed.
408 | if (rotationAssess < 0) mRoundOfNumber++;
409 |
410 | float targetAngle = ((360f * mRoundOfNumber * rotationAssess) + 270f - getAngleOfIndexTarget(index) - (360f / mLuckyItemList.size()) / 2);
411 | animate()
412 | .setInterpolator(new DecelerateInterpolator())
413 | .setDuration(mRoundOfNumber * 1000 + 900L)
414 | .setListener(new Animator.AnimatorListener() {
415 | @Override
416 | public void onAnimationStart(Animator animation) {
417 | isRunning = true;
418 | }
419 |
420 | @Override
421 | public void onAnimationEnd(Animator animation) {
422 | isRunning = false;
423 | setRotation(getRotation() % 360f);
424 | if (mPieRotateListener != null) {
425 | mPieRotateListener.rotateDone(index);
426 | }
427 | }
428 |
429 | @Override
430 | public void onAnimationCancel(Animator animation) {
431 | }
432 |
433 | @Override
434 | public void onAnimationRepeat(Animator animation) {
435 | }
436 | })
437 | .rotation(targetAngle)
438 | .start();
439 | }
440 |
441 | public boolean touchEnabled = true;
442 |
443 | public boolean isTouchEnabled() {
444 | return touchEnabled;
445 | }
446 |
447 | public void setTouchEnabled(boolean touchEnabled) {
448 | this.touchEnabled = touchEnabled;
449 | }
450 |
451 | @Override
452 | public boolean onTouchEvent(MotionEvent event) {
453 | if (isRunning || !touchEnabled) {
454 | return false;
455 | }
456 |
457 | final float x = event.getX();
458 | final float y = event.getY();
459 |
460 | final float xc = getWidth() / 2.0f;
461 | final float yc = getHeight() / 2.0f;
462 |
463 | double newFingerRotation;
464 |
465 |
466 | switch (event.getAction()) {
467 | case MotionEvent.ACTION_DOWN:
468 | viewRotation = (getRotation() + 360f) % 360f;
469 | fingerRotation = Math.toDegrees(Math.atan2(x - xc, yc - y));
470 | downPressTime = event.getEventTime();
471 | return true;
472 | case MotionEvent.ACTION_MOVE:
473 | newFingerRotation = Math.toDegrees(Math.atan2(x - xc, yc - y));
474 |
475 | if (isRotationConsistent(newFingerRotation)) {
476 | setRotation(newRotationValue(viewRotation, fingerRotation, newFingerRotation));
477 | }
478 | return true;
479 | case MotionEvent.ACTION_UP:
480 | newFingerRotation = Math.toDegrees(Math.atan2(x - xc, yc - y));
481 | float computedRotation = newRotationValue(viewRotation, fingerRotation, newFingerRotation);
482 |
483 | fingerRotation = newFingerRotation;
484 |
485 | // This computes if you're holding the tap for too long
486 | upPressTime = event.getEventTime();
487 | if (upPressTime - downPressTime > 700L) {
488 | // Disregarding the touch since the tap is too slow
489 | return true;
490 | }
491 |
492 | // These operators are added so that fling difference can be evaluated
493 | // with usually numbers that are only around more or less 100 / -100.
494 | if (computedRotation <= -250f) {
495 | computedRotation += 360f;
496 | } else if (computedRotation >= 250f) {
497 | computedRotation -= 360f;
498 | }
499 |
500 | double flingDiff = computedRotation - viewRotation;
501 | if (flingDiff >= 200 || flingDiff <= -200) {
502 | if (viewRotation <= -50f) {
503 | viewRotation += 360f;
504 | } else if (viewRotation >= 50f) {
505 | viewRotation -= 360f;
506 | }
507 | }
508 |
509 | flingDiff = computedRotation - viewRotation;
510 |
511 | if (flingDiff <= -60 ||
512 | //If you have a very fast flick / swipe, you an disregard the touch difference
513 | (flingDiff < 0 && flingDiff >= -59 && upPressTime - downPressTime <= 200L)) {
514 | if (predeterminedNumber > -1) {
515 | rotateTo(predeterminedNumber, SpinRotation.COUNTERCLOCKWISE, false);
516 | } else {
517 | rotateTo(getFallBackRandomIndex(), SpinRotation.COUNTERCLOCKWISE, false);
518 | }
519 | }
520 |
521 | if (flingDiff >= 60 ||
522 | //If you have a very fast flick / swipe, you an disregard the touch difference
523 | (flingDiff > 0 && flingDiff <= 59 && upPressTime - downPressTime <= 200L)) {
524 | if (predeterminedNumber > -1) {
525 | rotateTo(predeterminedNumber, SpinRotation.CLOCKWISE, false);
526 | } else {
527 | rotateTo(getFallBackRandomIndex(), SpinRotation.CLOCKWISE, false);
528 | }
529 | }
530 |
531 | return true;
532 | }
533 | return super.onTouchEvent(event);
534 | }
535 |
536 | private float newRotationValue(final float originalWheenRotation, final double originalFingerRotation, final double newFingerRotation) {
537 | double computationalRotation = newFingerRotation - originalFingerRotation;
538 | return (originalWheenRotation + (float) computationalRotation + 360f) % 360f;
539 | }
540 |
541 | private int getFallBackRandomIndex() {
542 | Random rand = new Random();
543 | return rand.nextInt(mLuckyItemList.size() - 1) + 0;
544 | }
545 |
546 | /**
547 | * This detects if your finger movement is a result of an actual raw touch event of if it's from a view jitter.
548 | * This uses 3 events of rotation temporary storage so that differentiation between swapping touch events can be determined.
549 | *
550 | * @param newRotValue
551 | */
552 | private boolean isRotationConsistent(final double newRotValue) {
553 | double evalValue = newRotValue;
554 |
555 | if (Double.compare(newRotationStore[2], newRotationStore[1]) != 0) {
556 | newRotationStore[2] = newRotationStore[1];
557 | }
558 | if (Double.compare(newRotationStore[1], newRotationStore[0]) != 0) {
559 | newRotationStore[1] = newRotationStore[0];
560 | }
561 |
562 | newRotationStore[0] = evalValue;
563 |
564 | if (Double.compare(newRotationStore[2], newRotationStore[0]) == 0
565 | || Double.compare(newRotationStore[1], newRotationStore[0]) == 0
566 | || Double.compare(newRotationStore[2], newRotationStore[1]) == 0
567 |
568 | //Is the middle event the odd one out
569 | || (newRotationStore[0] > newRotationStore[1] && newRotationStore[1] < newRotationStore[2])
570 | || (newRotationStore[0] < newRotationStore[1] && newRotationStore[1] > newRotationStore[2])
571 | ) {
572 | return false;
573 | }
574 | return true;
575 | }
576 |
577 |
578 | @IntDef({
579 | SpinRotation.CLOCKWISE,
580 | SpinRotation.COUNTERCLOCKWISE
581 | })
582 | @interface SpinRotation {
583 | int CLOCKWISE = 0;
584 | int COUNTERCLOCKWISE = 1;
585 | }
586 | }
587 |
--------------------------------------------------------------------------------