Android 控件移动控制

大部分代码网上找的,参考地址再找太麻烦了,懒得再找了。

设置位置

//x,y为控件中心点坐标
fun move(x:Int,y:Int,view:View){
        val left = x - view.width/2
        val top = y - view.height/2
        val right = left + view.width
        val bottom = top + view.height
        //如果控件做过translation动画,需要复位(2d)
        view.translationX = 0f
        view.translationY = 0f
        view.layout(left,top,right,bottom)
    }

弹簧动画1

// 通过自定义Interpolator可以实现一些有趣的效果 
class SpringInterpolator(private val factor: Float) : Interpolator {

    override fun getInterpolation(input: Float): Float {
        //factor = 0.4
        //        pow(2, -10 * x) * sin((x - factor / 4) * (2 * PI) / factor) + 1

        return (Math.pow(
            2.0,
            (-10 * input).toDouble()
        ) * Math.sin((input - factor / 4) * (2 * Math.PI) / factor) + 1).toFloat()
    }
}
fun moveAni(x:Int,y:Int,view:View){
        val left = x - view.width/2 - view.translationX
        val top = y - view.height/2 - view.translationY
        val mx = left - view.left
        val my = top - view.top

        var aniset = AnimatorSet()
        var objAniY = ObjectAnimator.ofFloat(view,"translationY",view.translationY,view.translationY +my)
        var objAniX = ObjectAnimator.ofFloat(view,"translationX",view.translationX,view.translationX +mx)
        aniset.play(objAniX).with(objAniY)
        aniset.duration = 300
        aniset.interpolator = SpringInterpolator(1.3f)
        aniset.start()
    }

弹簧动画2

//弹簧动画库 
implementation 'com.android.support:support-dynamic-animation:28.0.0'
     //弹簧2 可以再封装一下,懒得弄了
    fun moveAni2(x:Int,y:Int,view:View){
        val left = x - view.width/2
        val top = y - view.height/2

        //这里可以用  SpringAnimation(view,SpringAnimation.TRANSLATION_Y,view.translationY)
        // TRANSLATION_X、 TRANSLATION_Y 等
        //这里的示例没有用TRANSLATION属性做动画,某种程度上这样更灵活,用这样的方法可以实现复杂的效果
        var spaniY = SpringAnimation(view,object : FloatPropertyCompat("top"){
            override fun setValue(p0: View, p1: Float) {
                p0.layout(p0.left,p1.toInt(),p0.right,p1.toInt()+p0.height)
            }
            override fun getValue(p0: View): Float {
               return p0.top.toFloat()
            }
        })
        spaniY.spring = SpringForce(top.toFloat())
        //复合动画这部分如果设置的不一样会有意想不到的混乱效果
        spaniY.minimumVisibleChange = DynamicAnimation.MIN_VISIBLE_CHANGE_PIXELS
        spaniY.spring.dampingRatio = SpringForce.DAMPING_RATIO_LOW_BOUNCY
        spaniY.spring.stiffness = SpringForce.STIFFNESS_MEDIUM
        spaniY.start()

        var spanX = SpringAnimation(view,object : FloatPropertyCompat("left"){
            override fun setValue(p0: View, p1: Float) {
                p0.layout(p1.toInt(),p0.top,p1.toInt()+p0.width,p0.bottom)
            }
            override fun getValue(p0: View): Float {
                return p0.left.toFloat()
            }
        })
        spanX.spring = SpringForce(left.toFloat())
        //与第一个的设置相同
        spanX.minimumVisibleChange = DynamicAnimation.MIN_VISIBLE_CHANGE_PIXELS
        spanX.spring.dampingRatio = SpringForce.DAMPING_RATIO_LOW_BOUNCY
        spanX.spring.stiffness = SpringForce.STIFFNESS_MEDIUM
        spanX.start()
    }

你可能感兴趣的:(Android 控件移动控制)