从任意位置缩放到屏幕大小

图片替换文本

代码实例:

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

/**
* @Author Yu
* @Date 2021/10/27 9:51
* @Description TODO
*/
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)
//beginSmallToBig(true)
animatorSet.start()
}
btnEnd.setOnClickListener {
beginBigToSmall(false)
// beginSmallToBig(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)// 在实际项目中这个是目标最终的距离离x的位置
var targetY = ViewUtil.dip2px(100f)// 在实际项目中这个是目标最终的距离离Y的位置
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() )
}

}
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
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/imgView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/btnEnd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="回去"
app:layout_constraintLeft_toRightOf="@+id/btnStart"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>