使用动画缩放图片

我们的app经常遇到这样一种场景,就是小图到大图的转换,这时候如果有个缩放动画就会很自然。本节将介绍如何使用动画进行缩放图片,在点击头像看大图这种场景可以使用。本文的例子的示意图如下所示:

640?wx_fmt=gif

创建View

布局主要包含两个View,一个ImageButton用于加载缩略图,一个ImageView用于显示大图。

 
 
  1. <?xml version="1.0" encoding="utf-8"?>

  2. <android.support.constraint.ConstraintLayout

  3. android:id="@+id/container"

  4. xmlns:android="http://schemas.android.com/apk/res/android"

  5. xmlns:app="http://schemas.android.com/apk/res-auto"

  6. xmlns:tools="http://schemas.android.com/tools"

  7. android:layout_width="match_parent"

  8. android:layout_height="match_parent"

  9. tools:context=".animation.EnlargeImageActivity">

  10. <ImageButton

  11. android:id="@+id/imageBtn"

  12. android:layout_width="100dp"

  13. android:layout_height="75dp"

  14. android:scaleType="centerCrop"

  15. android:src="@drawable/pic_11"/>

  16. <ImageView

  17. android:id="@+id/imageView"

  18. android:layout_width="match_parent"

  19. android:layout_height="match_parent"

  20. android:src="@drawable/pic_11"

  21. android:visibility="invisible"/>

  22. </android.support.constraint.ConstraintLayout>

设置缩放动画

ImageButtton触发动画,这里就不赘述了。

缩放动画

大体上,你需要从正常尺寸的View的界限动画到大尺寸的View的界限。下面的方法通过四步介绍了如何实现一个从缩略图到大图的放大动画。

  1. 分配大图给ImageView,即放大后的View。下面的代码是在主线程中加载图片的,这个过程在现实app中一般是要进行网络操作的,需要放在非UI线程。理想状态下,这个图片的尺寸是不应该超过屏幕尺寸的。

  2. 计算ImageView的起始和结束尺寸。

  3. 从起始尺寸同时动画四个属性:X、Y、SCALEX和SCALEY。这四个参数一起加入到AnimationSet,以便可以同时动画。

  4. 使用一个相似的动画作用于大的ImageView,当点击后,图片缩小回去,最后隐藏ImageView。

从小到大动画

代码如下:

 
 
  1. //从小到大

  2. private fun zoomImageFromThumb() {

  3. mCurrentAnimator?.cancel()

  4. imageView.setImageResource(R.drawable.pic_11)

  5. //获取尺寸

  6. val startBoundsInt = Rect()

  7. val finalBoundsInt = Rect()

  8. val globalOffset = Point()

  9. imageBtn.getGlobalVisibleRect(startBoundsInt)

  10. imageView.getGlobalVisibleRect(finalBoundsInt, globalOffset)

  11. //调整使top=left=0

  12. startBoundsInt.offset(-globalOffset.x, -globalOffset.y)

  13. finalBoundsInt.offset(-globalOffset.x, -globalOffset.y)

  14. val startBounds = RectF(startBoundsInt)

  15. val finalBounds = RectF(finalBoundsInt)

  16. //计算宽高缩放比

  17. val startScale: Float

  18. if ((finalBounds.width() / finalBounds.height() > startBounds.width() / startBounds.height())) {

  19. startScale = startBounds.height() / finalBounds.height()

  20. val startWidth: Float = startScale * finalBounds.width()

  21. val deltaWidth: Float = (startWidth - startBounds.width()) / 2

  22. startBounds.left -= deltaWidth.toInt()

  23. startBounds.right += deltaWidth.toInt()

  24. } else {

  25. // Extend start bounds vertically

  26. startScale = startBounds.width() / finalBounds.width()

  27. val startHeight: Float = startScale * finalBounds.height()

  28. val deltaHeight: Float = (startHeight - startBounds.height()) / 2f

  29. startBounds.top -= deltaHeight.toInt()

  30. startBounds.bottom += deltaHeight.toInt()

  31. }

  32. imageBtn.visibility = View.INVISIBLE

  33. imageView.visibility = View.VISIBLE

  34. imageView.pivotX = 0f

  35. imageView.pivotY = 0f

  36. mCurrentAnimator = AnimatorSet().apply {

  37. //x、y、scaleX、scaleY四个维度一起动画

  38. play(ObjectAnimator.ofFloat(

  39. imageView,

  40. View.X,

  41. startBounds.left,

  42. finalBounds.left)

  43. ).apply {

  44. with(ObjectAnimator.ofFloat(imageView, View.Y, startBounds.top, finalBounds.top))

  45. with(ObjectAnimator.ofFloat(imageView, View.SCALE_X, startScale, 1f))

  46. with(ObjectAnimator.ofFloat(imageView, View.SCALE_Y, startScale, 1f))

  47. }

  48. duration = mShortAnimationDuration.toLong()

  49. interpolator = DecelerateInterpolator()

  50. addListener(object : AnimatorListenerAdapter() {

  51. override fun onAnimationEnd(animation: Animator) {

  52. mCurrentAnimator = null

  53. }

  54. override fun onAnimationCancel(animation: Animator) {

  55. mCurrentAnimator = null

  56. }

  57. })

  58. start()

  59. }

  60. }

其中有一点说明,宽高采用了同样的缩放比,但是由于初始尺寸的宽高比不一定完全等于结束时的宽高比,因此会对初始尺寸进行微调,使比例与最终比例一致。针对我们这里的情况,示意图如下: ![]

640?wx_fmt=jpeg

初始宽高比大于1,结束宽高比小于1,为了统一,对初始尺寸进行调整,如中间图所示。

从大到小缩放

从大到小的缩放动画与上面的动画相反,这里就不贴代码了,感兴趣的可以去后面找demo地址查看。

缩放比例不一致的效果

上面的例子与官方类似,都是缩放比例一致。本着好奇心,试试缩放比例不一致的效果如何。

效果如下:

640?wx_fmt=gif

反正我是没怎么看出差距来,看出来的差距的欢迎留言我。

总结

关于代码,请移步Github地址:https://github.com/wangli135/ClimbDemo/tree/master/jetpackdemo/src/main/java/com/xingfeng/jetpackdemo/animation。这次对整个项目做了个整理,更好查看各个demo了,样式如下:

640?wx_fmt=jpeg

参考

  • https://developer.android.com/training/animation/zoom?hl=zh-cn


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值