aboutsummaryrefslogtreecommitdiff
path: root/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/BlurredImageView.kt
blob: f1e32d17b0a9930cac461a9e845d253333743eda (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
package ca.allanwang.kau.mediapicker

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import ca.allanwang.kau.ui.views.MeasureSpecContract
import ca.allanwang.kau.ui.views.MeasureSpecDelegate
import ca.allanwang.kau.utils.*
import com.mikepenz.google_material_typeface_library.GoogleMaterial
import jp.wasabeef.blurry.internal.BlurFactor
import jp.wasabeef.blurry.internal.BlurTask
import kotlinx.android.synthetic.main.kau_blurred_imageview.view.*

/**
 * 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() {

    private var blurred = false
    val imageBase: ImageView get() = image_base

    init {
        inflate(R.layout.kau_blurred_imageview, true)
        initAttrs(context, attrs)
        image_foreground.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()
        imageBase.clearAnimation()
        image_blur.clearAnimation()
        image_foreground.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)


    fun isBlurred(): Boolean {
        return blurred
    }

    /**
     * Applies a blur and fills the blur image asynchronously
     * When ready, scales the image down and shows the blur & foreground
     */
    fun blur() {
        if (blurred) return
        blurred = true
        val factor = BlurFactor()
        factor.width = width
        factor.height = height
        BlurTask(imageBase, factor) {
            image_blur.setImageDrawable(it)
            scaleAnimate(ANIMATION_SCALE).start()
            image_blur.alphaAnimate(1f).start()
            image_foreground.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() {
        blurred = true
        clearAnimation()
        val factor = BlurFactor()
        factor.width = width
        factor.height = height
        BlurTask(imageBase, factor) { drawable ->
            image_blur.setImageDrawable(drawable)
            scaleXY = ANIMATION_SCALE
            image_blur.alpha = 1f
            image_foreground.alpha = 1f
        }.execute()
    }

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


    /**
     * Clear all animations and unblur the image
     */
    fun removeBlurInstantly() {
        blurred = false
        clearAnimation()
        scaleX = 1.0f
        scaleX = 1.0f
        image_blur.alpha = 0f
        image_blur.setImageDrawable(null)
        image_foreground.alpha = 0f
    }

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

    /**
     * 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()
        fullAction({ it.visible().background = null })
        image_foreground.setBackgroundColorRes(R.color.kau_blurred_image_selection_overlay)
        image_foreground.setIcon(GoogleMaterial.Icon.gmd_check, 30, Color.WHITE)
    }

    private fun fullAction(action: (View) -> Unit) {
        action(this)
        action(imageBase)
        action(image_blur)
        action(image_foreground)
    }
}