package io.xdrm.lebonprix.anim import android.content.Context import android.graphics.* import android.support.v4.content.res.ResourcesCompat import android.view.animation.Animation import android.view.animation.Transformation import android.widget.ImageView import io.xdrm.lebonprix.R class ResultOpeningAnimation( val ctx: Context, val target: ImageView ) : Animation() { // VARIABLES private var width: Int = 1000 private var height: Int = 1000 private var bitmap : Bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) private var canvas : Canvas = Canvas(bitmap) private var progress: Float = 0F init { target.addOnLayoutChangeListener { v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom -> width = right-left height = bottom-top // unregister previous target.setImageBitmap(null) canvas.setBitmap(null) bitmap.recycle() // build new bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) canvas = Canvas(bitmap) target.setImageBitmap(bitmap) } } override fun start() { target.startAnimation(this) } override fun applyTransformation(interpolatedTime: Float, t: Transformation?) { super.applyTransformation(interpolatedTime, t) progress = interpolatedTime draw() } private fun draw(){ canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR) drawFilter() target.setImageBitmap(bitmap) } private fun drawFilter(){ val bg = Paint() bg.color = ResourcesCompat.getColor(ctx.resources, R.color.colorBackground, null) // max radius to hide the whole screen val maxRad = Math.sqrt( Math.pow(width.toDouble(),2.toDouble()) + Math.pow(height.toDouble(), 2.toDouble())) * 1.5F // security of 1.5x the max radius var rad = (1F-progress) * maxRad // centered val x = width.toFloat() / 2F val y = height.toFloat() / 2F canvas.drawCircle(x, y, rad.toFloat(), bg) } }