├── .gitignore
├── .idea
├── .gitignore
├── .name
├── compiler.xml
├── gradle.xml
├── jarRepositories.xml
├── misc.xml
└── vcs.xml
├── LICENSE
├── Media
├── HowItWorks1.jpg
├── HowItWorks2.jpg
└── sample.gif
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── xyx
│ │ └── belvi
│ │ └── blurHashSample
│ │ └── ExampleInstrumentedTest.kt
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── xyx
│ │ │ └── belvi
│ │ │ └── blurHashSample
│ │ │ ├── MainActivity.kt
│ │ │ └── SampleResponse.kt
│ └── res
│ │ ├── drawable-v24
│ │ └── ic_launcher_foreground.xml
│ │ ├── drawable
│ │ └── ic_launcher_background.xml
│ │ ├── layout
│ │ ├── activity_main.xml
│ │ └── fields.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
│ │ ├── raw
│ │ └── sample.json
│ │ ├── values-night
│ │ └── themes.xml
│ │ └── values
│ │ ├── colors.xml
│ │ ├── strings.xml
│ │ └── themes.xml
│ └── test
│ └── java
│ └── xyx
│ └── belvi
│ └── blurHashSample
│ └── ExampleUnitTest.kt
├── blurHash
├── .gitignore
├── build.gradle
├── consumer-rules.pro
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── xyz
│ │ └── belvi
│ │ └── blurhash
│ │ └── ExampleInstrumentedTest.kt
│ ├── main
│ ├── AndroidManifest.xml
│ └── java
│ │ └── xyz
│ │ └── belvi
│ │ └── blurhash
│ │ ├── BlurHash.kt
│ │ ├── BlurHashDecoder.kt
│ │ └── BlurHashExt.kt
│ └── test
│ └── java
│ └── xyz
│ └── belvi
│ └── blurhash
│ └── ExampleUnitTest.kt
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/caches
5 | /.idea/libraries
6 | /.idea/modules.xml
7 | /.idea/workspace.xml
8 | /.idea/navEditor.xml
9 | /.idea/assetWizardSettings.xml
10 | .DS_Store
11 | /build
12 | /captures
13 | .externalNativeBuild
14 | .cxx
15 | local.properties
16 |
--------------------------------------------------------------------------------
/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /shelf/
3 | /workspace.xml
4 |
--------------------------------------------------------------------------------
/.idea/.name:
--------------------------------------------------------------------------------
1 | BlurHashSample
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
21 |
22 |
--------------------------------------------------------------------------------
/.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 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2020 Nosakhare Belvi
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/Media/HowItWorks1.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/KingsMentor/BlurHashExt/bd4ab454e941166475daf017bc3dee035cbd0652/Media/HowItWorks1.jpg
--------------------------------------------------------------------------------
/Media/HowItWorks2.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/KingsMentor/BlurHashExt/bd4ab454e941166475daf017bc3dee035cbd0652/Media/HowItWorks2.jpg
--------------------------------------------------------------------------------
/Media/sample.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/KingsMentor/BlurHashExt/bd4ab454e941166475daf017bc3dee035cbd0652/Media/sample.gif
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # BlurHashExt
2 |
3 | [  ](https://bintray.com/kingsmentor/maven/blurHash/_latestVersion)
4 |
5 | Kotlin extensions of [BlurHash](https://blurha.sh/) on Android ImageView, Glide, and Picasso.
6 |
7 | Based Blurhash implementation is from https://github.com/woltapp/blurhash.
8 |
9 | This implementation focus on optimizing BlurHash for Android development.
10 |
11 | 
12 |
13 |
14 |
15 | ### How BlurHash works?
16 |
17 |
18 | In short, BlurHash takes an image, and gives you a short string (only 20-30 characters!) that represents the placeholder for this image. You do this on the backend of your service, and store the string along with the image. When you send data to your client, you send both the URL to the image, and the BlurHash string. Your client then takes the string, and decodes it into an image that it shows while the real image is loading over the network. The string is short enough that it comfortably fits into whatever data format you use. For instance, it can easily be added as a field in a JSON object.
19 |
20 | BlurHashExt implements BlurHash Algorithm(request to generate bitmap from the blurHash and convert to drawable) using coroutine. This allows image processing to happen on the IO thread. Call `blurHash.clean()` to dispose any pending job operation and other cache bitmap. `BlurHash` uses LRU-Cache to cache drawable bitmap in memory. `lruSize` can be defined when initializing `BlurHash`.
21 |
22 | ### In summary:
23 |
24 |
25 |
26 | Want to know all the gory technical details? Read the [algorithm description](Algorithm.md).
27 |
28 |
29 | ### Download
30 |
31 | Gradle:
32 | ```gradle
33 | dependencies {
34 | implementation 'xyz.belvi.blurHash:blurHash:1.0.4'
35 | }
36 | ```
37 |
38 | ### Usage
39 |
40 | #### Step 1 - Initialize BlurHash
41 |
42 | `val blurHash: BlurHash = BlurHash(this, lruSize = 20, punch = 1F)`
43 |
44 | `lruSize` determines the number of blur drawable that will be cache in memory. The default size is 10
45 |
46 | #### Step 2 - Using BlurHash
47 |
48 | **With Glide**
49 | ```kotlin
50 | Glide.with(this).load(imgUrl)
51 | .blurPlaceHolder(blurHashString, imageView, blurHash)
52 | {
53 | requestBuilder ->
54 | requestBuilder.into(imageView)
55 | }
56 | ```
57 |
58 | or
59 |
60 | ```kotlin
61 | Glide.with(this).load(imgUrl)
62 | .blurPlaceHolder(blurHashString, width = 200, height= 200, blurHash = blurHash)
63 | {
64 | requestBuilder ->
65 | requestBuilder.into(imageView)
66 | }
67 | ```
68 |
69 |
70 | **With Picasso**
71 | ```kotlin
72 | Picasso.get().load(imgUrl)
73 | .blurPlaceHolder(blurHashString, imageView, blurHash)
74 | {
75 | request ->
76 | request.into(imageView)
77 | }
78 | ```
79 |
80 | or
81 |
82 | ```kotlin
83 | Picasso.get().load(imgUrl)
84 | .blurPlaceHolder(blurHashString, width = 200, height= 200, blurHash = blurHash)
85 | {
86 | request ->
87 | request.into(imageView)
88 | }
89 | ```
90 |
91 |
92 | **With Coil**
93 | ```kotlin
94 | val request = ImageRequest.Builder(context)
95 | .data("https://www.example.com/image.jpg")
96 | .target { drawable ->
97 | // Handle the result.
98 |
99 | }.blurPlaceHolder(blurHashString, imageView, blurHash = blurHash)
100 | {coilImageBuilder ->
101 | coilImageBuilder.build()
102 | }
103 | ```
104 |
105 | or
106 |
107 | ```kotlin
108 | imageView.load("https://www.example.com/image.jpg") {
109 | crossfade(true)
110 | blurPlaceHolder(blurHashString, imageView, blurHash = blurHash)
111 | {coilImageBuilder ->
112 | coilImageBuilder.build()
113 | }
114 | transformations(CircleCropTransformation())
115 | }
116 | ```
117 |
118 | **In an ImageView**
119 |
120 | This is useful for loading a placeholder before makeing a call to load the actual Image
121 | ```kotlin
122 | imageView.placeHolder(blurHashString, blurHash)
123 | {
124 | imageView.setImageURI(imgUrl)
125 | }
126 | ```
127 |
128 |
129 | **Just interested in getting the blurHash Drawabe ?**
130 |
131 | This is useful for getting blurHasdDrablw asynchronously.
132 |
133 | ```kotlin
134 | blurHashDrawable(blurHashString, imageView, blurHash)
135 | {
136 | drawable ->
137 | // do something with drawable or use whatver imageloading library you want. blurDrawable is ready to be used as error image or placeholder
138 | }
139 | ```
140 |
141 | or
142 |
143 | ```kotlin
144 | blurHashDrawable(blurHashString, width = 200, height = 200, blurHash)
145 | {
146 | drawable ->
147 | // do something with drawable or use whatver imageloading library you want. blurDrawable is ready to be used as error image or placeholder
148 | }
149 | ```
150 |
151 | #### Step 3 - Finally, Disposing
152 |
153 | On `onDestroy`, do not forget to clean cached bitmap and tell `BlurHash` to cancel any pending coroutine transaction. Here's how :
154 |
155 | ```kotlin
156 | override fun onDestroy() {
157 | super.onDestroy()
158 | blurHash.clean()
159 | }
160 | ```
161 |
162 | ### What is the `punch` parameter in some of these implementations?
163 |
164 | It is a parameter that adjusts the contrast on the decoded image. 1 means normal, smaller values will make the effect more subtle,
165 | and larger values will make it stronger. This is basically a design parameter, which lets you adjust the look.
166 |
167 | Technically, what it does is scale the AC components up or down.
168 |
169 |
170 | ## Contributing
171 |
172 | Contributions are welcomed even to [blurHash base library](https://github.com/woltapp/blurhash/)!
173 |
174 | You can also contribute by reporting issues or helping to resolve issues listed here [issue tracker](https://github.com/kingsmentor/blurhash/issues) or
175 | file a pull request.
176 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | plugins {
2 | id 'com.android.application'
3 | id 'kotlin-android'
4 | id 'kotlin-android-extensions'
5 | }
6 |
7 | android {
8 | compileSdkVersion 30
9 | buildToolsVersion "30.0.2"
10 |
11 | defaultConfig {
12 | applicationId "xyx.belvi.blurHashSample"
13 | minSdkVersion 16
14 | targetSdkVersion 30
15 | versionCode 1
16 | versionName "1.0"
17 |
18 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
19 | }
20 |
21 | buildTypes {
22 | release {
23 | minifyEnabled false
24 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
25 | }
26 | }
27 | compileOptions {
28 | sourceCompatibility JavaVersion.VERSION_1_8
29 | targetCompatibility JavaVersion.VERSION_1_8
30 | }
31 | kotlinOptions {
32 | jvmTarget = '1.8'
33 | }
34 | }
35 |
36 | dependencies {
37 |
38 | implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
39 | implementation 'com.google.code.gson:gson:2.8.6'
40 | implementation 'androidx.core:core-ktx:1.3.2'
41 | implementation 'androidx.appcompat:appcompat:1.2.0'
42 | implementation 'com.google.android.material:material:1.2.1'
43 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
44 | implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.7'
45 | implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.4"
46 |
47 |
48 | implementation 'io.coil-kt:coil:1.0.0'
49 | implementation 'com.squareup.picasso:picasso:2.71828'
50 | implementation 'com.github.bumptech.glide:glide:4.11.0'
51 |
52 | annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
53 | implementation project(path: ':blurHash')
54 | testImplementation 'junit:junit:4.+'
55 | androidTestImplementation 'androidx.test.ext:junit:1.1.2'
56 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
57 | }
--------------------------------------------------------------------------------
/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/xyx/belvi/blurHashSample/ExampleInstrumentedTest.kt:
--------------------------------------------------------------------------------
1 | package xyx.belvi.blurHashSample
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("xyx.belvi", appContext.packageName)
23 | }
24 | }
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/app/src/main/java/xyx/belvi/blurHashSample/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package xyx.belvi.blurHashSample
2 |
3 | import android.os.Bundle
4 | import android.util.Log
5 | import android.view.LayoutInflater
6 | import android.view.View
7 | import android.view.ViewGroup
8 | import androidx.appcompat.app.AppCompatActivity
9 | import androidx.recyclerview.widget.LinearLayoutManager
10 | import androidx.recyclerview.widget.RecyclerView
11 | import coil.ImageLoader
12 | import coil.load
13 | import coil.request.ImageRequest
14 | import com.google.gson.Gson
15 | import com.google.gson.reflect.TypeToken
16 | import kotlinx.android.synthetic.main.activity_main.*
17 | import kotlinx.android.synthetic.main.fields.view.*
18 | import xyz.belvi.blurhash.BlurHash
19 | import xyz.belvi.blurhash.blurHashDrawable
20 | import xyz.belvi.blurhash.blurPlaceHolder
21 | import java.lang.Exception
22 |
23 | class MainActivity : AppCompatActivity() {
24 |
25 | val blurHash: BlurHash = BlurHash(this, lruSize = 20)
26 | override fun onCreate(savedInstanceState: Bundle?) {
27 | super.onCreate(savedInstanceState)
28 | setContentView(R.layout.activity_main)
29 | recyclerview.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
30 | recyclerview.adapter = SampleRecyclerAdapter(sampleResponse())
31 | }
32 |
33 | private fun sampleResponse(): List {
34 | val response = resources.openRawResource(R.raw.sample).bufferedReader()
35 | .use { it.readText() }
36 | return Gson().fromJson(response, object : TypeToken>() {}.type)
37 | }
38 |
39 | inner class SampleRecyclerAdapter(private val response: List) :
40 | RecyclerView.Adapter() {
41 | override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SampleRecyclerHolder {
42 | return SampleRecyclerHolder(
43 | LayoutInflater.from(parent.context).inflate(R.layout.fields, parent, false)
44 | )
45 | }
46 |
47 | override fun onBindViewHolder(holder: SampleRecyclerHolder, position: Int) {
48 | holder.bindView(response[position])
49 | }
50 |
51 | override fun getItemCount(): Int {
52 | return response.size
53 | }
54 | }
55 |
56 | inner class SampleRecyclerHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
57 | fun bindView(sampleResponse: SampleResponse) {
58 | with(itemView) {
59 | textView.text = sampleResponse.blur
60 | ImageRequest.Builder(context)
61 | .data("https://www.example.com/image.jpg")
62 | .target { drawable ->
63 | // Handle the result.
64 | }
65 | .placeholder(R.drawable.ic_launcher_background)
66 | .error(R.drawable.ic_launcher_background)
67 | .build()
68 |
69 | }
70 | }
71 | }
72 |
73 | override fun onDestroy() {
74 | super.onDestroy()
75 | blurHash.clean()
76 | }
77 | }
--------------------------------------------------------------------------------
/app/src/main/java/xyx/belvi/blurHashSample/SampleResponse.kt:
--------------------------------------------------------------------------------
1 | package xyx.belvi.blurHashSample
2 |
3 | data class SampleResponse(val blur: String, val img: String)
--------------------------------------------------------------------------------
/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/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/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fields.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
19 |
20 |
32 |
--------------------------------------------------------------------------------
/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/KingsMentor/BlurHashExt/bd4ab454e941166475daf017bc3dee035cbd0652/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/KingsMentor/BlurHashExt/bd4ab454e941166475daf017bc3dee035cbd0652/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/KingsMentor/BlurHashExt/bd4ab454e941166475daf017bc3dee035cbd0652/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/KingsMentor/BlurHashExt/bd4ab454e941166475daf017bc3dee035cbd0652/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/KingsMentor/BlurHashExt/bd4ab454e941166475daf017bc3dee035cbd0652/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/KingsMentor/BlurHashExt/bd4ab454e941166475daf017bc3dee035cbd0652/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/KingsMentor/BlurHashExt/bd4ab454e941166475daf017bc3dee035cbd0652/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/KingsMentor/BlurHashExt/bd4ab454e941166475daf017bc3dee035cbd0652/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/KingsMentor/BlurHashExt/bd4ab454e941166475daf017bc3dee035cbd0652/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/KingsMentor/BlurHashExt/bd4ab454e941166475daf017bc3dee035cbd0652/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/raw/sample.json:
--------------------------------------------------------------------------------
1 | [
2 | {
3 | "blur": "LEHV6nWB2yk8pyoJadR*.7kCMdnj",
4 | "img": "https://blurha.sh/assets/images/img1.jpg"
5 | },
6 | {
7 | "blur": "LGF5]+Yk^6#M@-5c,1J5@[or[Q6.",
8 | "img": "https://blurha.sh/assets/images/img2.jpg"
9 | },
10 | {
11 | "blur": "L6Pj0^i_.AyE_3t7t7R**0o#DgR4",
12 | "img": "https://blurha.sh/assets/images/img3.jpg"
13 | },
14 | {
15 | "blur": "LKO2?U%2Tw=w]~RBVZRi};RPxuwH",
16 | "img": "https://blurha.sh/assets/images/img4.jpg"
17 | },
18 | {
19 | "blur": "LEHV6nWB2yk8pyoJadR*.7kCMdnj",
20 | "img": "https://blurha.sh/assets/images/img1.jpg"
21 | },
22 | {
23 | "blur": "LGF5]+Yk^6#M@-5c,1J5@[or[Q6.",
24 | "img": "https://blurha.sh/assets/images/img2.jpg"
25 | },
26 | {
27 | "blur": "L6Pj0^i_.AyE_3t7t7R**0o#DgR4",
28 | "img": "https://blurha.sh/assets/images/img3.jpg"
29 | },
30 | {
31 | "blur": "LKO2?U%2Tw=w]~RBVZRi};RPxuwH",
32 | "img": "https://blurha.sh/assets/images/img4.jpg"
33 | },
34 | {
35 | "blur": "LEHV6nWB2yk8pyoJadR*.7kCMdnj",
36 | "img": "https://blurha.sh/assets/images/img1.jpg"
37 | },
38 | {
39 | "blur": "LGF5]+Yk^6#M@-5c,1J5@[or[Q6.",
40 | "img": "https://blurha.sh/assets/images/img2.jpg"
41 | },
42 | {
43 | "blur": "L6Pj0^i_.AyE_3t7t7R**0o#DgR4",
44 | "img": "https://blurha.sh/assets/images/img3.jpg"
45 | },
46 | {
47 | "blur": "LKO2?U%2Tw=w]~RBVZRi};RPxuwH",
48 | "img": "https://blurha.sh/assets/images/img4.jpg"
49 | },
50 | {
51 | "blur": "LEHV6nWB2yk8pyoJadR*.7kCMdnj",
52 | "img": "https://blurha.sh/assets/images/img1.jpg"
53 | },
54 | {
55 | "blur": "LGF5]+Yk^6#M@-5c,1J5@[or[Q6.",
56 | "img": "https://blurha.sh/assets/images/img2.jpg"
57 | },
58 | {
59 | "blur": "L6Pj0^i_.AyE_3t7t7R**0o#DgR4",
60 | "img": "https://blurha.sh/assets/images/img3.jpg"
61 | },
62 | {
63 | "blur": "LKO2?U%2Tw=w]~RBVZRi};RPxuwH",
64 | "img": "https://blurha.sh/assets/images/img4.jpg"
65 | },
66 | {
67 | "blur": "LEHV6nWB2yk8pyoJadR*.7kCMdnj",
68 | "img": "https://blurha.sh/assets/images/img1.jpg"
69 | },
70 | {
71 | "blur": "LGF5]+Yk^6#M@-5c,1J5@[or[Q6.",
72 | "img": "https://blurha.sh/assets/images/img2.jpg"
73 | },
74 | {
75 | "blur": "L6Pj0^i_.AyE_3t7t7R**0o#DgR4",
76 | "img": "https://blurha.sh/assets/images/img3.jpg"
77 | },
78 | {
79 | "blur": "LKO2?U%2Tw=w]~RBVZRi};RPxuwH",
80 | "img": "https://blurha.sh/assets/images/img4.jpg"
81 | }
82 | ]
--------------------------------------------------------------------------------
/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 | BlurHashSample
3 |
--------------------------------------------------------------------------------
/app/src/main/res/values/themes.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
16 |
--------------------------------------------------------------------------------
/app/src/test/java/xyx/belvi/blurHashSample/ExampleUnitTest.kt:
--------------------------------------------------------------------------------
1 | package xyx.belvi.blurHashSample
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 | }
--------------------------------------------------------------------------------
/blurHash/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/blurHash/build.gradle:
--------------------------------------------------------------------------------
1 | plugins {
2 | id 'com.android.library'
3 | id 'kotlin-android'
4 | }
5 |
6 | android {
7 | compileSdkVersion 30
8 | buildToolsVersion "30.0.2"
9 |
10 | defaultConfig {
11 | minSdkVersion 16
12 | targetSdkVersion 30
13 | versionCode 1
14 | versionName "1.0"
15 |
16 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
17 | consumerProguardFiles "consumer-rules.pro"
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 |
42 | implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.7'
43 | implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.4"
44 |
45 | implementation 'com.squareup.picasso:picasso:2.71828'
46 | implementation 'com.github.bumptech.glide:glide:4.11.0'
47 | implementation 'io.coil-kt:coil:1.0.0'
48 | annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
49 |
50 | testImplementation 'junit:junit:4.+'
51 | androidTestImplementation 'androidx.test.ext:junit:1.1.2'
52 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
53 | }
54 |
55 |
56 | ext {
57 | bintrayRepo = 'maven'
58 | bintrayName = 'blurHash'
59 |
60 | publishedGroupId = 'xyz.belvi.blurHash'
61 | libraryName = 'blurHash'
62 | artifact = 'blurHash'
63 |
64 | libraryDescription = 'Kotlin extensions of BlurHash for ImageView, Glide and Piccasso optimized for Android.'
65 |
66 | siteUrl = 'https://github.com/KingsMentor/BlurHashExt'
67 | gitUrl = 'https://github.com/KingsMentor/BlurHashExt'
68 |
69 | libraryVersion = '1.0.3'
70 |
71 | developerId = 'kingsmentor'
72 | developerName = 'Nosakhare Belvi'
73 | developerEmail = 'nosakharebelvi@gmail.com'
74 |
75 | licenseName = 'MIT License'
76 | licenseUrl = 'https://github.com/KingsMentor/BlurHashExt/blob/master/LICENSE'
77 | allLicenses = ["MIT License"]
78 | }
79 | apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/installv1.gradle'
80 | apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/bintrayv1.gradle'
--------------------------------------------------------------------------------
/blurHash/consumer-rules.pro:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/KingsMentor/BlurHashExt/bd4ab454e941166475daf017bc3dee035cbd0652/blurHash/consumer-rules.pro
--------------------------------------------------------------------------------
/blurHash/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
--------------------------------------------------------------------------------
/blurHash/src/androidTest/java/xyz/belvi/blurhash/ExampleInstrumentedTest.kt:
--------------------------------------------------------------------------------
1 | package xyz.belvi.blurhash
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("xyz.belvi.blurhash.test", appContext.packageName)
23 | }
24 | }
--------------------------------------------------------------------------------
/blurHash/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
--------------------------------------------------------------------------------
/blurHash/src/main/java/xyz/belvi/blurhash/BlurHash.kt:
--------------------------------------------------------------------------------
1 | package xyz.belvi.blurhash
2 |
3 | import android.content.Context
4 | import android.graphics.drawable.BitmapDrawable
5 | import android.util.LruCache
6 | import kotlinx.coroutines.CoroutineScope
7 | import kotlinx.coroutines.Dispatchers
8 | import kotlinx.coroutines.cancel
9 | import kotlinx.coroutines.launch
10 | import kotlinx.coroutines.withContext
11 |
12 | const val LUR_SIZE = 10
13 |
14 | class BlurHash(
15 | private var context: Context,
16 | lruSize: Int = LUR_SIZE,
17 | private var punch: Float = 1F
18 | ) {
19 | private var data: LruCache = LruCache(lruSize)
20 | private val scope = CoroutineScope(Dispatchers.Main)
21 |
22 | fun clean() {
23 | scope.cancel()
24 | data.evictAll()
25 | }
26 |
27 | fun execute(
28 | blurString: String,
29 | width: Int,
30 | height: Int,
31 | response: (drawable: BitmapDrawable) -> Unit
32 | ) {
33 | scope.launch {
34 | var blurBitmap = getBlurDrawable(blurString)
35 | withContext(Dispatchers.IO) {
36 | blurBitmap ?: run {
37 | val bitmap = BlurHashDecoder.decode(
38 | blurString,
39 | width,
40 | height,
41 | punch,
42 | useCache = false
43 | )
44 | blurBitmap = BitmapDrawable(
45 | context.resources,
46 | bitmap
47 | )
48 | cache(blurString, blurBitmap!!)
49 | }
50 | }
51 | response(blurBitmap!!)
52 | }
53 | }
54 |
55 | private fun cache(blurString: String, drawable: BitmapDrawable) {
56 | data.put(blurString, drawable)
57 | }
58 |
59 | private fun getBlurDrawable(blurString: String): BitmapDrawable? {
60 | return data.get(blurString)
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/blurHash/src/main/java/xyz/belvi/blurhash/BlurHashDecoder.kt:
--------------------------------------------------------------------------------
1 | package xyz.belvi.blurhash
2 |
3 | import android.graphics.Bitmap
4 | import android.graphics.Color
5 | import kotlin.math.cos
6 | import kotlin.math.pow
7 | import kotlin.math.withSign
8 |
9 | object BlurHashDecoder {
10 |
11 | // cache Math.cos() calculations to improve performance.
12 | // The number of calculations can be huge for many bitmaps: width * height * numCompX * numCompY * 2 * nBitmaps
13 | // the cache is enabled by default, it is recommended to disable it only when just a few images are displayed
14 | private val cacheCosinesX = HashMap()
15 | private val cacheCosinesY = HashMap()
16 |
17 | /**
18 | * Clear calculations stored in memory cache.
19 | * The cache is not big, but will increase when many image sizes are used,
20 | * if the app needs memory it is recommended to clear it.
21 | */
22 | fun clearCache() {
23 | cacheCosinesX.clear()
24 | cacheCosinesY.clear()
25 | }
26 |
27 | /**
28 | * Decode a blur hash into a new bitmap.
29 | *
30 | * @param useCache use in memory cache for the calculated math, reused by images with same size.
31 | * if the cache does not exist yet it will be created and populated with new calculations.
32 | * By default it is true.
33 | */
34 | suspend fun decode(
35 | blurHash: String?,
36 | width: Int,
37 | height: Int,
38 | punch: Float = 1f,
39 | useCache: Boolean = true
40 | ): Bitmap? {
41 | if (blurHash == null || blurHash.length < 6) {
42 | return null
43 | }
44 | val numCompEnc = decode83(blurHash, 0, 1)
45 | val numCompX = (numCompEnc % 9) + 1
46 | val numCompY = (numCompEnc / 9) + 1
47 | if (blurHash.length != 4 + 2 * numCompX * numCompY) {
48 | return null
49 | }
50 | val maxAcEnc = decode83(blurHash, 1, 2)
51 | val maxAc = (maxAcEnc + 1) / 166f
52 | val colors = Array(numCompX * numCompY) { i ->
53 | if (i == 0) {
54 | val colorEnc = decode83(blurHash, 2, 6)
55 | decodeDc(colorEnc)
56 | } else {
57 | val from = 4 + i * 2
58 | val colorEnc = decode83(blurHash, from, from + 2)
59 | decodeAc(colorEnc, maxAc * punch)
60 | }
61 | }
62 | return composeBitmap(width, height, numCompX, numCompY, colors, useCache)
63 | }
64 |
65 | private fun decode83(str: String, from: Int = 0, to: Int = str.length): Int {
66 | var result = 0
67 | for (i in from until to) {
68 | val index = charMap[str[i]] ?: -1
69 | if (index != -1) {
70 | result = result * 83 + index
71 | }
72 | }
73 | return result
74 | }
75 |
76 | private fun decodeDc(colorEnc: Int): FloatArray {
77 | val r = colorEnc shr 16
78 | val g = (colorEnc shr 8) and 255
79 | val b = colorEnc and 255
80 | return floatArrayOf(srgbToLinear(r), srgbToLinear(g), srgbToLinear(b))
81 | }
82 |
83 | private fun srgbToLinear(colorEnc: Int): Float {
84 | val v = colorEnc / 255f
85 | return if (v <= 0.04045f) {
86 | (v / 12.92f)
87 | } else {
88 | ((v + 0.055f) / 1.055f).pow(2.4f)
89 | }
90 | }
91 |
92 | private fun decodeAc(value: Int, maxAc: Float): FloatArray {
93 | val r = value / (19 * 19)
94 | val g = (value / 19) % 19
95 | val b = value % 19
96 | return floatArrayOf(
97 | signedPow2((r - 9) / 9.0f) * maxAc,
98 | signedPow2((g - 9) / 9.0f) * maxAc,
99 | signedPow2((b - 9) / 9.0f) * maxAc
100 | )
101 | }
102 |
103 | private fun signedPow2(value: Float) = value.pow(2f).withSign(value)
104 |
105 | private fun composeBitmap(
106 | width: Int, height: Int,
107 | numCompX: Int, numCompY: Int,
108 | colors: Array,
109 | useCache: Boolean
110 | ): Bitmap {
111 | // use an array for better performance when writing pixel colors
112 | val imageArray = IntArray(width * height)
113 | val calculateCosX = !useCache || !cacheCosinesX.containsKey(width * numCompX)
114 | val cosinesX = getArrayForCosinesX(calculateCosX, width, numCompX)
115 | val calculateCosY = !useCache || !cacheCosinesY.containsKey(height * numCompY)
116 | val cosinesY = getArrayForCosinesY(calculateCosY, height, numCompY)
117 | for (y in 0 until height) {
118 | for (x in 0 until width) {
119 | var r = 0f
120 | var g = 0f
121 | var b = 0f
122 | for (j in 0 until numCompY) {
123 | for (i in 0 until numCompX) {
124 | val cosX = cosinesX.getCos(calculateCosX, i, numCompX, x, width)
125 | val cosY = cosinesY.getCos(calculateCosY, j, numCompY, y, height)
126 | val basis = (cosX * cosY).toFloat()
127 | val color = colors[j * numCompX + i]
128 | r += color[0] * basis
129 | g += color[1] * basis
130 | b += color[2] * basis
131 | }
132 | }
133 | imageArray[x + width * y] =
134 | Color.rgb(linearToSrgb(r), linearToSrgb(g), linearToSrgb(b))
135 | }
136 | }
137 | return Bitmap.createBitmap(imageArray, width, height, Bitmap.Config.ARGB_8888)
138 | }
139 |
140 | private fun getArrayForCosinesY(calculate: Boolean, height: Int, numCompY: Int) = when {
141 | calculate -> {
142 | DoubleArray(height * numCompY).also {
143 | cacheCosinesY[height * numCompY] = it
144 | }
145 | }
146 | else -> {
147 | cacheCosinesY[height * numCompY]!!
148 | }
149 | }
150 |
151 | private fun getArrayForCosinesX(calculate: Boolean, width: Int, numCompX: Int) = when {
152 | calculate -> {
153 | DoubleArray(width * numCompX).also {
154 | cacheCosinesX[width * numCompX] = it
155 | }
156 | }
157 | else -> cacheCosinesX[width * numCompX]!!
158 | }
159 |
160 | private fun DoubleArray.getCos(
161 | calculate: Boolean,
162 | x: Int,
163 | numComp: Int,
164 | y: Int,
165 | size: Int
166 | ): Double {
167 | if (calculate) {
168 | this[x + numComp * y] = cos(Math.PI * y * x / size)
169 | }
170 | return this[x + numComp * y]
171 | }
172 |
173 | private fun linearToSrgb(value: Float): Int {
174 | val v = value.coerceIn(0f, 1f)
175 | return if (v <= 0.0031308f) {
176 | (v * 12.92f * 255f + 0.5f).toInt()
177 | } else {
178 | ((1.055f * v.pow(1 / 2.4f) - 0.055f) * 255 + 0.5f).toInt()
179 | }
180 | }
181 |
182 | private val charMap = listOf(
183 | '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
184 | 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
185 | 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
186 | 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '#', '$', '%', '*', '+', ',',
187 | '-', '.', ':', ';', '=', '?', '@', '[', ']', '^', '_', '{', '|', '}', '~'
188 | )
189 | .mapIndexed { i, c -> c to i }
190 | .toMap()
191 | }
--------------------------------------------------------------------------------
/blurHash/src/main/java/xyz/belvi/blurhash/BlurHashExt.kt:
--------------------------------------------------------------------------------
1 | package xyz.belvi.blurhash
2 |
3 | import android.annotation.SuppressLint
4 | import android.graphics.drawable.Drawable
5 | import android.view.View
6 | import android.widget.ImageView
7 | import coil.request.ImageRequest
8 | import com.bumptech.glide.RequestBuilder
9 | import com.bumptech.glide.request.RequestOptions
10 | import com.squareup.picasso.RequestCreator
11 |
12 | // FOR GLIDE
13 | fun RequestBuilder.blurPlaceHolder(
14 | blurString: String,
15 | width: Int = 0,
16 | height: Int = 0,
17 | blurHash: BlurHash,
18 | response: (requestBuilder: RequestBuilder) -> Unit
19 | ) {
20 | if (width != 0 && height != 0) {
21 | blurHash.execute(blurString, width, height) { drawable ->
22 | this@blurPlaceHolder.placeholder(drawable)
23 | response(this@blurPlaceHolder)
24 | }
25 | }
26 | }
27 |
28 | fun RequestBuilder.blurPlaceHolder(
29 | blurString: String,
30 | targetView: View,
31 | blurHash: BlurHash,
32 | response: (requestBuilder: RequestBuilder) -> Unit
33 | ) {
34 | targetView.post {
35 | blurPlaceHolder(blurString, targetView.width, targetView.height, blurHash, response)
36 | }
37 | }
38 |
39 | fun RequestOptions.blurPlaceHolderOf(
40 | blurString: String,
41 | width: Int = 0,
42 | height: Int = 0,
43 | blurHash: BlurHash,
44 | response: (requestOptions: RequestOptions) -> Unit
45 | ) {
46 | if (width != 0 && height != 0) {
47 | blurHash.execute(blurString, width, height) { drawable ->
48 | this@blurPlaceHolderOf.placeholder(drawable)
49 | response(this@blurPlaceHolderOf)
50 | }
51 | }
52 | }
53 |
54 | fun RequestOptions.blurPlaceHolderOf(
55 | blurString: String,
56 | targetView: View,
57 | blurHash: BlurHash,
58 | response: (requestOptions: RequestOptions) -> Unit
59 | ) {
60 | targetView.post {
61 | blurPlaceHolderOf(blurString, targetView.width, targetView.height, blurHash, response)
62 | }
63 | }
64 |
65 | // FOR COIL
66 |
67 | fun ImageRequest.Builder.blurPlaceHolder(
68 | blurString: String,
69 | width: Int = 0,
70 | height: Int = 0,
71 | blurHash: BlurHash,
72 | response: (requestBuilder: ImageRequest.Builder) -> Unit
73 | ) {
74 | if (width != 0 && height != 0) {
75 | blurHash.execute(blurString, width, height) { drawable ->
76 | this@blurPlaceHolder.placeholder(drawable)
77 | response(this@blurPlaceHolder)
78 | }
79 | }
80 | }
81 |
82 | @SuppressLint("CheckResult")
83 | fun ImageRequest.Builder.blurPlaceHolder(
84 | blurString: String,
85 | targetView: View,
86 | blurHash: BlurHash,
87 | response: (requestBuilder: ImageRequest.Builder) -> Unit
88 | ) {
89 | targetView.post {
90 | blurPlaceHolder(blurString, targetView.width, targetView.height, blurHash, response)
91 | }
92 | }
93 |
94 | // FOR PICASSO
95 | fun RequestCreator.blurPlaceHolder(
96 | blurString: String,
97 | width: Int = 0,
98 | height: Int = 0,
99 | blurHash: BlurHash,
100 | response: (requestBuilder: RequestCreator) -> Unit
101 | ) {
102 | if (width != 0 && height != 0) {
103 | blurHash.execute(blurString, width, height) { drawable ->
104 | this@blurPlaceHolder.placeholder(drawable)
105 | response(this@blurPlaceHolder)
106 | }
107 | }
108 | }
109 |
110 | @SuppressLint("CheckResult")
111 | fun RequestCreator.blurPlaceHolder(
112 | blurString: String,
113 | targetView: View,
114 | blurHash: BlurHash,
115 | response: (requestBuilder: RequestCreator) -> Unit
116 | ) {
117 | targetView.post {
118 | blurPlaceHolder(blurString, targetView.width, targetView.height, blurHash, response)
119 | }
120 | }
121 |
122 | // FOR IMAGEVIEW
123 |
124 | fun ImageView.blurPlaceHolder(
125 | blurString: String,
126 | blurHash: BlurHash,
127 | response: (drawable: Drawable) -> Unit
128 | ) {
129 | this.post {
130 | if (width != 0 && height != 0) {
131 | blurHash.execute(blurString, width, height) { drawable ->
132 | if (getDrawable() != null)
133 | setImageDrawable(drawable)
134 | response(drawable)
135 | }
136 | }
137 | }
138 | }
139 |
140 | // GENERIC USAGE
141 | fun blurHashDrawable(
142 | blurString: String,
143 | targetView: View,
144 | blurHash: BlurHash,
145 | response: (drawable: Drawable) -> Unit
146 | ) {
147 | targetView.post {
148 | blurHashDrawable(blurString, targetView.width, targetView.height, blurHash, response)
149 | }
150 | }
151 |
152 | fun blurHashDrawable(
153 | blurString: String,
154 | width: Int = 0,
155 | height: Int = 0,
156 | blurHash: BlurHash,
157 | response: (drawable: Drawable) -> Unit
158 | ) {
159 | if (width != 0 && height != 0) {
160 | blurHash.execute(blurString, width, height) { drawable ->
161 | response(drawable)
162 | }
163 | }
164 | }
--------------------------------------------------------------------------------
/blurHash/src/test/java/xyz/belvi/blurhash/ExampleUnitTest.kt:
--------------------------------------------------------------------------------
1 | package xyz.belvi.blurhash
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.10"
4 | repositories {
5 | google()
6 | jcenter()
7 | }
8 | dependencies {
9 | classpath "com.android.tools.build:gradle:4.1.0"
10 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
11 | classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
12 | classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0'
13 |
14 | // NOTE: Do not place your application dependencies here; they belong
15 | // in the individual module build.gradle files
16 | }
17 | }
18 |
19 | allprojects {
20 | repositories {
21 | google()
22 | jcenter()
23 | }
24 | tasks.withType(Javadoc).all {
25 | enabled = false
26 | }
27 | }
28 |
29 | task clean(type: Delete) {
30 | delete rootProject.buildDir
31 | }
--------------------------------------------------------------------------------
/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/KingsMentor/BlurHashExt/bd4ab454e941166475daf017bc3dee035cbd0652/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Sat Oct 17 20:48:01 EDT 2020
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-all.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 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':blurHash'
2 | include ':app'
3 | rootProject.name = "BlurHashSample"
--------------------------------------------------------------------------------