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
| package com.example.androidx_branch.scalestudy
import android.animation.Animator import android.animation.AnimatorSet import android.animation.ObjectAnimator import android.os.Bundle import android.util.Log import android.view.MotionEvent import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.widget.ViewUtils import androidx.constraintlayout.widget.ConstraintSet import com.example.androidx_branch.R import com.example.androidx_branch.scrollerpicker.library.util.ScreenUtil import com.uppack.lksmall.baseyu.weight.util.ViewUtil import kotlinx.android.synthetic.main.activity_picture.* import kotlinx.android.synthetic.main.activity_scalea.* import java.util.function.LongFunction
class ScaleStudyActivity : AppCompatActivity() { lateinit var scaleAnimal: ObjectAnimator lateinit var scaleYAnimal: ObjectAnimator lateinit var scaleXAnimal: ObjectAnimator var animatorSet: AnimatorSet = AnimatorSet() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_scalea)
btnStart.setOnClickListener { beginBigToSmall(true) animatorSet.start() } btnEnd.setOnClickListener { beginBigToSmall(false) animatorSet.start() } }
fun beginSmallToBig(toBig: Boolean) { scaleXAnimal = ObjectAnimator.ofFloat( imgView, "scaleX", imgView.scaleX, if (toBig) ScreenUtil.getScreenWidth() / imgView.width.toFloat() else 1f ) scaleYAnimal = ObjectAnimator.ofFloat( imgView, "scaleY", imgView.scaleY, if (toBig) ScreenUtil.getScreenHeight() / imgView.height.toFloat() else 1f ) animatorSet.playTogether(scaleXAnimal, scaleYAnimal) animatorSet.setDuration(2000) imgView.pivotX = (imgView.left.toFloat()) / ((ScreenUtil.getScreenWidth() / imgView.width.toFloat()) - 1) imgView.pivotY = (imgView.top.toFloat()) / ((ScreenUtil.getScreenHeight() / imgView.height.toFloat()) - 1) }
fun beginBigToSmall(toSmall: Boolean) { var targetX = ViewUtil.dip2px(40f) var targetY = ViewUtil.dip2px(100f) scaleXAnimal = ObjectAnimator.ofFloat( imgView, "scaleX", imgView.scaleX, if (toSmall) ViewUtil.dip2px(100f) / imgView.width.toFloat() else 1f ) scaleYAnimal = ObjectAnimator.ofFloat( imgView, "scaleY", imgView.scaleY, if (toSmall) ViewUtil.dip2px(90f) / imgView.height.toFloat() else 1f ) animatorSet.playTogether(scaleXAnimal, scaleYAnimal) animatorSet.setDuration(2000) imgView.pivotX =targetX/(1f-ViewUtil.dip2px(100f)/imgView.width.toFloat() ) imgView.pivotY =targetY/(1f-ViewUtil.dip2px(90f)/imgView.height.toFloat() ) }
}
|