aboutsummaryrefslogtreecommitdiff
path: root/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/BlurredImageView.kt
blob: 8e78abfe16b1b73606fb069595c4f9b9c3eecfdb (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * Copyright 2018 Allan Wang
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ca.allanwang.kau.mediapicker

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import ca.allanwang.kau.mediapicker.databinding.KauBlurredImageviewBinding
import ca.allanwang.kau.ui.views.MeasureSpecContract
import ca.allanwang.kau.ui.views.MeasureSpecDelegate
import ca.allanwang.kau.utils.scaleXY
import ca.allanwang.kau.utils.setBackgroundColorRes
import ca.allanwang.kau.utils.setIcon
import ca.allanwang.kau.utils.visible
import com.mikepenz.iconics.typeface.library.googlematerial.GoogleMaterial
import jp.wasabeef.blurry.internal.BlurFactor
import jp.wasabeef.blurry.internal.BlurTask

/**
 * Created by Allan Wang on 2017-07-14.
 *
 * ImageView that can be blurred and selected
 * The frame is composed of three layers: the base, the blur, and the foreground
 * Images should be placed in the base view, and the blur view should not be touched
 * as the class will handle it
 * The foreground by default contains a white checkmark, but can be customized or hidden depending on the situation
 */
class BlurredImageView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr), MeasureSpecContract by MeasureSpecDelegate() {

    var isBlurred = false
        private set

    val imageBase: ImageView get() = binding.imageBase

    private val binding: KauBlurredImageviewBinding = KauBlurredImageviewBinding.inflate(LayoutInflater.from(context), this)

    init {
        initAttrs(context, attrs)
        binding.imageForeground.setIcon(GoogleMaterial.Icon.gmd_check, 30)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val result = onMeasure(this, widthMeasureSpec, heightMeasureSpec)
        super.onMeasure(result.first, result.second)
    }

    override fun clearAnimation() {
        super.clearAnimation()
        with(binding) {
            imageBase.clearAnimation()
            imageBlur.clearAnimation()
            imageForeground.clearAnimation()
        }
    }

    private fun View.scaleAnimate(scale: Float) =
        animate().scaleXY(scale).setDuration(ANIMATION_DURATION)

    private fun View.alphaAnimate(alpha: Float) =
        animate().alpha(alpha).setDuration(ANIMATION_DURATION)

    /**
     * Applies a blur and fills the blur image asynchronously
     * When ready, scales the image down and shows the blur & foreground
     */
    fun blur() {
        if (isBlurred) return
        isBlurred = true
        val factor = BlurFactor()
        factor.width = width
        factor.height = height
        BlurTask(imageBase, factor) {
            with(binding) {
                imageBlur.setImageDrawable(it)
                scaleAnimate(ANIMATION_SCALE).start()
                imageBlur.alphaAnimate(1f).start()
                imageForeground.alphaAnimate(1f).start()
            }
        }.execute()
    }

    /**
     * Clears animations and blurs the image without further animations
     * This method is relatively instantaneous, as retrieving the blurred image
     * is still asynchronous and takes time
     */
    fun blurInstantly() {
        isBlurred = true
        clearAnimation()
        val factor = BlurFactor()
        factor.width = width
        factor.height = height
        BlurTask(imageBase, factor) { drawable ->
            with(binding) {
                imageBlur.setImageDrawable(drawable)
                scaleXY = ANIMATION_SCALE
                imageBlur.alpha = 1f
                imageForeground.alpha = 1f
            }
        }.execute()
    }

    /**
     * Animate view back to original state and remove drawable when finished
     */
    fun removeBlur() {
        if (!isBlurred) return
        isBlurred = false
        scaleAnimate(1.0f).start()
        with(binding) {
            imageBlur.alphaAnimate(0f).withEndAction { imageBlur.setImageDrawable(null) }.start()
            imageForeground.alphaAnimate(0f).start()
        }
    }

    /**
     * Clear all animations and unblur the image
     */
    fun removeBlurInstantly() {
        isBlurred = false
        clearAnimation()
        scaleX = 1.0f
        scaleX = 1.0f
        with(binding) {
            imageBlur.alpha = 0f
            imageBlur.setImageDrawable(null)
            imageForeground.alpha = 0f
        }
    }

    /**
     * Switch blur state and apply transition
     *
     * @return true if new state is blurred; false otherwise
     */
    fun toggleBlur(): Boolean {
        if (isBlurred) removeBlur()
        else blur()
        return isBlurred
    }

    /**
     * Clears all of the blur effects to restore the original states
     * If views were modified in other ways, this method won't affect it
     */
    fun reset() {
        removeBlurInstantly()
        imageBase.setImageDrawable(null)
    }

    /**
     * Reset most of possible changes to the view
     */
    fun fullReset() {
        reset()
        with(binding) {
            fullAction { it.visible().background = null }
            imageForeground.setBackgroundColorRes(R.color.kau_blurred_image_selection_overlay)
            imageForeground.setIcon(GoogleMaterial.Icon.gmd_check, 30, Color.WHITE)
        }
    }

    private fun KauBlurredImageviewBinding.fullAction(action: (View) -> Unit) {
        action(this@BlurredImageView)
        action(imageBase)
        action(imageBlur)
        action(imageForeground)
    }
}