package com.example.administrator.mktproject.weight
import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.graphics.drawable.GradientDrawable
import android.os.Build
import android.support.annotation.RequiresApi
import android.util.AttributeSet
import android.widget.Button
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
/**
* Created by Administrator on 2019/7/30 0030.
* GradientDrawable Canvas
*/
open class SignInBtn(context: Context?, attrs: AttributeSet?) : Button(context, attrs){
private var mGradientDrawable: GradientDrawable = null // set the button background style?
Private var Paint: Paint = null // brush?
Private MLEFT var = 0F // left button zoom animation of the value of
private var mRight = 0f // button to the right of the zoom animation value
private var start: Float = 0f // value of the arc to start the animation
private var isdraw: Boolean = false // whether or not to start drawing arc
private var mValueAnimator2:? ValueAnimator = null
/ **
* initialization block
* /
{the init
initial pattern initView () // set button
}
/ **
* Set the initial style button
* /
Private as initView Fun () {
// Initialization brushes
paint = Paint () // create brushes
paint !!. IsAntiAlias = true // Set brush antialiasing (smoothing the lines)
Paint! ! .color = Color.parseColor ( "# ffffff ") // set the pen color
paint !!. strokeWidth = 4f // set the pen width of
paint !!. style = Paint.Style.STROKE // set the pen style
// Initialization button background pattern
mGradientDrawable = GradientDrawable () // shape in use code
mGradientDrawable !!. SetColor (Color.argb (100,0,0,255 )) // Button background color
mGradientDrawable !!. CornerRadius = 130f / / button round angle
background = mGradientDrawable
}
/ **
* Start button becomes circular animation
* /
Open Fun mBtnAinim () {
isEnabled = // set to false during the animation can not click button
text = "" // null character setting button
val mValueAnimator = ValueAnimator.ofInt (width , height) // button animation deformation, is the same as the width and height becomes
mValueAnimator.addUpdateListener (Object: {ValueAnimator.AnimatorUpdateListener
the override Fun onAnimationUpdate (animation: ValueAnimator) {
Val value: Int = animation.animatedValue aS Int
MLEFT = (width -value) /. 2F
mRight = width- (width-value) /. 2F
// the setBounds (left, Top, right, bottom) four parameter values can not understand the reference coordinate system view
mGradientDrawable !!. setBounds (mLeft.toInt ( ), 0, mRight.toInt (), height)
}
})
length of 500 msec mValueAnimator.duration = 500 // animate
mValueAnimator.start ()
mValueAnimator.addListener (Object: AnimatorListenerAdapter () {
the override Fun onAnimationEnd (Animation:? Animator) {// Animation end event listener
super.onAnimationEnd (Animation)
drawArc () // Draw an arc arc loading animation
}
})
}
/ **
* arc starts to rotate animation
* /
Private Fun the drawArc () {
isdraw = change state to true // this method can be used to draw an arc onDraw
mValueAnimator2 = ValueAnimator.ofFloat (0F, 360F)
mValueAnimator2 !!. AddUpdateListener ( Object: ValueAnimator.AnimatorUpdateListener {
the override Fun onAnimationUpdate (animation:? ValueAnimator) {
. = Start animation !! animatedValue AS Float
the invalidate () // redraw View
}
})
. mValueAnimator2 !! repeatCount = ValueAnimator.INFINITE // animate unlimited repeat
mValueAnimator2 !!. DURATION = 500
mValueAnimator2 !!. Start ()
}
/ **
* end of the animation
* /
Open Fun endAnim (btnText: String) {
// btn to the original pattern
val mValueAnimator = ValueAnimator.ofInt (0, width ) // original width value button
mValueAnimator.addUpdateListener (object: ValueAnimator {.AnimatorUpdateListener
the override Fun onAnimationUpdate (Animation: ValueAnimator) {
Val value: Int = animation.animatedValue AS Int
. mGradientDrawable !! the setBounds ((width-value) / 2,0, width- (width-value) / 2, height)
}
})
mValueAnimator.duration = 500
mValueAnimator.start ()
mValueAnimator.addListener (Object: AnimatorListenerAdapter () {
the override Fun onAnimationEnd (Animation:? Animator) {
super.onAnimationEnd (Animation)
text = btnText // btn change the font
isEnabled = true // set during the animation button not clickable
}
})
mValueAnimator2 !!. end () // stop the rotation of the arc
isdraw = false // do not draw an arc
invalidate () // screen redraw
}
@SuppressLint ( "DrawAllocation")
the override Fun onDraw (Canvas:? Canvas) {
super.onDraw (Canvas)
IF (isdraw) {// If the button animation is complete scaling deformation draw an arc in the beginning, will draw an arc when the zoom buttons let , it is difficult to see
Val mRectF = RectF (MLEFT + 10,10f, mRight-10, height-10.toFloat ())
Canvas !!. the drawArc (mRectF, Start, 16Of, to false, Paint) // draw an arc
}
}
}
---------------------