diff --git a/Kotlin/demo/build.gradle b/Kotlin/demo/build.gradle index 6ccb0378..a3d2c78a 100644 --- a/Kotlin/demo/build.gradle +++ b/Kotlin/demo/build.gradle @@ -26,4 +26,5 @@ android { dependencies { implementation project(path: ':lib') implementation 'androidx.appcompat:appcompat:1.1.0' + implementation 'androidx.constraintlayout:constraintlayout:1.1.3' } diff --git a/Kotlin/demo/src/main/java/com/wolt/blurhashapp/MainActivity.kt b/Kotlin/demo/src/main/java/com/wolt/blurhashapp/MainActivity.kt index 274b058a..14d6cdeb 100644 --- a/Kotlin/demo/src/main/java/com/wolt/blurhashapp/MainActivity.kt +++ b/Kotlin/demo/src/main/java/com/wolt/blurhashapp/MainActivity.kt @@ -1,24 +1,53 @@ package com.wolt.blurhashapp +import android.graphics.Bitmap +import android.graphics.Canvas +import android.graphics.drawable.BitmapDrawable +import android.graphics.drawable.Drawable import android.os.Bundle import android.view.View -import android.widget.EditText -import android.widget.ImageView -import android.widget.TextView import androidx.appcompat.app.AppCompatActivity +import androidx.core.content.ContextCompat import com.wolt.blurhashkt.BlurHashDecoder +import com.wolt.blurhashkt.BlurHashEncoder +import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) - val etInput: EditText = findViewById(R.id.etInput) - val ivResult: ImageView = findViewById(R.id.ivResult) - findViewById(R.id.tvDecode).setOnClickListener { + + btnDecode.setOnClickListener { val bitmap = BlurHashDecoder.decode(etInput.text.toString(), 20, 12) ivResult.setImageBitmap(bitmap) } + + val buttons = listOf(btnEncode1, btnEncode2, btnEncode3, btnEncode4, btnEncode5) + val drawableResList = listOf(R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5) + val onClickListener = View.OnClickListener { + val bitmap = drawableToBitmap(ContextCompat.getDrawable(this, drawableResList[buttons.indexOf(it)])!!) + etInput.setText(BlurHashEncoder.encode(bitmap)) + } + for (button in buttons) { + button.setOnClickListener(onClickListener) + } + } + + private fun drawableToBitmap(drawable: Drawable): Bitmap { + return drawableToBitmap(drawable, drawable.intrinsicWidth, + drawable.intrinsicHeight) + } + + private fun drawableToBitmap(drawable: Drawable, w: Int, h: Int): Bitmap { + if (drawable is BitmapDrawable) { + return drawable.bitmap + } + val bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888) + val canvas = Canvas(bitmap) + drawable.setBounds(0, 0, canvas.width, canvas.height) + drawable.draw(canvas) + return bitmap } } diff --git a/Kotlin/demo/src/main/res/drawable-nodpi/img1.jpg b/Kotlin/demo/src/main/res/drawable-nodpi/img1.jpg new file mode 100644 index 00000000..0e487799 Binary files /dev/null and b/Kotlin/demo/src/main/res/drawable-nodpi/img1.jpg differ diff --git a/Kotlin/demo/src/main/res/drawable-nodpi/img2.jpg b/Kotlin/demo/src/main/res/drawable-nodpi/img2.jpg new file mode 100644 index 00000000..9eb5e434 Binary files /dev/null and b/Kotlin/demo/src/main/res/drawable-nodpi/img2.jpg differ diff --git a/Kotlin/demo/src/main/res/drawable-nodpi/img3.jpg b/Kotlin/demo/src/main/res/drawable-nodpi/img3.jpg new file mode 100644 index 00000000..e5181983 Binary files /dev/null and b/Kotlin/demo/src/main/res/drawable-nodpi/img3.jpg differ diff --git a/Kotlin/demo/src/main/res/drawable-nodpi/img4.jpg b/Kotlin/demo/src/main/res/drawable-nodpi/img4.jpg new file mode 100644 index 00000000..e3f5b2aa Binary files /dev/null and b/Kotlin/demo/src/main/res/drawable-nodpi/img4.jpg differ diff --git a/Kotlin/demo/src/main/res/drawable-nodpi/img5.jpg b/Kotlin/demo/src/main/res/drawable-nodpi/img5.jpg new file mode 100644 index 00000000..39f3f0f6 Binary files /dev/null and b/Kotlin/demo/src/main/res/drawable-nodpi/img5.jpg differ diff --git a/Kotlin/demo/src/main/res/layout/activity_main.xml b/Kotlin/demo/src/main/res/layout/activity_main.xml index 84249e62..e62629cd 100644 --- a/Kotlin/demo/src/main/res/layout/activity_main.xml +++ b/Kotlin/demo/src/main/res/layout/activity_main.xml @@ -1,43 +1,116 @@ - + android:maxLines="1" + android:text="@string/demo_blurhash_string" + android:textColor="@color/textColor" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="parent" /> - + android:textColor="@color/textColor" + android:textSize="16sp" + app:layout_constraintEnd_toStartOf="@id/btnEncode1" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toBottomOf="@id/etInput" /> + +