原文

概要


本课程演示如何执行触摸缩放动画,这对诸如照片库等应用程序可以将视图从缩略图动画化为充满屏幕的全尺寸图像。

以下是触摸缩放动画看起来像展开图像缩略图以填充屏幕:
视频地址:https://developer.android.com/training/animation/anim_zoom.mp4

如果你想跳过并看到一个完整的工作示例,请参阅 GitHub上android-WearSpeakerSample项目中的 UIAnimation类

创建视图


创建一个布局文件,其中包含要放大的内容的大小版本。以下示例创建一个ImageButton可点击的图像缩略图,并ImageView显示该图像的放大视图:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/container"android:layout_width="match_parent"android:layout_height="match_parent"><LinearLayout android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"android:padding="16dp"><ImageButtonandroid:id="@+id/thumb_button_1"android:layout_width="100dp"android:layout_height="75dp"android:layout_marginRight="1dp"android:src="@drawable/thumb1"android:scaleType="centerCrop"android:contentDescription="@string/description_image_1" /></LinearLayout><!-- This initially-hidden ImageView will hold the expanded/zoomed version ofthe images above. Without transformations applied, it takes up the entirescreen. To achieve the "zoom" animation, this view's bounds are animatedfrom the bounds of the thumbnail button above, to its final laid-outbounds.--><ImageViewandroid:id="@+id/expanded_image"android:layout_width="match_parent"android:layout_height="match_parent"android:visibility="invisible"android:contentDescription="@string/description_zoom_touch_close" /></FrameLayout>

设置缩放动画


应用布局后,设置触发缩放动画的事件处理程序。以下示例将添加一个View.OnClickListener以ImageButton在用户单击图像按钮时执行缩放动画:

public class ZoomActivity extends FragmentActivity {// Hold a reference to the current animator,// so that it can be canceled mid-way.private Animator mCurrentAnimator;// The system "short" animation time duration, in milliseconds. This// duration is ideal for subtle animations or animations that occur// very frequently.private int mShortAnimationDuration;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_zoom);// Hook up clicks on the thumbnail views.final View thumb1View = findViewById(R.id.thumb_button_1);thumb1View.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {zoomImageFromThumb(thumb1View, R.drawable.image1);}});// Retrieve and cache the system's default "short" animation time.mShortAnimationDuration = getResources().getInteger(android.R.integer.config_shortAnimTime);}...
}

放大视图


适当时,您现在需要从正常大小的视图动画到缩放视图。一般来说,您需要从正常大小的视图的边界到较大视图的边界进行动画处理。以下方法向您展示了如何通过执行以下操作来实现从图像缩略图放大到放大视图的缩放动画:

  • 将高分辨率图像分配给隐藏的“放大”(放大)ImageView。为简单起见,以下示例在UI线程上加载大图像资源。您需要在单独的线程中进行加载以防止在UI线程上阻塞,然后在UI线程上设置位图。理想情况下,位图不应该大于屏幕尺寸。
  • 计算的开始和结束范围ImageView。
  • 动画四个位置和大小的属性X,Y(SCALE_X和SCALE_Y)同时,从起始边界到结束边界。这四个动画被添加到一个AnimatorSet以便它们可以同时启动。
  • 通过运行类似的动画缩小范围,但当用户在放大图像时触摸屏幕时反过来缩小。您可以通过向图像添加a View.OnClickListener来实现ImageView。点击后, ImageView最小化为图像缩略图的大小,并将其可见性设置GONE为隐藏。
private void zoomImageFromThumb(final View thumbView, int imageResId) {// If there's an animation in progress, cancel it// immediately and proceed with this one.if (mCurrentAnimator != null) {mCurrentAnimator.cancel();}// Load the high-resolution "zoomed-in" image.final ImageView expandedImageView = (ImageView) findViewById(R.id.expanded_image);expandedImageView.setImageResource(imageResId);// Calculate the starting and ending bounds for the zoomed-in image.// This step involves lots of math. Yay, math.final Rect startBounds = new Rect();final Rect finalBounds = new Rect();final Point globalOffset = new Point();// The start bounds are the global visible rectangle of the thumbnail,// and the final bounds are the global visible rectangle of the container// view. Also set the container view's offset as the origin for the// bounds, since that's the origin for the positioning animation// properties (X, Y).thumbView.getGlobalVisibleRect(startBounds);findViewById(R.id.container).getGlobalVisibleRect(finalBounds, globalOffset);startBounds.offset(-globalOffset.x, -globalOffset.y);finalBounds.offset(-globalOffset.x, -globalOffset.y);// Adjust the start bounds to be the same aspect ratio as the final// bounds using the "center crop" technique. This prevents undesirable// stretching during the animation. Also calculate the start scaling// factor (the end scaling factor is always 1.0).float startScale;if ((float) finalBounds.width() / finalBounds.height()> (float) startBounds.width() / startBounds.height()) {// Extend start bounds horizontallystartScale = (float) startBounds.height() / finalBounds.height();float startWidth = startScale * finalBounds.width();float deltaWidth = (startWidth - startBounds.width()) / 2;startBounds.left -= deltaWidth;startBounds.right += deltaWidth;} else {// Extend start bounds verticallystartScale = (float) startBounds.width() / finalBounds.width();float startHeight = startScale * finalBounds.height();float deltaHeight = (startHeight - startBounds.height()) / 2;startBounds.top -= deltaHeight;startBounds.bottom += deltaHeight;}// Hide the thumbnail and show the zoomed-in view. When the animation// begins, it will position the zoomed-in view in the place of the// thumbnail.thumbView.setAlpha(0f);expandedImageView.setVisibility(View.VISIBLE);// Set the pivot point for SCALE_X and SCALE_Y transformations// to the top-left corner of the zoomed-in view (the default// is the center of the view).expandedImageView.setPivotX(0f);expandedImageView.setPivotY(0f);// Construct and run the parallel animation of the four translation and// scale properties (X, Y, SCALE_X, and SCALE_Y).AnimatorSet set = new AnimatorSet();set.play(ObjectAnimator.ofFloat(expandedImageView, View.X,startBounds.left, finalBounds.left)).with(ObjectAnimator.ofFloat(expandedImageView, View.Y,startBounds.top, finalBounds.top)).with(ObjectAnimator.ofFloat(expandedImageView, View.SCALE_X,startScale, 1f)).with(ObjectAnimator.ofFloat(expandedImageView,View.SCALE_Y, startScale, 1f));set.setDuration(mShortAnimationDuration);set.setInterpolator(new DecelerateInterpolator());set.addListener(new AnimatorListenerAdapter() {@Overridepublic void onAnimationEnd(Animator animation) {mCurrentAnimator = null;}@Overridepublic void onAnimationCancel(Animator animation) {mCurrentAnimator = null;}});set.start();mCurrentAnimator = set;// Upon clicking the zoomed-in image, it should zoom back down// to the original bounds and show the thumbnail instead of// the expanded image.final float startScaleFinal = startScale;expandedImageView.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {if (mCurrentAnimator != null) {mCurrentAnimator.cancel();}// Animate the four positioning/sizing properties in parallel,// back to their original values.AnimatorSet set = new AnimatorSet();set.play(ObjectAnimator.ofFloat(expandedImageView, View.X, startBounds.left)).with(ObjectAnimator.ofFloat(expandedImageView,View.Y,startBounds.top)).with(ObjectAnimator.ofFloat(expandedImageView,View.SCALE_X, startScaleFinal)).with(ObjectAnimator.ofFloat(expandedImageView,View.SCALE_Y, startScaleFinal));set.setDuration(mShortAnimationDuration);set.setInterpolator(new DecelerateInterpolator());set.addListener(new AnimatorListenerAdapter() {@Overridepublic void onAnimationEnd(Animator animation) {thumbView.setAlpha(1f);expandedImageView.setVisibility(View.GONE);mCurrentAnimator = null;}@Overridepublic void onAnimationCancel(Animator animation) {thumbView.setAlpha(1f);expandedImageView.setVisibility(View.GONE);mCurrentAnimator = null;}});set.start();mCurrentAnimator = set;}});
}
Lastest Update:2018.04.25

联系我

QQ:94297366
微信打赏:https://pan.baidu.com/s/1dSBXk3eFZu3mAMkw3xu9KQ

公众号推荐:

转载于:https://blog.51cto.com/4789781/2120982

【Animations】使用缩放动画放大视图(7)相关推荐

  1. android图片缩放模式,Android使用缩放动画放大你的图片

    注:本篇文章是对官方开发文档的翻译,加上自己的理解和分析. 地址:https://developer.android.com/training/animation/zoom 本篇文章所实现的功能: 触 ...

  2. iOS项目开发实战——制作视图的缩放动画

    视图的大小应该是随时可控的.今天我们就来实现对一个View的缩放动画.该动画的实现与位移动画,透明度动画稍有不同. 详细实现例如以下: import UIKitclass ScaleViewContr ...

  3. android动画放大后缩小,Android 补间动画 scale(缩放)

    今天又遇到了关于Android 动画方面的问题,免不了一番疯狂找资料,所幸解决了自己的问题,为了避免以后遇到同样的问题,再次到处找资料,于是决定写篇随笔记录下来,方便自己方便大家^_^:废话就不说了先 ...

  4. AndroidUI 视图动画-缩放动画效果 (ScaleAnimation)

    放动画效果,可以使用ScaleAnimation: <Buttonandroid:id="@+id/btnScale2"android:layout_width=" ...

  5. 安卓动画全解:补间动画(视图动画)、布局动画、属性动画、逐帧动画。动画Animation属性、Alpha属性、Scale属性、Translate属性、Rotate属性,动画集AnimationSet

    全栈工程师开发手册 (作者:栾鹏) 安卓教程全解 安卓动画全解:补间动画(视图动画).布局动画.属性动画.逐帧动画. 主要内容包含:动画Animation属性.Alpha属性.Scale属性.Tran ...

  6. Android直播头像动画,iOS 仿抖音直播头像缩放动画

    效果图 仿抖音直播头像缩放效果, 简单写了demo, 思路简单, 直接用的递归重复调用, 呈上所有代码. @interface YCXHeaderZoomViewController () @prop ...

  7. Android动画之视图动画和属性动画

    Android 动画分为两大类,分别是视图动画(View Animation)和属性动画(Property Animation).对于这两种动画,都能够使用xml和代码的形式定义动画. 注:布局动画相 ...

  8. 缩放动画 ScaleAnimation 总结

    最近用到了ScaleAnimation来实现图片放大需求,今天就把使用过程中学习的一些东西总结记录一下,希望能对大家有所帮助. - ScaleAnimation是 Android官方提供的动画类Ani ...

  9. Android Property Animation属性动画:scale缩放动画(4)

     Android Property Animation属性动画:scale缩放动画(4) 和之前我写的附录文章1,2,3相似,本文将接着使用Android Property Animation属性 ...

最新文章

  1. scanf()函数的用法和实践
  2. 仿QQ空间用一个tableview显示多种自定义cell
  3. mysql保存数据提示:Out of range value for column错误
  4. CI/CD大幅减少甩锅!
  5. 迁徙图_虾米音乐上的原住民会迁徙去哪呢?
  6. 005 - react
  7. Vnc安装rhel6服务器
  8. 基于 Bootstrp 构建简洁的登录框并实现记住我功能
  9. Android APP 稳定性测试工具—Fastboot使用教程
  10. java gbk编码_java中如何得到输入的汉字的GBK编码
  11. [JZOJ3296] 【SDOI2013】刺客信条
  12. 电脑重装系统后usbcleaner怎么格式化u盘
  13. MongoDB应用记录
  14. Spring: error at ::0 can‘t find referenced pointcut的错误并解决
  15. Cesium通过primitives绘制大数据量扇形图
  16. 代码之外——醒世良言
  17. 华为magic book笔记本无法重装系统的麻烦
  18. 启用计算机的无线同屏,完美:将计算机转换为无线显示器,Windows 10的此功能确实强大...
  19. 国内知名IT互联网公司名单
  20. python 计时方法_Python计时器类| cancel()方法与示例

热门文章

  1. Ubuntu 上 hi3531 交叉编译环境 arm-hisiv100nptl-linux 建设过程
  2. Storm【配置项】 - 详细解释
  3. 演示:PPPOE服务端、拨号路由器、拨号计算机的配置
  4. 使用Struts2上传文件超过2M报错解决方法
  5. 系统开出出现问题~~~\WINDOWS\SYSTEM32\CONFIG\SYSTEM 损坏或丢失无法开机
  6. 超简单破解网页加密源代码
  7. 第三课 查询指定id的单个对象
  8. 关于Vue.js2.0生命周期的研究与理解
  9. 设计模式 ( 十六 ) 观察者模式Observer(对象行为型)
  10. Android 资源保护问题——探索