Skip to content

Commit

Permalink
upgrade album gallery, clean strings.xml
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethosa committed Mar 16, 2022
1 parent 0b22b4f commit a9f8d02
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 28 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ android {
minSdk 21
targetSdk 32
versionCode 5
versionName "0.3.1"
versionName "0.4.0"
ndk {
abiFilters 'armeabi-v7a','arm64-v8a','x86','x86_64'
}
Expand Down
99 changes: 76 additions & 23 deletions app/src/main/java/com/ethosa/ktc/ui/adapters/AlbumAdapter.kt
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package com.ethosa.ktc.ui.adapters

import android.animation.ObjectAnimator
import android.animation.PropertyValuesHolder
import android.annotation.SuppressLint
import android.app.Dialog
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Build
import android.util.DisplayMetrics
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.view.*
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.ethosa.ktc.R
import com.ethosa.ktc.databinding.LayoutAlbumImageBinding
import kotlin.math.abs


/**
* Provides RecyclerView.Adapter behavior for album photos.
Expand All @@ -28,8 +29,19 @@ class AlbumAdapter(

private val dialog = Dialog(activity)
private var animator = ObjectAnimator()
private var animX = PropertyValuesHolder.ofFloat("x", 0f)
private var animY = PropertyValuesHolder.ofFloat("y", 0f)
private var img: ImageView? = null
private var root: ConstraintLayout? = null
private var pos: Int = -1

companion object {
private const val MAX_DIM_AMOUNT = 0.95f
private const val HIDE_OFFSET = 400
private const val CHANGE_IMAGE_OFFSET = 200
private const val Y_PRIORITY = 3
private const val DIM_STEP = 0.1f
}

/**
* Provides RecyclerView.ViewHolder behavior.
Expand All @@ -49,31 +61,52 @@ class AlbumAdapter(
/**
* Builds photo
*/
override fun onBindViewHolder(holder: ViewHolder, pos: Int) {
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val binding = holder.binding
val image = items[pos]
val image = items[position]
// Download image
Glide.with(binding.root)
.load(image)
.into(binding.imageView)

// Show photo dialog.
binding.root.setOnClickListener {
// Load image
root!!.x = 0f
root!!.y = 0f
Glide.with(dialog.context)
.load(image)
.into(img!!)
// Resize dialog
val metrics = DisplayMetrics()
@Suppress("DEPRECATION")
activity.windowManager.defaultDisplay.getMetrics(metrics)
dialog.window?.setLayout(metrics.widthPixels, metrics.heightPixels)
dialog.show()
showImage(image, position)
}
}

private fun changeImage(inc: Int = 1) {
pos += inc
root!!.x = root!!.width.toFloat() * inc
Glide.with(dialog.context)
.load(items[pos])
.into(img!!)
animator = ObjectAnimator.ofPropertyValuesHolder(root!!, animX)
animator.duration = 600
animator.start()
}

private fun showImage(imgUrl: String, position: Int) {
// Load image
root!!.x = 0f
root!!.y = 0f
pos = position
Glide.with(dialog.context)
.load(imgUrl)
.into(img!!)
// Resize dialog
val metrics = DisplayMetrics()
@Suppress("DEPRECATION")
activity.windowManager.defaultDisplay.getMetrics(metrics)
dialog.window!!.attributes.dimAmount = MAX_DIM_AMOUNT
dialog.window!!.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
dialog.window?.setLayout(metrics.widthPixels, metrics.heightPixels)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
dialog.window!!.attributes.blurBehindRadius = 5
}
dialog.show()
}

/**
* @return image urls count
*/
Expand All @@ -82,33 +115,53 @@ class AlbumAdapter(
/**
* Provides dialog behavior
*/
@SuppressLint("ClickableViewAccessibility")
@SuppressLint("ClickableViewAccessibility", "ObjectAnimatorBinding")
private fun setupDialog() {
// set content view
dialog.window?.setContentView(R.layout.layout_album_photo)
dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
root = dialog.findViewById(R.id.album_photo_root)
img = dialog.findViewById(R.id.album_photo)
var touchX = 0f
var touchY = 0f
var lastMotionEvent = 0
val hideHeight = 400

// Provides touch
root!!.setOnTouchListener { _, motionEvent ->
when (motionEvent.action) {
MotionEvent.ACTION_DOWN -> {
touchY = motionEvent.y
touchX = motionEvent.x
}
// Move img on X
MotionEvent.ACTION_MOVE -> {
root!!.y += motionEvent.y - touchY
val tmpX = motionEvent.x - touchX
val tmpY = motionEvent.y - touchY
if (lastMotionEvent == MotionEvent.ACTION_DOWN) {
if (abs(tmpX) > abs(tmpY)* Y_PRIORITY)
touchY = 0f
else
touchX = 0f
}
else if(touchY != 0f) {
root!!.y += tmpY
dialog.window!!.attributes.dimAmount -= abs(tmpY * DIM_STEP)
}
else if(touchX != 0f)
root!!.x += tmpX
}
// Hide and change image.
MotionEvent.ACTION_UP -> {
if (lastMotionEvent == MotionEvent.ACTION_DOWN) {
dialog.dismiss()
} else if (root!!.y < -hideHeight || root!!.y > root!!.width - hideHeight) {
} else if (root!!.y < -HIDE_OFFSET || root!!.y > root!!.width - HIDE_OFFSET) {
dialog.dismiss()
} else if (root!!.x < -CHANGE_IMAGE_OFFSET && pos < itemCount-1) {
changeImage()
} else if (root!!.x > CHANGE_IMAGE_OFFSET && pos > 0) {
changeImage(-1)
} else {
animator = ObjectAnimator.ofFloat(root!!, "y", 0f)
animator = ObjectAnimator.ofPropertyValuesHolder(root!!, animX, animY)
animator.duration = 600
animator.start()
}
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/res/layout/layout_album_photo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/album_photo_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_photo">
android:layout_height="match_parent">

<ImageView
android:id="@+id/album_photo"
Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/values-night/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<color name="background">#150524</color>
<color name="background_secondary">#1A1430</color>
<color name="background_toolbar">@color/background_secondary</color>
<color name="background_photo">#B51A1430</color>
<color name="primary">#D7BBF2</color>
<color name="primary_variant">#E6CFFD</color>
<color name="secondary">#2F1D38</color>
Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<color name="background">#fcfefe</color>
<color name="background_secondary">#ECF2F8</color>
<color name="background_toolbar">#1E2832</color>
<color name="background_photo">#B5101119</color>
<color name="primary">#6979B4</color>
<color name="primary_variant">#6076AC</color>
<color name="secondary">#8E95AF</color>
Expand Down

0 comments on commit a9f8d02

Please sign in to comment.