Skip to content

Commit

Permalink
Added shadow
Browse files Browse the repository at this point in the history
  • Loading branch information
PopPsyA committed Nov 14, 2019
1 parent f530e03 commit 3c12e9c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 14 deletions.
17 changes: 12 additions & 5 deletions app/src/main/java/devit951/github/magictipexample/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package devit951.github.magictipexample

import android.annotation.TargetApi
import android.graphics.Color
import android.graphics.Outline
import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.ViewOutlineProvider
import androidx.appcompat.app.AppCompatActivity
import devit951.github.magictip.AutoCloseMagicTip
import devit951.github.magictip.tip.AutoCloseMagicTip
import devit951.github.magictip.MagicTip
import devit951.github.magictip.OneMagicTip
import devit951.github.magictip.animationdelegate.AlphaMagicTipAnimation
import devit951.github.magictip.tip.OneMagicTip
import devit951.github.magictip.animationdelegate.CombinedMagicTipAnimation
import devit951.github.magictip.animationdelegate.ScaledTranslateMagicTipAnimation
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Expand All @@ -27,10 +32,12 @@ class MainActivity : AppCompatActivity() {
imageView.setOnClickListener {
OneMagicTip(it, MagicTip(it)
.settings {
startAnimationDelegate = CombinedMagicTipAnimation(ScaledTranslateMagicTipAnimation())
startAnimationDelegate =
CombinedMagicTipAnimation(ScaledTranslateMagicTipAnimation())
exitAnimationDelegate = ScaledTranslateMagicTipAnimation.Reversed()
text = "Lorem Posen"
text = "Lorem Posen"
bgColor = Color.BLUE

})
.show()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package devit951.github.magictip

import android.content.Context
import android.content.res.Resources
import android.content.ContextWrapper
import android.app.Activity
import android.content.ContextWrapper
import android.content.res.Resources
import android.graphics.Color
import android.view.View
import kotlin.math.min
import kotlin.math.roundToInt


fun dp2Px(dp: Int): Int = (Resources.getSystem().displayMetrics.density * dp).toInt()
Expand All @@ -18,4 +20,11 @@ fun View.activity(): Activity {
context = context.baseContext
}
error("Activity could not find")
}
}

fun manipulateColor(color: Int, factor: Float) = Color.argb(
Color.alpha(color),
min((Color.red(color) * factor).roundToInt(), 255),
min((Color.green(color) * factor).roundToInt(), 255),
min((Color.blue(color) * factor).roundToInt(), 255)
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package devit951.github.magictip
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.view.ViewCompat
Expand All @@ -17,9 +18,9 @@ class MagicTipView @JvmOverloads constructor(context: Context, attrs: AttributeS

private val trianglePath = Path()

private var magicTipPadding = dp2Px(4)
private var magicTipPadding = dp2Px(5)
private var widthOfTriangle = dp2Px(14)
private var heightOfTriangle = dp2Px(10)
private var heightOfTriangle = dp2Px(12)


internal var clickDelegate: MagicTipClickDelegate = ImmediatlyRemoveViewClickDelegate()
Expand All @@ -28,6 +29,7 @@ class MagicTipView @JvmOverloads constructor(context: Context, attrs: AttributeS
set(value) {
field = value
bgPaint.color = value
bgPaint.setShadowLayer(4f, 0.0f, 2.5f, manipulateColor(field, 0.8f))
}

var startAnimationDelegate: MagicTipAnimationDelegate? = null
Expand All @@ -46,6 +48,7 @@ class MagicTipView @JvmOverloads constructor(context: Context, attrs: AttributeS
setMargins(margin, margin, margin, margin)
}
ViewCompat.setTranslationZ(this, Integer.MAX_VALUE.toFloat())
setLayerType(View.LAYER_TYPE_SOFTWARE, bgPaint)
}

override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
Expand Down Expand Up @@ -73,7 +76,7 @@ class MagicTipView @JvmOverloads constructor(context: Context, attrs: AttributeS
val halfTriangleWidth = widthOfTriangle / 2f
val y = h.toFloat() - heightOfTriangle
trianglePath.moveTo(halfWidth - halfTriangleWidth, y)
trianglePath.lineTo(halfWidth, h.toFloat())
trianglePath.lineTo(halfWidth, h.toFloat() - 5)
trianglePath.lineTo(halfWidth + halfTriangleWidth, y)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package devit951.github.magictip
package devit951.github.magictip.tip

import android.os.Handler
import android.view.View
import devit951.github.magictip.MagicTip

class AutoCloseMagicTip(anchorView: View, private val delay: Long = 1_000): MagicTip(anchorView){
override fun show() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package devit951.github.magictip
package devit951.github.magictip.tip

import android.view.View
import androidx.core.view.children
import devit951.github.magictip.MagicTip
import devit951.github.magictip.MagicTipView

class OneMagicTip(anchorView: View, private val magicTip: MagicTip): MagicTip(anchorView) {

Expand Down

0 comments on commit 3c12e9c

Please sign in to comment.