├── .gitignore
├── .idea
├── .gitignore
├── codeStyles
│ ├── Project.xml
│ └── codeStyleConfig.xml
├── compiler.xml
├── gradle.xml
├── jarRepositories.xml
├── misc.xml
├── runConfigurations.xml
└── vcs.xml
├── LICENSE
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── bullfrog
│ │ └── particle
│ │ └── ExampleInstrumentedTest.kt
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── bullfrog
│ │ │ └── particle
│ │ │ └── MainActivity.kt
│ └── res
│ │ ├── drawable-v24
│ │ └── ic_launcher_foreground.xml
│ │ ├── drawable-xhdpi
│ │ └── button.png
│ │ ├── drawable
│ │ ├── bg_color.xml
│ │ ├── ic_launcher_background.xml
│ │ ├── ic_thumbs_up.xml
│ │ └── star.xml
│ │ ├── layout
│ │ └── activity_main.xml
│ │ ├── mipmap-anydpi-v26
│ │ ├── ic_launcher.xml
│ │ └── ic_launcher_round.xml
│ │ ├── mipmap-hdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-mdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxxhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── values-night
│ │ └── themes.xml
│ │ └── values
│ │ ├── colors.xml
│ │ ├── strings.xml
│ │ └── themes.xml
│ └── test
│ └── java
│ └── com
│ └── bullfrog
│ └── particle
│ └── ExampleUnitTest.kt
├── build.gradle
├── gifs
├── demo.gif
├── demo1.gif
├── demo2.gif
├── demo3.gif
├── demo4.gif
└── demo5.gif
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── particlelib
├── .gitignore
├── build.gradle
├── consumer-rules.pro
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── bullfrog
│ │ └── particle
│ │ └── ExampleInstrumentedTest.kt
│ ├── main
│ ├── AndroidManifest.xml
│ └── java
│ │ └── com
│ │ └── bullfrog
│ │ └── particle
│ │ ├── IParticleManager.kt
│ │ ├── ParticleManager.kt
│ │ ├── Particles.kt
│ │ ├── animation
│ │ └── ParticleAnimation.kt
│ │ ├── animator
│ │ └── ParticleAnimator.kt
│ │ ├── extensions
│ │ └── BitmapExtension.kt
│ │ ├── particle
│ │ ├── IParticle.kt
│ │ ├── configuration
│ │ │ ├── ParticleConfiguration.kt
│ │ │ ├── Rotation.kt
│ │ │ └── Shape.kt
│ │ └── impl
│ │ │ ├── BitmapParticle.kt
│ │ │ ├── CircleIParticle.kt
│ │ │ ├── HollowCircleParticle.kt
│ │ │ ├── HollowPentacleParticle.kt
│ │ │ ├── HollowRectangleParticle.kt
│ │ │ ├── HollowTriangleParticle.kt
│ │ │ ├── PentacleParticle.kt
│ │ │ ├── RectangleParticle.kt
│ │ │ └── TriangleParticle.kt
│ │ ├── path
│ │ ├── FallPathGenerator.kt
│ │ ├── FireWorkPathGenerator.kt
│ │ ├── IPathGenerator.kt
│ │ ├── LinearPathGenerator.kt
│ │ └── RisePathGenerator.kt
│ │ ├── util
│ │ └── ColorUtil.kt
│ │ └── view
│ │ └── ParticleView.kt
│ └── test
│ └── java
│ └── com
│ └── bullfrog
│ └── particle
│ └── ExampleUnitTest.kt
├── settings.gradle
└── 一些比较有意思的公式.md
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea
5 | .DS_Store
6 | /build
7 | /captures
8 | .externalNativeBuild
9 | .cxx
10 | local.properties
11 |
--------------------------------------------------------------------------------
/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /shelf/
3 | /workspace.xml
4 |
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 | xmlns:android
18 |
19 | ^$
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 | xmlns:.*
29 |
30 | ^$
31 |
32 |
33 | BY_NAME
34 |
35 |
36 |
37 |
38 |
39 |
40 | .*:id
41 |
42 | http://schemas.android.com/apk/res/android
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 | .*:name
52 |
53 | http://schemas.android.com/apk/res/android
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 | name
63 |
64 | ^$
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 | style
74 |
75 | ^$
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 | .*
85 |
86 | ^$
87 |
88 |
89 | BY_NAME
90 |
91 |
92 |
93 |
94 |
95 |
96 | .*
97 |
98 | http://schemas.android.com/apk/res/android
99 |
100 |
101 | ANDROID_ATTRIBUTE_ORDER
102 |
103 |
104 |
105 |
106 |
107 |
108 | .*
109 |
110 | .*
111 |
112 |
113 | BY_NAME
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
--------------------------------------------------------------------------------
/.idea/codeStyles/codeStyleConfig.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
22 |
23 |
--------------------------------------------------------------------------------
/.idea/jarRepositories.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # particle
2 |
3 | [](https://jitpack.io/#ultimateHandsomeBoy666/particle)
4 |
5 | [中文介绍](https://juejin.cn/post/6986667003884863519)
6 |
7 | This is a handy android library for particle effect.
8 |
9 | To start with, you need to add it in your root build.gradle at the end of repositories:
10 |
11 | ```groovy
12 | allprojects {
13 | repositories {
14 | ...
15 | maven { url 'https://jitpack.io' }
16 | }
17 | }
18 | ```
19 |
20 | Then add the dependency
21 |
22 | ```groovy
23 | dependencies {
24 | implementation 'com.github.ultimateHandsomeBoy666:Particle:0.1.2'
25 | }
26 | ```
27 |
28 |
29 |
30 | And add the following code:
31 | ```kotlin
32 | Particles.with(context, container) // container is the parent ViewGroup for particles
33 | .colorFromView(button)// color sampling from button
34 | .particleNum(200)// how many particles
35 | .anchor(button)// use button as the anchor of the animation
36 | .shape(Shape.CIRCLE)// circle particle
37 | .radius(2, 6)// random circle radius from 2 to 6
38 | .anim(ParticleAnimation.EXPLOSION)// using explosion animation
39 | .start()
40 | ```
41 | you can get something like this:
42 |
43 |
44 |
45 | You can have more particle annimations by customizing the particle motion curve. The app module in the project shows how to customize it, check it out.
46 |
47 | 




48 |
49 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | plugins {
2 | id 'com.android.application'
3 | id 'kotlin-android'
4 | }
5 |
6 | android {
7 | compileSdkVersion 30
8 | buildToolsVersion "30.0.2"
9 |
10 | defaultConfig {
11 | applicationId "com.bullfrog.particle"
12 | minSdkVersion 21
13 | targetSdkVersion 30
14 | versionCode 1
15 | versionName "1.0"
16 |
17 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
18 | }
19 |
20 | buildTypes {
21 | release {
22 | minifyEnabled false
23 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
24 | }
25 | }
26 | compileOptions {
27 | sourceCompatibility JavaVersion.VERSION_1_8
28 | targetCompatibility JavaVersion.VERSION_1_8
29 | }
30 | kotlinOptions {
31 | jvmTarget = '1.8'
32 | }
33 | }
34 |
35 | dependencies {
36 |
37 | implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
38 | implementation 'androidx.core:core-ktx:1.3.2'
39 | implementation 'androidx.appcompat:appcompat:1.2.0'
40 | implementation 'com.google.android.material:material:1.2.1'
41 | implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
42 | implementation project(path: ':particlelib')
43 | testImplementation 'junit:junit:4.+'
44 | androidTestImplementation 'androidx.test.ext:junit:1.1.2'
45 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
46 | }
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/bullfrog/particle/ExampleInstrumentedTest.kt:
--------------------------------------------------------------------------------
1 | package com.bullfrog.particle
2 |
3 | import android.content.Context
4 | import android.graphics.BitmapFactory
5 | import android.util.Log
6 | import androidx.test.platform.app.InstrumentationRegistry
7 | import androidx.test.ext.junit.runners.AndroidJUnit4
8 | import com.bullfrog.particle.extensions.getColorFromBitmap
9 | import com.bullfrog.particle.util.getColorFromBitmap
10 |
11 | import org.junit.Test
12 | import org.junit.runner.RunWith
13 |
14 | import org.junit.Assert.*
15 |
16 | /**
17 | * Instrumented test, which will execute on an Android device.
18 | *
19 | * See [testing documentation](http://d.android.com/tools/testing).
20 | */
21 | @RunWith(AndroidJUnit4::class)
22 | class ExampleInstrumentedTest {
23 | @Test
24 | fun useAppContext() {
25 | // Context of the app under test.
26 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext
27 | assertEquals("com.bullfrog.particle", appContext.packageName)
28 | }
29 |
30 | private fun getAppContext(): Context {
31 | return InstrumentationRegistry.getInstrumentation().targetContext
32 | }
33 |
34 | @Test
35 | fun bitmap_color_sampling() {
36 | val bitmap = BitmapFactory.decodeResource(getAppContext().resources, R.drawable.button)
37 | val map = bitmap.getColorFromBitmap(100)
38 | var total = 0f
39 | map.values.forEach {
40 | total += it
41 | }
42 | }
43 | }
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/app/src/main/java/com/bullfrog/particle/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.bullfrog.particle
2 |
3 | import android.animation.ValueAnimator
4 | import androidx.appcompat.app.AppCompatActivity
5 | import android.os.Bundle
6 | import android.view.View
7 | import android.view.ViewGroup
8 | import android.widget.Button
9 | import android.widget.TextView
10 | import com.bullfrog.particle.animation.ParticleAnimation
11 | import com.bullfrog.particle.particle.configuration.Shape
12 | import com.bullfrog.particle.particle.configuration.Rotation
13 | import com.bullfrog.particle.path.*
14 | import kotlin.math.*
15 | import kotlin.random.Random
16 |
17 | class MainActivity : AppCompatActivity() {
18 |
19 | private lateinit var button: TextView
20 |
21 | private lateinit var resetButton: Button
22 |
23 | private lateinit var container: ViewGroup
24 |
25 | private var particleManager: IParticleManager? = null
26 |
27 | override fun onCreate(savedInstanceState: Bundle?) {
28 | super.onCreate(savedInstanceState)
29 | setContentView(R.layout.activity_main)
30 | button = findViewById(R.id.button)
31 | resetButton = findViewById(R.id.reset_button)
32 | container = findViewById(R.id.container)
33 |
34 | particleManager = Particles.with(this, container)
35 |
36 | button.setOnClickListener {
37 | particleManager!!.colorFromView(button)
38 | .particleNum(500)
39 | .anchor(it)
40 | .shape(Shape.CIRCLE, Shape.HOLLOW_RECTANGLE)
41 | .radius(8, 14)
42 | .strokeWidth(8f)
43 | .size(40, 40)
44 | .rotation(
45 | Rotation(
46 | 600
47 | )
48 | )
49 | .bitmap(R.drawable.ic_thumbs_up)
50 | .anim(ParticleAnimation.with({
51 | createAnimator()
52 | }, {
53 | createPathGenerator()
54 | }))
55 | particleManager!!.start()
56 | button.visibility = View.GONE
57 | }
58 |
59 | resetButton.setOnClickListener {
60 | button.visibility = View.VISIBLE
61 | particleManager?.cancel()
62 | }
63 | }
64 |
65 | private fun createPathGenerator(): IPathGenerator {
66 | return object : LinearPathGenerator() {
67 | val cos = Random.nextDouble(-1.0, 1.0)
68 | val sin = Random.nextDouble(-1.0, 1.0)
69 |
70 | override fun getCurrentCoord(
71 | progress: Float,
72 | duration: Long,
73 | outCoord: IntArray
74 | ): Unit {
75 | val originalX = distance * progress
76 | val originalY = 100 * sin(originalX / 50)
77 | val x = originalX * cos - originalY * sin
78 | val y = originalX * sin + originalY * cos
79 | outCoord[0] = (0.01 * x * originalY).toInt()
80 | outCoord[1] = -(0.0001 * y.pow(2) * originalX).toInt()
81 | }
82 | }
83 | }
84 |
85 | private fun createAnimator(): ValueAnimator {
86 | val animator = ValueAnimator.ofInt(0, 1)
87 | animator.repeatCount = -1
88 | animator.repeatMode = ValueAnimator.REVERSE
89 | animator.duration = 4000L
90 | return animator
91 | }
92 | }
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
9 |
15 |
18 |
21 |
22 |
23 |
24 |
30 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xhdpi/button.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ultimateHandsomeBoy666/Particle/54e3c104fcdc5fd51fa160730fb20155f087f551/app/src/main/res/drawable-xhdpi/button.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/bg_color.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_thumbs_up.xml:
--------------------------------------------------------------------------------
1 |
6 |
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/star.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
23 |
24 |
35 |
36 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ultimateHandsomeBoy666/Particle/54e3c104fcdc5fd51fa160730fb20155f087f551/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ultimateHandsomeBoy666/Particle/54e3c104fcdc5fd51fa160730fb20155f087f551/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ultimateHandsomeBoy666/Particle/54e3c104fcdc5fd51fa160730fb20155f087f551/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ultimateHandsomeBoy666/Particle/54e3c104fcdc5fd51fa160730fb20155f087f551/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ultimateHandsomeBoy666/Particle/54e3c104fcdc5fd51fa160730fb20155f087f551/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ultimateHandsomeBoy666/Particle/54e3c104fcdc5fd51fa160730fb20155f087f551/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ultimateHandsomeBoy666/Particle/54e3c104fcdc5fd51fa160730fb20155f087f551/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ultimateHandsomeBoy666/Particle/54e3c104fcdc5fd51fa160730fb20155f087f551/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ultimateHandsomeBoy666/Particle/54e3c104fcdc5fd51fa160730fb20155f087f551/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ultimateHandsomeBoy666/Particle/54e3c104fcdc5fd51fa160730fb20155f087f551/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values-night/themes.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
16 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #FFBB86FC
4 | #FF6200EE
5 | #FF3700B3
6 | #FF03DAC5
7 | #FF018786
8 | #FF000000
9 | #FFFFFFFF
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | particle
3 |
--------------------------------------------------------------------------------
/app/src/main/res/values/themes.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
16 |
--------------------------------------------------------------------------------
/app/src/test/java/com/bullfrog/particle/ExampleUnitTest.kt:
--------------------------------------------------------------------------------
1 | package com.bullfrog.particle
2 |
3 | import org.junit.Test
4 |
5 | import org.junit.Assert.*
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * See [testing documentation](http://d.android.com/tools/testing).
11 | */
12 | class ExampleUnitTest {
13 | @Test
14 | fun addition_isCorrect() {
15 | assertEquals(4, 2 + 2)
16 | }
17 | }
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 | buildscript {
3 | ext.kotlin_version = "1.4.30"
4 | repositories {
5 | google()
6 | jcenter()
7 | }
8 | dependencies {
9 | classpath "com.android.tools.build:gradle:4.1.3"
10 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
11 | classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
12 | // NOTE: Do not place your application dependencies here; they belong
13 | // in the individual module build.gradle files
14 | }
15 | }
16 |
17 | allprojects {
18 | repositories {
19 | google()
20 | jcenter()
21 | }
22 | }
23 |
24 | task clean(type: Delete) {
25 | delete rootProject.buildDir
26 | }
--------------------------------------------------------------------------------
/gifs/demo.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ultimateHandsomeBoy666/Particle/54e3c104fcdc5fd51fa160730fb20155f087f551/gifs/demo.gif
--------------------------------------------------------------------------------
/gifs/demo1.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ultimateHandsomeBoy666/Particle/54e3c104fcdc5fd51fa160730fb20155f087f551/gifs/demo1.gif
--------------------------------------------------------------------------------
/gifs/demo2.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ultimateHandsomeBoy666/Particle/54e3c104fcdc5fd51fa160730fb20155f087f551/gifs/demo2.gif
--------------------------------------------------------------------------------
/gifs/demo3.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ultimateHandsomeBoy666/Particle/54e3c104fcdc5fd51fa160730fb20155f087f551/gifs/demo3.gif
--------------------------------------------------------------------------------
/gifs/demo4.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ultimateHandsomeBoy666/Particle/54e3c104fcdc5fd51fa160730fb20155f087f551/gifs/demo4.gif
--------------------------------------------------------------------------------
/gifs/demo5.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ultimateHandsomeBoy666/Particle/54e3c104fcdc5fd51fa160730fb20155f087f551/gifs/demo5.gif
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app"s APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Automatically convert third-party libraries to use AndroidX
19 | android.enableJetifier=true
20 | # Kotlin code style for this project: "official" or "obsolete":
21 | kotlin.code.style=official
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ultimateHandsomeBoy666/Particle/54e3c104fcdc5fd51fa160730fb20155f087f551/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Tue Jun 29 20:00:49 CST 2021
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-6.5-bin.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/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 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
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 Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/particlelib/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/particlelib/build.gradle:
--------------------------------------------------------------------------------
1 | plugins {
2 | id 'com.android.library'
3 | id 'kotlin-android'
4 | }
5 |
6 | apply plugin: 'com.github.dcendents.android-maven'
7 | group = 'com.github.ultimateHandsomeBoy666'
8 | version = '0.1.2'
9 |
10 | android {
11 | compileSdkVersion 30
12 | buildToolsVersion "30.0.2"
13 |
14 | defaultConfig {
15 | minSdkVersion 21
16 | targetSdkVersion 30
17 | }
18 |
19 | buildTypes {
20 | release {
21 | minifyEnabled false
22 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
23 | }
24 | }
25 | compileOptions {
26 | sourceCompatibility JavaVersion.VERSION_1_8
27 | targetCompatibility JavaVersion.VERSION_1_8
28 | }
29 | kotlinOptions {
30 | jvmTarget = '1.8'
31 | }
32 | }
33 |
34 | dependencies {
35 |
36 | implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
37 | implementation 'androidx.core:core-ktx:1.3.2'
38 | implementation 'androidx.appcompat:appcompat:1.2.0'
39 | testImplementation 'junit:junit:4.+'
40 | androidTestImplementation 'androidx.test.ext:junit:1.1.2'
41 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
42 | }
--------------------------------------------------------------------------------
/particlelib/consumer-rules.pro:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ultimateHandsomeBoy666/Particle/54e3c104fcdc5fd51fa160730fb20155f087f551/particlelib/consumer-rules.pro
--------------------------------------------------------------------------------
/particlelib/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
--------------------------------------------------------------------------------
/particlelib/src/androidTest/java/com/bullfrog/particle/ExampleInstrumentedTest.kt:
--------------------------------------------------------------------------------
1 | package com.bullfrog.particle
2 |
3 | import androidx.test.platform.app.InstrumentationRegistry
4 | import androidx.test.ext.junit.runners.AndroidJUnit4
5 |
6 | import org.junit.Test
7 | import org.junit.runner.RunWith
8 |
9 | import org.junit.Assert.*
10 |
11 | /**
12 | * Instrumented test, which will execute on an Android device.
13 | *
14 | * See [testing documentation](http://d.android.com/tools/testing).
15 | */
16 | @RunWith(AndroidJUnit4::class)
17 | class ExampleInstrumentedTest {
18 | @Test
19 | fun useAppContext() {
20 | // Context of the app under test.
21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22 | assertEquals("com.bullfrog.particle.test", appContext.packageName)
23 | }
24 | }
--------------------------------------------------------------------------------
/particlelib/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
--------------------------------------------------------------------------------
/particlelib/src/main/java/com/bullfrog/particle/IParticleManager.kt:
--------------------------------------------------------------------------------
1 | package com.bullfrog.particle
2 |
3 | import android.graphics.Bitmap
4 | import android.graphics.drawable.Drawable
5 | import android.view.View
6 | import androidx.annotation.ColorInt
7 | import androidx.annotation.DrawableRes
8 | import com.bullfrog.particle.particle.configuration.Shape
9 | import com.bullfrog.particle.animation.ParticleAnimation
10 | import com.bullfrog.particle.particle.configuration.Rotation
11 |
12 | interface IParticleManager {
13 |
14 | // region particle color
15 |
16 | fun color(@ColorInt vararg colors: Int): IParticleManager
17 |
18 | fun colorFromBitmap(bitmap: Bitmap, sampleNum: Int = 50): IParticleManager
19 |
20 | fun colorFromView(view: View, sampleNum: Int = 50): IParticleManager
21 |
22 | fun colorFromDrawable(drawable: Drawable, sampleNum: Int = 50): IParticleManager
23 |
24 | fun colorFromDrawable(@DrawableRes drawable: Int, sampleNum: Int = 50): IParticleManager
25 |
26 | // endregion
27 |
28 | // region particle
29 |
30 | fun shape(vararg shapes: Shape): IParticleManager
31 |
32 | fun anim(anim: ParticleAnimation): IParticleManager
33 |
34 | // endregion
35 |
36 | fun anchor(view: View): IParticleManager
37 |
38 | fun anchor(x: Int, y: Int): IParticleManager
39 |
40 | fun particleNum(num: Int): IParticleManager
41 |
42 | fun size(width: Int, height: Int): IParticleManager
43 |
44 | fun size(widthFrom: Int, widthTo: Int, heightFrom: Int, heightTo: Int): IParticleManager
45 |
46 | fun radius(radius: Float): IParticleManager
47 |
48 | fun radius(radiusFrom: Int, radiusTo: Int):IParticleManager
49 |
50 | fun strokeWidth(strokeWidth: Float): IParticleManager
51 |
52 | // resId can be vector resId or bitmap resId
53 | fun bitmap(@DrawableRes resId: Int): IParticleManager
54 |
55 | fun bitmap(view: View): IParticleManager
56 |
57 | fun bitmap(bitmap: Bitmap): IParticleManager
58 |
59 | fun bitmap(drawable: Drawable): IParticleManager
60 |
61 | // region effect
62 |
63 | fun rotation(rotation: Rotation): IParticleManager
64 |
65 | // fun shimmer(): IParticleManager
66 |
67 | // fun keep(): IParticleManager
68 |
69 | // endregion
70 |
71 | fun start()
72 |
73 | fun pause()
74 |
75 | fun cancel()
76 |
77 | fun show()
78 |
79 | fun hide()
80 |
81 | fun remove()
82 | }
--------------------------------------------------------------------------------
/particlelib/src/main/java/com/bullfrog/particle/ParticleManager.kt:
--------------------------------------------------------------------------------
1 | package com.bullfrog.particle
2 |
3 | import android.content.Context
4 | import android.graphics.Bitmap
5 | import android.graphics.drawable.Drawable
6 | import android.view.View
7 | import android.view.ViewGroup
8 | import androidx.annotation.DrawableRes
9 | import androidx.core.content.ContextCompat
10 | import androidx.core.content.res.ResourcesCompat
11 | import androidx.core.graphics.drawable.toBitmap
12 | import androidx.core.view.drawToBitmap
13 | import com.bullfrog.particle.particle.configuration.Shape
14 | import com.bullfrog.particle.view.ParticleView
15 | import com.bullfrog.particle.animation.ParticleAnimation
16 | import com.bullfrog.particle.extensions.getColorFromBitmap
17 | import com.bullfrog.particle.particle.configuration.Rotation
18 |
19 | internal class ParticleManager(
20 | private val context: Context,
21 | private val container: ViewGroup
22 | ) : IParticleManager {
23 |
24 | private var particleView: ParticleView = ParticleView(context)
25 |
26 | override fun color(vararg colors: Int): IParticleManager {
27 | val size = colors.size
28 | colors.forEach {
29 | particleView.colorMap[it] = 1f / size
30 | }
31 | return this
32 | }
33 |
34 | override fun colorFromBitmap(bitmap: Bitmap, sampleNum: Int): IParticleManager {
35 | particleView.colorMap = bitmap.getColorFromBitmap(sampleNum)
36 | return this
37 | }
38 |
39 | override fun colorFromView(view: View, sampleNum: Int): IParticleManager {
40 | val bitmap = view.drawToBitmap()
41 | particleView.colorMap = bitmap.getColorFromBitmap(sampleNum)
42 | return this
43 | }
44 |
45 | override fun colorFromDrawable(drawable: Drawable, sampleNum: Int): IParticleManager {
46 | val bitmap = drawable.toBitmap()
47 | particleView.colorMap = bitmap.getColorFromBitmap(sampleNum)
48 | return this
49 | }
50 |
51 | override fun colorFromDrawable(drawable: Int, sampleNum: Int): IParticleManager {
52 | val bitmap = ResourcesCompat.getDrawable(context.resources, drawable, null)?.toBitmap()
53 | bitmap?.let {
54 | particleView.colorMap = bitmap.getColorFromBitmap(sampleNum)
55 | }
56 | return this
57 | }
58 |
59 | override fun shape(vararg shapes: Shape): IParticleManager {
60 | shapes.forEach {
61 | particleView.shapeList.add(it)
62 | }
63 | return this
64 | }
65 |
66 | override fun anim(anim: ParticleAnimation): IParticleManager {
67 | particleView.anim = anim
68 | return this
69 | }
70 |
71 | override fun anchor(view: View) : IParticleManager {
72 | particleView.anchorX = (view.left + view.right) / 2
73 | particleView.anchorY = (view.top + view.bottom) / 2
74 | return this
75 | }
76 |
77 | override fun anchor(x: Int, y: Int) : IParticleManager {
78 | particleView.anchorX = x
79 | particleView.anchorY = y
80 | return this
81 | }
82 |
83 | override fun particleNum(num: Int) : IParticleManager {
84 | particleView.particleNum = num
85 | return this
86 | }
87 |
88 | override fun size(width: Int, height: Int): IParticleManager {
89 | particleView.widthSize = width
90 | particleView.heightSize = height
91 | particleView.randomSize = false
92 | return this
93 | }
94 |
95 | override fun size(widthFrom: Int, widthTo: Int, heightFrom: Int, heightTo: Int): IParticleManager {
96 | particleView.randomSize = true
97 | particleView.widthSizeRange = IntRange(widthFrom, widthTo)
98 | particleView.heightSizeRange = IntRange(heightFrom, heightTo)
99 | return this
100 | }
101 |
102 | override fun radius(radius: Float): IParticleManager {
103 | particleView.randomRadius = false
104 | particleView.radius = radius
105 | return this
106 | }
107 |
108 | override fun radius(radiusFrom: Int, radiusTo: Int): IParticleManager {
109 | particleView.randomRadius = true
110 | particleView.radiusRange = IntRange(radiusFrom, radiusTo)
111 | return this
112 | }
113 |
114 | override fun strokeWidth(strokeWidth: Float): IParticleManager {
115 | particleView.strokeWidth = strokeWidth
116 | return this
117 | }
118 |
119 | override fun bitmap(@DrawableRes resId: Int): IParticleManager {
120 | val drawable = ContextCompat.getDrawable(context, resId)
121 | particleView.bitmap = drawable?.toBitmap()
122 | return this
123 | }
124 |
125 | override fun bitmap(view: View): IParticleManager {
126 | val bitmap = view.drawToBitmap()
127 | particleView.bitmap = bitmap
128 | return this
129 | }
130 |
131 | override fun bitmap(drawable: Drawable): IParticleManager {
132 | particleView.bitmap = drawable.toBitmap()
133 | return this
134 | }
135 |
136 | override fun bitmap(bitmap: Bitmap): IParticleManager {
137 | particleView.bitmap = bitmap
138 | return this
139 | }
140 |
141 | override fun rotation(rotation: Rotation): IParticleManager {
142 | particleView.rotation = rotation
143 | return this
144 | }
145 |
146 | override fun start() {
147 | if (particleView.parent != container) {
148 | addParticleView()
149 | particleView.configureAndStart()
150 | } else {
151 | particleView.start()
152 | }
153 | }
154 |
155 | override fun pause() {
156 | particleView.pause()
157 | }
158 |
159 | override fun cancel() {
160 | particleView.cancel()
161 | }
162 |
163 | override fun remove() {
164 | removeParticleView()
165 | }
166 |
167 | override fun show() {
168 | particleView.visibility = View.VISIBLE
169 | }
170 |
171 | override fun hide() {
172 | particleView.visibility = View.INVISIBLE
173 | }
174 |
175 | private fun addParticleView() {
176 | container.addView(particleView, ViewGroup.LayoutParams.MATCH_PARENT,
177 | ViewGroup.LayoutParams.MATCH_PARENT)
178 | }
179 |
180 | private fun removeParticleView() {
181 | container.removeView(particleView)
182 | }
183 | }
--------------------------------------------------------------------------------
/particlelib/src/main/java/com/bullfrog/particle/Particles.kt:
--------------------------------------------------------------------------------
1 | package com.bullfrog.particle
2 |
3 | import android.content.Context
4 | import android.view.ViewGroup
5 |
6 | /**
7 | * Util class for user to initialize particle animation chain.
8 | */
9 | class Particles {
10 |
11 | companion object {
12 | fun with(context: Context, container: ViewGroup): IParticleManager {
13 | return ParticleManager(context, container)
14 | }
15 | }
16 | }
--------------------------------------------------------------------------------
/particlelib/src/main/java/com/bullfrog/particle/animation/ParticleAnimation.kt:
--------------------------------------------------------------------------------
1 | package com.bullfrog.particle.animation
2 |
3 | import android.animation.ValueAnimator
4 | import android.view.animation.AccelerateDecelerateInterpolator
5 | import com.bullfrog.particle.path.*
6 |
7 | /**
8 | * Helper class for particle motion paths and animations.
9 | */
10 | class ParticleAnimation private constructor(
11 | private val pathGenerator: () -> IPathGenerator,
12 | private val animator: () -> ValueAnimator = DEFAULT_ANIMATOR_LAMBDA
13 | ) {
14 |
15 | companion object {
16 |
17 | private const val DEFAULT_DURATION = 2000L
18 |
19 | private val DEFAULT_ANIMATOR_LAMBDA = {
20 | val animator = ValueAnimator
21 | .ofInt(0, 1)
22 | .setDuration(DEFAULT_DURATION)
23 | animator.interpolator = AccelerateDecelerateInterpolator()
24 | animator
25 | }
26 |
27 | val EXPLOSION = ParticleAnimation({
28 | LinearPathGenerator()
29 | })
30 |
31 | val FIREWORK = ParticleAnimation({
32 | FireWorkPathGenerator()
33 | })
34 |
35 | val RISE = ParticleAnimation({
36 | RisePathGenerator()
37 | })
38 |
39 | val FALL = ParticleAnimation({
40 | FallPathGenerator()
41 | })
42 |
43 | fun with(animator: () -> ValueAnimator = DEFAULT_ANIMATOR_LAMBDA,
44 | generator: () -> IPathGenerator): ParticleAnimation {
45 | return ParticleAnimation(generator, animator)
46 | }
47 | }
48 |
49 | fun createPathGenerator(): IPathGenerator {
50 | return pathGenerator.invoke()
51 | }
52 |
53 | fun createAnimator(): ValueAnimator {
54 | return animator.invoke()
55 | }
56 | }
--------------------------------------------------------------------------------
/particlelib/src/main/java/com/bullfrog/particle/animator/ParticleAnimator.kt:
--------------------------------------------------------------------------------
1 | package com.bullfrog.particle.animator
2 |
3 | import android.animation.ValueAnimator
4 | import com.bullfrog.particle.particle.IParticle
5 | import com.bullfrog.particle.animation.ParticleAnimation
6 | import com.bullfrog.particle.particle.configuration.RotationDirection
7 |
8 | /**
9 | * Core class that draws the particle motion tracks.
10 | */
11 | class ParticleAnimator(
12 | private val paticles: List,
13 | private val animation: ParticleAnimation
14 | ) {
15 |
16 | private var animator: ValueAnimator? = null
17 |
18 | init {
19 | initPathGenerators()
20 | initAnimator()
21 | }
22 |
23 | private fun initPathGenerators() {
24 | paticles.forEach {
25 | it.pathGenerator = animation.createPathGenerator()
26 | }
27 | }
28 |
29 | private fun initAnimator() {
30 | animator = animation.createAnimator()
31 | val duration = animator!!.duration
32 | val coords = IntArray(2)
33 | animator!!.addUpdateListener { animator ->
34 | val progress = animator.animatedFraction
35 | paticles.forEach {
36 | it.pathGenerator?.getCurrentCoord(progress, duration, coords)
37 | it.x = it.initialX + coords[0]
38 | it.y = it.initialY + coords[1]
39 |
40 | val rotation = it.configuration!!.rotation
41 | val sign = if (rotation.rotationDirection == RotationDirection.ClockWise) 1 else -1
42 | it.angle = sign * rotation.angularVelocity * duration * progress / 1000f
43 | }
44 | }
45 | }
46 |
47 | internal fun start() {
48 | animator?.start()
49 | }
50 |
51 | internal fun pause() {
52 | animator?.pause()
53 | }
54 |
55 | internal fun cancel() {
56 | animator?.cancel()
57 | }
58 | }
--------------------------------------------------------------------------------
/particlelib/src/main/java/com/bullfrog/particle/extensions/BitmapExtension.kt:
--------------------------------------------------------------------------------
1 | package com.bullfrog.particle.extensions
2 |
3 | import android.graphics.Bitmap
4 | import kotlin.math.sqrt
5 |
6 | // scale sampling
7 | fun Bitmap.getColorFromBitmap(sampleNum: Int): MutableMap {
8 | val map = mutableMapOf()
9 |
10 | val width = this.width
11 | val height = this.height
12 |
13 | if (width * height < sampleNum) {
14 | throw Exception("Sample num exceed bitmap pixels num!")
15 | }
16 |
17 | val scale = sqrt(sampleNum.toDouble())
18 | val heightStep = (height / scale).toInt().coerceAtLeast(1)
19 | val widthStep = (width / scale).toInt().coerceAtLeast(1)
20 |
21 | val heightSample = if (height % heightStep == 0) (height / heightStep) else (height / heightStep + 1)
22 | val widthSample = if (width % widthStep == 0) (width / widthStep) else (width / widthStep + 1)
23 | val actualSampleNum = heightSample * widthSample
24 |
25 | for (i in 0 until height step heightStep) {
26 | for (j in 0 until width step widthStep) {
27 | val pixel = this.getPixel(j, i)
28 | map[pixel] = (map[pixel] ?: 0f) + 1f / actualSampleNum
29 | }
30 | }
31 |
32 | return map
33 | }
--------------------------------------------------------------------------------
/particlelib/src/main/java/com/bullfrog/particle/particle/IParticle.kt:
--------------------------------------------------------------------------------
1 | package com.bullfrog.particle.particle
2 |
3 | import android.graphics.Canvas
4 | import android.graphics.Paint
5 | import com.bullfrog.particle.particle.configuration.ParticleConfiguration
6 | import com.bullfrog.particle.path.IPathGenerator
7 |
8 | /**
9 | * This interface defines what basic attributes a particle should have.
10 | */
11 | interface IParticle {
12 |
13 | var initialX: Int
14 |
15 | var initialY: Int
16 |
17 | var x: Int
18 |
19 | var y: Int
20 |
21 | var angle: Float
22 |
23 | //var alpha: Int
24 |
25 | var configuration: ParticleConfiguration?
26 |
27 | var pathGenerator: IPathGenerator?
28 |
29 | fun draw(canvas: Canvas, paint: Paint)
30 |
31 | fun initAfterConfigure() {
32 |
33 | }
34 |
35 | }
--------------------------------------------------------------------------------
/particlelib/src/main/java/com/bullfrog/particle/particle/configuration/ParticleConfiguration.kt:
--------------------------------------------------------------------------------
1 | package com.bullfrog.particle.particle.configuration
2 |
3 | import android.graphics.Bitmap
4 | import android.graphics.Color
5 |
6 |
7 | const val DEFAULT_COLOR = Color.GREEN
8 |
9 | const val DEFAULT_RADIUS = 8f
10 |
11 | const val DEFAULT_STROKE_WIDTH = 1.5f
12 |
13 | const val DEFAULT_WIDTH = 10
14 |
15 | const val DEFAULT_HEIGHT = 10
16 |
17 | val DEFAULT_SHAPE = Shape.CIRCLE
18 |
19 | val ROTATION_NONE = Rotation()
20 |
21 | class ParticleConfiguration {
22 |
23 | // BASE
24 | internal var color: Int = DEFAULT_COLOR
25 |
26 | internal var shape: Shape = DEFAULT_SHAPE
27 |
28 | internal var width: Int = DEFAULT_WIDTH
29 |
30 | internal var height: Int = DEFAULT_HEIGHT
31 |
32 | internal var shimmer: Boolean = false
33 |
34 | internal var rotation: Rotation = ROTATION_NONE
35 |
36 | // CIRCLE
37 | internal var radius: Float = DEFAULT_RADIUS
38 |
39 | // HOLLOW
40 | internal var strokeWidth: Float = DEFAULT_STROKE_WIDTH
41 |
42 | // BITMAP
43 | internal var bitmap: Bitmap? = null
44 | }
--------------------------------------------------------------------------------
/particlelib/src/main/java/com/bullfrog/particle/particle/configuration/Rotation.kt:
--------------------------------------------------------------------------------
1 | package com.bullfrog.particle.particle.configuration
2 |
3 | enum class RotationDirection {
4 | ClockWise,
5 | AntiClockWise
6 | }
7 |
8 | class Rotation(
9 | internal var angularVelocity: Int = 0, // degree per second
10 | internal var rotationDirection: RotationDirection = RotationDirection.ClockWise
11 | ) {
12 |
13 | }
--------------------------------------------------------------------------------
/particlelib/src/main/java/com/bullfrog/particle/particle/configuration/Shape.kt:
--------------------------------------------------------------------------------
1 | package com.bullfrog.particle.particle.configuration
2 |
3 | /**
4 | * All particle shapes.
5 | */
6 | enum class Shape {
7 | CIRCLE,
8 | HOLLOW_CIRCLE,
9 | TRIANGLE,
10 | HOLLOW_TRIANGLE,
11 | RECTANGLE,
12 | HOLLOW_RECTANGLE,
13 | PENTACLE,
14 | HOLLOW_PENTACLE,
15 | BITMAP
16 | }
--------------------------------------------------------------------------------
/particlelib/src/main/java/com/bullfrog/particle/particle/impl/BitmapParticle.kt:
--------------------------------------------------------------------------------
1 | package com.bullfrog.particle.particle.impl
2 |
3 | import android.graphics.Canvas
4 | import android.graphics.Matrix
5 | import android.graphics.Paint
6 | import com.bullfrog.particle.particle.IParticle
7 | import com.bullfrog.particle.particle.configuration.ParticleConfiguration
8 | import com.bullfrog.particle.path.IPathGenerator
9 |
10 | class BitmapParticle : IParticle {
11 |
12 | override var configuration: ParticleConfiguration? = null
13 |
14 | override var initialX: Int = 0
15 |
16 | override var initialY: Int = 0
17 |
18 | override var x: Int = 0
19 |
20 | override var y: Int = 0
21 |
22 | override var angle: Float = 0f
23 |
24 | override var pathGenerator: IPathGenerator? = null
25 |
26 | private var matrix = Matrix()
27 |
28 | private var xScale = 1f
29 |
30 | private var yScale = 1f
31 |
32 | override fun initAfterConfigure() {
33 | val bitmap = configuration!!.bitmap
34 | bitmap?.let {
35 | xScale = configuration!!.width / it.width.toFloat()
36 | yScale = configuration!!.height / it.height.toFloat()
37 | }
38 | }
39 |
40 | override fun draw(canvas: Canvas, paint: Paint) {
41 | val bitmap = configuration!!.bitmap
42 | bitmap?.let {
43 | matrix.reset()
44 | matrix.postTranslate(-it.width / 2f, -it.height / 2f)
45 | matrix.postScale(xScale, yScale)
46 | matrix.postTranslate(x.toFloat(), y.toFloat())
47 | matrix.postRotate(angle, x.toFloat(), y.toFloat())
48 | canvas.drawBitmap(it, matrix, paint)
49 | }
50 | }
51 | }
--------------------------------------------------------------------------------
/particlelib/src/main/java/com/bullfrog/particle/particle/impl/CircleIParticle.kt:
--------------------------------------------------------------------------------
1 | package com.bullfrog.particle.particle.impl
2 |
3 | import android.graphics.Canvas
4 | import android.graphics.Paint
5 | import com.bullfrog.particle.particle.IParticle
6 | import com.bullfrog.particle.particle.configuration.ParticleConfiguration
7 | import com.bullfrog.particle.path.IPathGenerator
8 |
9 | class CircleIParticle : IParticle {
10 |
11 | override var configuration: ParticleConfiguration? = null
12 |
13 | override var initialX: Int = 0
14 |
15 | override var initialY: Int = 0
16 |
17 | override var x: Int = 0
18 |
19 | override var y: Int = 0
20 |
21 | override var angle: Float = 0f
22 |
23 | override var pathGenerator: IPathGenerator? = null
24 |
25 | override fun draw(canvas: Canvas, paint: Paint) {
26 | paint.color = configuration!!.color
27 | paint.style = Paint.Style.FILL
28 | val radius = configuration!!.radius
29 | canvas.drawCircle(x.toFloat(), y.toFloat(), radius, paint)
30 | }
31 | }
--------------------------------------------------------------------------------
/particlelib/src/main/java/com/bullfrog/particle/particle/impl/HollowCircleParticle.kt:
--------------------------------------------------------------------------------
1 | package com.bullfrog.particle.particle.impl
2 |
3 | import android.graphics.Canvas
4 | import android.graphics.Paint
5 | import com.bullfrog.particle.particle.*
6 | import com.bullfrog.particle.particle.configuration.ParticleConfiguration
7 | import com.bullfrog.particle.path.IPathGenerator
8 |
9 | class HollowCircleParticle : IParticle {
10 |
11 | override var configuration: ParticleConfiguration? = null
12 |
13 | override var initialX: Int = 0
14 |
15 | override var initialY: Int = 0
16 |
17 | override var x: Int = 0
18 |
19 | override var y: Int = 0
20 |
21 | override var angle: Float = 0f
22 |
23 | override var pathGenerator: IPathGenerator? = null
24 |
25 | override fun draw(canvas: Canvas, paint: Paint) {
26 | paint.color = configuration!!.color
27 | paint.strokeWidth = configuration!!.strokeWidth
28 | paint.style = Paint.Style.STROKE
29 | val radius = configuration!!.radius
30 | canvas.drawCircle(x.toFloat(), y.toFloat(), radius, paint)
31 | }
32 | }
--------------------------------------------------------------------------------
/particlelib/src/main/java/com/bullfrog/particle/particle/impl/HollowPentacleParticle.kt:
--------------------------------------------------------------------------------
1 | package com.bullfrog.particle.particle.impl
2 |
3 | import android.graphics.Canvas
4 | import android.graphics.Matrix
5 | import android.graphics.Paint
6 | import android.graphics.Path
7 | import com.bullfrog.particle.particle.IParticle
8 | import com.bullfrog.particle.particle.configuration.ParticleConfiguration
9 | import com.bullfrog.particle.path.IPathGenerator
10 | import kotlin.math.PI
11 | import kotlin.math.cos
12 | import kotlin.math.sin
13 |
14 | class HollowPentacleParticle : IParticle {
15 |
16 | override var configuration: ParticleConfiguration? = null
17 |
18 | override var initialX: Int = 0
19 |
20 | override var initialY: Int = 0
21 |
22 | override var x: Int = 0
23 |
24 | override var y: Int = 0
25 |
26 | override var angle: Float = 0f
27 |
28 | override var pathGenerator: IPathGenerator? = null
29 |
30 | private val path: Path = Path()
31 |
32 | private val matrix = Matrix()
33 |
34 | private val sin1 = sin(PI / 5f).toFloat()
35 | private val cos1 = cos(PI / 5f).toFloat()
36 |
37 | private val sin2 = sin(PI / 5f * 2 ).toFloat()
38 | private val cos2 = cos(PI / 5f * 2).toFloat()
39 |
40 | private val sin3 = sin(PI / 5f * 3).toFloat()
41 | private val cos3 = cos(PI / 5f * 3).toFloat()
42 |
43 | private val sin4 = sin(PI / 5f * 4).toFloat()
44 | private val cos4 = cos(PI / 5f * 4).toFloat()
45 |
46 | private val sin5 = sin(PI).toFloat()
47 | private val cos5 = cos(PI).toFloat()
48 |
49 | private val sin6 = sin(PI / 5f * 6).toFloat()
50 | private val cos6 = cos(PI / 5f * 6).toFloat()
51 |
52 | private val sin7 = sin(PI / 5f * 7).toFloat()
53 | private val cos7 = cos(PI / 5f * 7).toFloat()
54 |
55 |
56 | private val sin8 = sin(PI / 5f * 8).toFloat()
57 | private val cos8 = cos(PI / 5f * 8).toFloat()
58 |
59 | private val sin9 = sin(PI / 5f * 9).toFloat()
60 | private val cos9 = cos(PI / 5f * 9).toFloat()
61 |
62 | private var innerRadius: Float = -1f
63 |
64 | override fun initAfterConfigure() {
65 | val radius = configuration!!.radius
66 | innerRadius = radius * cos1 - radius * sin1 * sin1 / cos1
67 | }
68 |
69 | override fun draw(canvas: Canvas, paint: Paint) {
70 | paint.color = configuration!!.color
71 | paint.style = Paint.Style.STROKE
72 | paint.strokeWidth = configuration!!.strokeWidth
73 | val radius = configuration!!.radius
74 | matrix.reset()
75 | matrix.postRotate(angle, x.toFloat(), y.toFloat())
76 | path.reset()
77 | path.moveTo(x.toFloat(), y - radius)
78 | path.lineTo(x + innerRadius * sin1, y - innerRadius * cos1)
79 | path.lineTo(x + radius * sin2, y - radius * cos2)
80 | path.lineTo(x + innerRadius * sin3, y - innerRadius * cos3)
81 | path.lineTo(x + radius * sin4, y - radius * cos4)
82 | path.lineTo(x.toFloat(), y + innerRadius)
83 | path.lineTo(x + radius * sin6, y - radius * cos6)
84 | path.lineTo(x + innerRadius * sin7, y - innerRadius * cos7)
85 | path.lineTo(x + radius * sin8, y - radius * cos8)
86 | path.lineTo(x + innerRadius * sin9, y - innerRadius * cos9)
87 | path.close()
88 | path.transform(matrix)
89 | canvas.drawPath(path, paint)
90 | }
91 | }
--------------------------------------------------------------------------------
/particlelib/src/main/java/com/bullfrog/particle/particle/impl/HollowRectangleParticle.kt:
--------------------------------------------------------------------------------
1 | package com.bullfrog.particle.particle.impl
2 |
3 | import android.graphics.Canvas
4 | import android.graphics.Matrix
5 | import android.graphics.Paint
6 | import android.graphics.Path
7 | import com.bullfrog.particle.particle.IParticle
8 | import com.bullfrog.particle.particle.configuration.ParticleConfiguration
9 | import com.bullfrog.particle.path.IPathGenerator
10 |
11 | class HollowRectangleParticle : IParticle {
12 |
13 |
14 | override var configuration: ParticleConfiguration? = null
15 |
16 | override var initialX: Int = 0
17 |
18 | override var initialY: Int = 0
19 |
20 | override var x: Int = 0
21 |
22 | override var y: Int = 0
23 |
24 | override var angle: Float = 0f
25 |
26 | override var pathGenerator: IPathGenerator? = null
27 |
28 | private val path = Path()
29 |
30 | private val matrix = Matrix()
31 |
32 | override fun draw(canvas: Canvas, paint: Paint) {
33 | paint.color = configuration!!.color
34 | paint.style = Paint.Style.STROKE
35 | paint.strokeWidth = configuration!!.strokeWidth
36 | val width = configuration!!.width
37 | val height = configuration!!.height
38 | matrix.reset()
39 | matrix.postRotate(angle, x.toFloat(), y.toFloat())
40 | path.reset()
41 | path.moveTo(x - width / 2f, y - height / 2f)
42 | path.lineTo(x + width / 2f, y - height / 2f)
43 | path.lineTo(x + width / 2f, y + height / 2f)
44 | path.lineTo(x - width / 2f, y + height / 2f)
45 | path.close()
46 | path.transform(matrix)
47 | canvas.drawPath(path, paint)
48 | }
49 | }
--------------------------------------------------------------------------------
/particlelib/src/main/java/com/bullfrog/particle/particle/impl/HollowTriangleParticle.kt:
--------------------------------------------------------------------------------
1 | package com.bullfrog.particle.particle.impl
2 |
3 | import android.graphics.Canvas
4 | import android.graphics.Matrix
5 | import android.graphics.Paint
6 | import android.graphics.Path
7 | import com.bullfrog.particle.particle.IParticle
8 | import com.bullfrog.particle.particle.configuration.ParticleConfiguration
9 | import com.bullfrog.particle.path.IPathGenerator
10 |
11 | class HollowTriangleParticle : IParticle {
12 |
13 | override var configuration: ParticleConfiguration? = null
14 |
15 | override var initialX: Int = 0
16 |
17 | override var initialY: Int = 0
18 |
19 | override var x: Int = 0
20 |
21 | override var y: Int = 0
22 |
23 | override var angle: Float = 0f
24 |
25 | override var pathGenerator: IPathGenerator? = null
26 |
27 | private val path = Path()
28 |
29 | private val matrix = Matrix()
30 |
31 | override fun draw(canvas: Canvas, paint: Paint) {
32 | paint.color = configuration!!.color
33 | paint.style = Paint.Style.STROKE
34 | paint.strokeWidth = configuration!!.strokeWidth
35 | val width = configuration!!.width
36 | val height = configuration!!.height
37 | matrix.reset()
38 | matrix.postRotate(angle, x.toFloat(), y.toFloat())
39 | path.reset()
40 | path.moveTo(x.toFloat(), y - height / 2f)
41 | path.lineTo(x - width / 2f, y + height / 2f)
42 | path.lineTo(x + width / 2f, y + height / 2f)
43 | path.close()
44 | path.transform(matrix)
45 | canvas.drawPath(path, paint)
46 | }
47 | }
--------------------------------------------------------------------------------
/particlelib/src/main/java/com/bullfrog/particle/particle/impl/PentacleParticle.kt:
--------------------------------------------------------------------------------
1 | package com.bullfrog.particle.particle.impl
2 |
3 | import android.graphics.Canvas
4 | import android.graphics.Matrix
5 | import android.graphics.Paint
6 | import android.graphics.Path
7 | import com.bullfrog.particle.particle.IParticle
8 | import com.bullfrog.particle.particle.configuration.ParticleConfiguration
9 | import com.bullfrog.particle.path.IPathGenerator
10 | import kotlin.math.PI
11 | import kotlin.math.cos
12 | import kotlin.math.sin
13 |
14 | class PentacleParticle : IParticle {
15 |
16 | override var configuration: ParticleConfiguration? = null
17 |
18 | override var initialX: Int = 0
19 |
20 | override var initialY: Int = 0
21 |
22 | override var x: Int = 0
23 |
24 | override var y: Int = 0
25 |
26 | override var angle: Float = 0f
27 |
28 | override var pathGenerator: IPathGenerator? = null
29 |
30 | private val path: Path = Path()
31 |
32 | private val matrix = Matrix()
33 |
34 | private val sin1 = sin(PI / 5f).toFloat()
35 | private val cos1 = cos(PI / 5f).toFloat()
36 |
37 | private val sin2 = sin(PI / 5f * 2 ).toFloat()
38 | private val cos2 = cos(PI / 5f * 2).toFloat()
39 |
40 | private val sin3 = sin(PI / 5f * 3).toFloat()
41 | private val cos3 = cos(PI / 5f * 3).toFloat()
42 |
43 | private val sin4 = sin(PI / 5f * 4).toFloat()
44 | private val cos4 = cos(PI / 5f * 4).toFloat()
45 |
46 | private val sin5 = sin(PI).toFloat()
47 | private val cos5 = cos(PI).toFloat()
48 |
49 | private val sin6 = sin(PI / 5f * 6).toFloat()
50 | private val cos6 = cos(PI / 5f * 6).toFloat()
51 |
52 | private val sin7 = sin(PI / 5f * 7).toFloat()
53 | private val cos7 = cos(PI / 5f * 7).toFloat()
54 |
55 |
56 | private val sin8 = sin(PI / 5f * 8).toFloat()
57 | private val cos8 = cos(PI / 5f * 8).toFloat()
58 |
59 | private val sin9 = sin(PI / 5f * 9).toFloat()
60 | private val cos9 = cos(PI / 5f * 9).toFloat()
61 |
62 | private var innerRadius: Float = -1f
63 |
64 | override fun initAfterConfigure() {
65 | val radius = configuration!!.radius
66 | innerRadius = radius * cos1 - radius * sin1 * sin1 / cos1
67 | }
68 |
69 | override fun draw(canvas: Canvas, paint: Paint) {
70 | paint.color = configuration!!.color
71 | paint.style = Paint.Style.FILL
72 | val radius = configuration!!.radius
73 | matrix.reset()
74 | matrix.postRotate(angle, x.toFloat(), y.toFloat())
75 | path.reset()
76 | path.moveTo(x.toFloat(), y - radius)
77 | path.lineTo(x + innerRadius * sin1, y - innerRadius * cos1)
78 | path.lineTo(x + radius * sin2, y - radius * cos2)
79 | path.lineTo(x + innerRadius * sin3, y - innerRadius * cos3)
80 | path.lineTo(x + radius * sin4, y - radius * cos4)
81 | path.lineTo(x.toFloat(), y + innerRadius)
82 | path.lineTo(x + radius * sin6, y - radius * cos6)
83 | path.lineTo(x + innerRadius * sin7, y - innerRadius * cos7)
84 | path.lineTo(x + radius * sin8, y - radius * cos8)
85 | path.lineTo(x + innerRadius * sin9, y - innerRadius * cos9)
86 | path.close()
87 | path.transform(matrix)
88 | canvas.drawPath(path, paint)
89 | }
90 | }
--------------------------------------------------------------------------------
/particlelib/src/main/java/com/bullfrog/particle/particle/impl/RectangleParticle.kt:
--------------------------------------------------------------------------------
1 | package com.bullfrog.particle.particle.impl
2 |
3 | import android.graphics.Canvas
4 | import android.graphics.Matrix
5 | import android.graphics.Paint
6 | import android.graphics.Path
7 | import com.bullfrog.particle.particle.IParticle
8 | import com.bullfrog.particle.particle.configuration.ParticleConfiguration
9 | import com.bullfrog.particle.path.IPathGenerator
10 |
11 | class RectangleParticle : IParticle {
12 |
13 | override var configuration: ParticleConfiguration? = null
14 |
15 | override var initialX: Int = 0
16 |
17 | override var initialY: Int = 0
18 |
19 | override var x: Int = 0
20 |
21 | override var y: Int = 0
22 |
23 | override var angle: Float = 0f
24 |
25 | override var pathGenerator: IPathGenerator? = null
26 |
27 | private val path = Path()
28 |
29 | private val matrix = Matrix()
30 |
31 | override fun draw(canvas: Canvas, paint: Paint) {
32 | paint.color = configuration!!.color
33 | paint.style = Paint.Style.FILL
34 | paint.strokeWidth = configuration!!.strokeWidth
35 | val width = configuration!!.width
36 | val height = configuration!!.height
37 | matrix.reset()
38 | matrix.postRotate(angle, x.toFloat(), y.toFloat())
39 | path.reset()
40 | path.moveTo(x - width / 2f, y - height / 2f)
41 | path.lineTo(x + width / 2f, y - height / 2f)
42 | path.lineTo(x + width / 2f, y + height / 2f)
43 | path.lineTo(x - width / 2f, y + height / 2f)
44 | path.close()
45 | path.transform(matrix)
46 | canvas.drawPath(path, paint)
47 | }
48 | }
--------------------------------------------------------------------------------
/particlelib/src/main/java/com/bullfrog/particle/particle/impl/TriangleParticle.kt:
--------------------------------------------------------------------------------
1 | package com.bullfrog.particle.particle.impl
2 |
3 | import android.graphics.*
4 | import com.bullfrog.particle.particle.IParticle
5 | import com.bullfrog.particle.particle.configuration.ParticleConfiguration
6 | import com.bullfrog.particle.path.IPathGenerator
7 |
8 | class TriangleParticle : IParticle {
9 |
10 | override var configuration: ParticleConfiguration? = null
11 |
12 | override var initialX: Int = 0
13 |
14 | override var initialY: Int = 0
15 |
16 | override var x: Int = 0
17 |
18 | override var y: Int = 0
19 |
20 | override var angle: Float = 0f
21 |
22 | override var pathGenerator: IPathGenerator? = null
23 |
24 | private val path = Path()
25 |
26 | private val matrix = Matrix()
27 |
28 | override fun draw(canvas: Canvas, paint: Paint) {
29 | paint.color = configuration!!.color
30 | paint.style = Paint.Style.FILL
31 | val width = configuration!!.width
32 | val height = configuration!!.height
33 | matrix.reset()
34 | matrix.postRotate(angle, x.toFloat(), y.toFloat())
35 | path.reset()
36 | path.moveTo(x.toFloat(), y - height / 2f)
37 | path.lineTo(x - width / 2f, y + height / 2f)
38 | path.lineTo(x + width / 2f, y + height / 2f)
39 | path.close()
40 | path.transform(matrix)
41 | canvas.drawPath(path, paint)
42 | }
43 | }
--------------------------------------------------------------------------------
/particlelib/src/main/java/com/bullfrog/particle/path/FallPathGenerator.kt:
--------------------------------------------------------------------------------
1 | package com.bullfrog.particle.path
2 |
3 | import kotlin.random.Random
4 |
5 | /**
6 | * All particles fall down.
7 | */
8 | open class FallPathGenerator : LinearPathGenerator() {
9 |
10 | override var distance = Random.nextInt(800)
11 |
12 | open val x = Random.nextInt(-300, 300)
13 |
14 | override fun getCurrentCoord(progress: Float, duration: Long, outCoord: IntArray): Unit {
15 | val coordY = progress * distance
16 | outCoord[0] = x
17 | outCoord[1] = coordY.toInt()
18 | }
19 | }
--------------------------------------------------------------------------------
/particlelib/src/main/java/com/bullfrog/particle/path/FireWorkPathGenerator.kt:
--------------------------------------------------------------------------------
1 | package com.bullfrog.particle.path
2 |
3 | import kotlin.math.PI
4 | import kotlin.math.cos
5 | import kotlin.math.pow
6 | import kotlin.math.sin
7 | import kotlin.random.Random
8 |
9 | /**
10 | * Particles explode like firework.
11 | */
12 | open class FireWorkPathGenerator : IPathGenerator {
13 |
14 | open var gravity = 1000
15 |
16 | open var initVelocity = Random.nextInt(-800, 800)
17 |
18 | open var theta = Random.nextDouble(PI)
19 |
20 | open var initVelocityX = 0.0
21 |
22 | open var initVelocityY = 0.0
23 |
24 | init {
25 | initVelocityX = initVelocity * cos(theta)
26 | initVelocityY = initVelocity * sin(theta)
27 | }
28 |
29 | override fun getCurrentCoord(progress: Float, duration: Long, outCoord: IntArray): Unit {
30 | val t = duration * progress / 1000
31 | val x = initVelocityX * t
32 | val y = initVelocityY * t - 0.5f * gravity * t.pow(2)
33 | outCoord[0] = x.toInt()
34 | outCoord[1] = -y.toInt()
35 | }
36 | }
--------------------------------------------------------------------------------
/particlelib/src/main/java/com/bullfrog/particle/path/IPathGenerator.kt:
--------------------------------------------------------------------------------
1 | package com.bullfrog.particle.path
2 |
3 | /**
4 | * This interface defines what to need to make particle motion paths. Basically this class just need
5 | * to calculate the particle coordinates while the particle animation is playing.
6 | */
7 | interface IPathGenerator {
8 |
9 | /**
10 | * Get current particle coordinates.
11 | * @param progress progress of the current particle animation.
12 | * @param duration duration of the current paticle animation.
13 | * @param outCoord array that holds the particle coordinate.
14 | */
15 | fun getCurrentCoord(progress: Float, duration: Long, outCoord: IntArray)
16 |
17 | }
--------------------------------------------------------------------------------
/particlelib/src/main/java/com/bullfrog/particle/path/LinearPathGenerator.kt:
--------------------------------------------------------------------------------
1 | package com.bullfrog.particle.path
2 |
3 | import kotlin.math.PI
4 | import kotlin.math.cos
5 | import kotlin.math.sin
6 | import kotlin.random.Random
7 |
8 | /**
9 | * All particles move in different straight ways.
10 | */
11 | open class LinearPathGenerator: IPathGenerator {
12 |
13 | open var distance = Random.nextInt( 150, 800)
14 |
15 | open val theta = Random.nextDouble(2 * PI)
16 |
17 | override fun getCurrentCoord(progress: Float, duration: Long, outCoord: IntArray): Unit {
18 | val coordX = distance * progress * cos(theta)
19 | val coordY = distance * progress * sin(theta)
20 | outCoord[0] = coordX.toInt()
21 | outCoord[1] = coordY.toInt()
22 | }
23 |
24 | }
--------------------------------------------------------------------------------
/particlelib/src/main/java/com/bullfrog/particle/path/RisePathGenerator.kt:
--------------------------------------------------------------------------------
1 | package com.bullfrog.particle.path
2 |
3 | import kotlin.random.Random
4 |
5 | /**
6 | * All particles rise up.
7 | */
8 | open class RisePathGenerator : LinearPathGenerator() {
9 |
10 | override var distance = Random.nextInt(800)
11 |
12 | open val x = Random.nextInt(-300, 300)
13 |
14 | override fun getCurrentCoord(progress: Float, duration: Long, outCoord: IntArray): Unit {
15 | val coordY = progress * distance
16 | outCoord[0] = x
17 | outCoord[1] = coordY.toInt()
18 | }
19 | }
--------------------------------------------------------------------------------
/particlelib/src/main/java/com/bullfrog/particle/util/ColorUtil.kt:
--------------------------------------------------------------------------------
1 | package com.bullfrog.particle.util
2 |
3 | import androidx.annotation.ColorInt
4 |
5 | @ColorInt fun getColorFromRGB(r: Int, g: Int, b: Int, a: Int = 255) : Int {
6 | if (r > 255 || g > 255 || b > 255 || a > 255) {
7 | throw Exception("Component of argb is bigger than 255!")
8 | }
9 | return (a shl 24) or (r shl 16) or (g shl 8) or (b)
10 | }
11 |
--------------------------------------------------------------------------------
/particlelib/src/main/java/com/bullfrog/particle/view/ParticleView.kt:
--------------------------------------------------------------------------------
1 | package com.bullfrog.particle.view
2 |
3 | import android.content.Context
4 | import android.graphics.Bitmap
5 | import android.graphics.Canvas
6 | import android.graphics.Paint
7 | import android.util.AttributeSet
8 | import android.view.View
9 | import com.bullfrog.particle.animator.ParticleAnimator
10 | import com.bullfrog.particle.particle.configuration.Shape
11 | import com.bullfrog.particle.animation.ParticleAnimation
12 | import com.bullfrog.particle.particle.*
13 | import com.bullfrog.particle.particle.configuration.*
14 | import com.bullfrog.particle.particle.impl.*
15 | import kotlin.random.Random
16 |
17 | internal class ParticleView @JvmOverloads constructor(
18 | context: Context,
19 | attributeSet: AttributeSet? = null,
20 | defStyleAttr: Int = 0,
21 | defStyleRes: Int = 0
22 | ) : View(
23 | context,
24 | attributeSet,
25 | defStyleAttr,
26 | defStyleRes
27 | ) {
28 |
29 | companion object {
30 | private const val DEFAULT_PARTICLE_NUM = 50
31 | private val DEFAULT_RANGE = 0..10
32 | private val DEFAULT_RADIUS_RANGE = 4..12
33 | }
34 |
35 | // key: color int; value: color portion
36 | var colorMap = mutableMapOf<@androidx.annotation.ColorInt Int, Float>()
37 |
38 | var shapeList = mutableListOf()
39 |
40 | var mParticles: MutableList = mutableListOf()
41 |
42 | var anim: ParticleAnimation = ParticleAnimation.EXPLOSION
43 |
44 | var anchorX: Int = 0
45 |
46 | var anchorY: Int = 0
47 |
48 | var particleNum: Int = DEFAULT_PARTICLE_NUM
49 |
50 | var shimmer: Boolean = false
51 |
52 | var rotation: Rotation = ROTATION_NONE
53 |
54 | var widthSize: Int = DEFAULT_WIDTH
55 |
56 | var heightSize: Int = DEFAULT_HEIGHT
57 |
58 | var widthSizeRange: IntRange = DEFAULT_RANGE
59 |
60 | var heightSizeRange: IntRange = DEFAULT_RANGE
61 |
62 | var randomSize: Boolean = false
63 |
64 | var radius: Float = DEFAULT_RADIUS
65 |
66 | var radiusRange: IntRange = DEFAULT_RADIUS_RANGE
67 |
68 | var randomRadius: Boolean = false
69 |
70 | var strokeWidth: Float = DEFAULT_STROKE_WIDTH
71 |
72 | var bitmap: Bitmap? = null
73 |
74 | var paint: Paint = Paint()
75 |
76 | var pathAnimator: ParticleAnimator? = null
77 |
78 | override fun onDraw(canvas: Canvas) {
79 | mParticles.forEach {
80 | it.draw(canvas, paint)
81 | }
82 | invalidate()
83 | }
84 |
85 | private fun startInternal() {
86 | visibility = VISIBLE
87 | pathAnimator?.start()
88 | }
89 |
90 | fun configureAndStart() {
91 | generateParticleList()
92 | configureColor()
93 | configureAnim()
94 | mParticles.forEach {
95 | configureAnchor(it)
96 | // configureShimmer(it)
97 | configureRotation(it)
98 | if (randomSize) {
99 | configureRandomSize(it)
100 | } else {
101 | configureSize(it)
102 | }
103 | if (randomRadius) {
104 | configureRandomRadius(it)
105 | } else {
106 | configureRadius(it)
107 | }
108 | configureStrokeWidth(it)
109 | configureBitmap(it)
110 |
111 | // init after configure
112 | it.initAfterConfigure()
113 | }
114 | startInternal()
115 | }
116 |
117 | fun start() {
118 | startInternal()
119 | }
120 |
121 | fun pause() {
122 | pathAnimator?.pause()
123 | }
124 |
125 | fun cancel() {
126 | visibility = INVISIBLE
127 | pathAnimator?.cancel()
128 | }
129 |
130 | private fun generateParticle(): IParticle {
131 | val shape = shapeList[Random.nextInt(shapeList.size)]
132 | return when (shape) {
133 | Shape.CIRCLE -> {
134 | val particle = CircleIParticle().also {
135 | it.configuration = ParticleConfiguration()
136 | it.configuration!!.shape = Shape.CIRCLE
137 | }
138 | particle
139 | }
140 | Shape.HOLLOW_CIRCLE -> {
141 | val particle = HollowCircleParticle().also {
142 | it.configuration = ParticleConfiguration()
143 | it.configuration!!.shape = Shape.HOLLOW_CIRCLE
144 | }
145 | particle
146 | }
147 | Shape.TRIANGLE -> {
148 | val particle = TriangleParticle().also {
149 | it.configuration = ParticleConfiguration()
150 | it.configuration!!.shape = Shape.TRIANGLE
151 | }
152 | particle
153 | }
154 | Shape.HOLLOW_TRIANGLE -> {
155 | val particle = HollowTriangleParticle().also {
156 | it.configuration = ParticleConfiguration()
157 | it.configuration!!.shape = Shape.HOLLOW_TRIANGLE
158 | }
159 | particle
160 | }
161 | Shape.RECTANGLE -> {
162 | val particle = RectangleParticle().also {
163 | it.configuration = ParticleConfiguration()
164 | it.configuration!!.shape = Shape.RECTANGLE
165 | }
166 | particle
167 | }
168 | Shape.HOLLOW_RECTANGLE -> {
169 | val particle = HollowRectangleParticle().also {
170 | it.configuration = ParticleConfiguration()
171 | it.configuration!!.shape = Shape.HOLLOW_RECTANGLE
172 | }
173 | particle
174 | }
175 | Shape.PENTACLE -> {
176 | val particle = PentacleParticle().also {
177 | it.configuration = ParticleConfiguration()
178 | it.configuration!!.shape = Shape.PENTACLE
179 | }
180 | particle
181 | }
182 | Shape.HOLLOW_PENTACLE -> {
183 | val particle = HollowPentacleParticle().also {
184 | it.configuration = ParticleConfiguration()
185 | it.configuration!!.shape = Shape.HOLLOW_PENTACLE
186 | }
187 | particle
188 | }
189 | Shape.BITMAP -> {
190 | val particle = BitmapParticle().also {
191 | it.configuration = ParticleConfiguration()
192 | it.configuration!!.shape = Shape.BITMAP
193 | }
194 | particle
195 | }
196 | }
197 | }
198 |
199 | private fun generateParticleList() {
200 | repeat(particleNum) {
201 | mParticles.add(generateParticle())
202 | }
203 | }
204 |
205 | private fun configureColor() {
206 | var cur = 0
207 | colorMap.onEachIndexed { index, entry ->
208 | val num = if (index == colorMap.size - 1) {
209 | particleNum - cur
210 | } else {
211 | (entry.value * particleNum).toInt()
212 | }
213 | for (i in cur until (cur + num)) {
214 | mParticles[i].configuration!!.color = entry.key
215 | }
216 | cur += num
217 | }
218 | }
219 |
220 | private fun configureAnim() {
221 | pathAnimator = ParticleAnimator(mParticles, anim)
222 | }
223 |
224 | private fun configureAnchor(particle: IParticle) {
225 | particle.initialX = anchorX
226 | particle.initialY = anchorY
227 | particle.x = anchorX
228 | particle.y = anchorY
229 | }
230 |
231 | private fun configureShimmer(particle: IParticle) {
232 | particle.configuration!!.shimmer = shimmer
233 | }
234 |
235 | private fun configureRotation(particle: IParticle) {
236 | particle.configuration!!.rotation = rotation
237 | }
238 |
239 | private fun configureRandomSize(particle: IParticle) {
240 | particle.configuration!!.width = Random.nextInt(widthSizeRange.first, widthSizeRange.last + 1)
241 | particle.configuration!!.height = Random.nextInt(heightSizeRange.first, heightSizeRange.last + 1)
242 |
243 | }
244 |
245 | private fun configureSize(particle: IParticle){
246 | particle.configuration!!.width = widthSize
247 | particle.configuration!!.height = heightSize
248 | }
249 |
250 | private fun configureRandomRadius(particle: IParticle) {
251 | particle.configuration!!.radius = Random.nextInt(radiusRange.first, radiusRange.last + 1).toFloat()
252 | }
253 |
254 | private fun configureRadius(particle: IParticle) {
255 | particle.configuration!!.radius = radius
256 | }
257 |
258 | private fun configureStrokeWidth(particle: IParticle) {
259 | particle.configuration!!.strokeWidth = strokeWidth
260 | }
261 |
262 | private fun configureBitmap(particle: IParticle) {
263 | particle.configuration!!.bitmap = bitmap
264 | }
265 |
266 | }
--------------------------------------------------------------------------------
/particlelib/src/test/java/com/bullfrog/particle/ExampleUnitTest.kt:
--------------------------------------------------------------------------------
1 | package com.bullfrog.particle
2 |
3 | import org.junit.Test
4 |
5 | import org.junit.Assert.*
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * See [testing documentation](http://d.android.com/tools/testing).
11 | */
12 | class ExampleUnitTest {
13 | @Test
14 | fun addition_isCorrect() {
15 | assertEquals(4, 2 + 2)
16 | }
17 | }
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':particlelib'
2 | include ':app'
3 | rootProject.name = "Particle"
--------------------------------------------------------------------------------
/一些比较有意思的公式.md:
--------------------------------------------------------------------------------
1 | ## 一些比较有意思的公式
2 |
3 | ### 中心向外旋转扩散
4 |
5 | ```kotlin
6 | val originalX = distance * progress
7 | val originalY = 0.01 * sqrt(originalX) * (originalX - distance)
8 | val x = originalX * cos - originalY * sin
9 | val y = originalX * sin + originalY * cos
10 | return Pair(x.toInt(), y.toInt())
11 | ```
12 |
13 | ### 中心向外扩散后往中心聚拢
14 |
15 | ```kotlin
16 | val originalX = distance * progress
17 | val originalY = 0.01 * originalX * (originalX - distance)
18 | val x = originalX * cos - originalY * sin
19 | val y = originalX * sin + originalY * cos
20 | return Pair(x.toInt(), y.toInt())
21 | ```
22 |
23 | ### 正弦弹性
24 |
25 | ```kotlin
26 | val originalX = distance * progress
27 | val originalY = 100 * sin(originalX / 50)
28 | val x = originalX * cos - originalY * sin
29 | val y = originalX * sin + originalY * cos
30 | return Pair(x.toInt(), y.toInt())
31 | ```
32 |
33 | ### 心形线
34 |
35 | ```kotlin
36 | val t = distance * progress
37 | val x = 16 * sin(t).pow(3)
38 | val y = 13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t)
39 | return Pair(30 * x.toInt(), 30 * y.toInt())
40 | ```
41 |
42 | ### 一些说不出名字的效果(重点在把 original 坐标和变换后坐标混用)
43 |
44 | #### 柱形
45 |
46 | ```kotlin
47 | val originalX = distance * progress
48 | val originalY = 100 * sin(originalX / 50)
49 | val x = originalX * cos - originalY * sin
50 | val y = originalX * sin + originalY * cos
51 | return Pair(originalY.toInt(), x.toInt())
52 | ```
53 |
54 | #### 波浪上浮
55 |
56 | ```kotlin
57 | val originalX = distance * progress
58 | val originalY = 100 * sin(originalX / 50)
59 | val x = originalX * cos - originalY * sin
60 | val y = originalX * sin + originalY * cos
61 | return Pair(y.toInt(), -originalX.toInt())
62 | ```
63 |
64 | #### 柱形展开
65 |
66 | ```kotlin
67 | val originalX = distance * progress
68 | val originalY = 100 * sin(originalX / 50)
69 | val x = originalX * cos - originalY * sin
70 | val y = originalX * sin + originalY * cos
71 | return Pair((0.008 * y * originalX).toInt(), (0.01 * x * originalY).toInt())
72 | ```
73 |
74 | #### 动感 DJ
75 |
76 | ```kotlin
77 | val originalX = distance * progress
78 | val originalY = 100 * sin(originalX / 50)
79 | val x = originalX * cos - originalY * sin
80 | val y = originalX * sin + originalY * cos
81 | return Pair((0.01 * x * originalY).toInt(), -(0.0001 * y.pow(2) * originalX).toInt())
82 | ```
83 |
84 | ### 以上基于随机 sin 和 cos 值。如果改为基于极坐标的 theta 正余弦值,同样有酷炫效果
85 |
86 | ### 以下是基于极坐标的公式:
87 |
88 | #### 圆圈中心向外螺旋扩散
89 |
90 | ```kotlin
91 | val originalX = distance * progress
92 | val originalY = 60
93 | val x = originalX * cos(theta) - originalY * sin(theta)
94 | val y = originalX * sin(theta) + originalY * cos(theta)
95 | return Pair(x.toInt(), y.toInt())
96 | ```
97 |
98 |
--------------------------------------------------------------------------------