Android使用缩放动画放大你的图片

这篇文章介绍了如何在Android中实现点击ImageView后启动缩放动画,将图片从缩略图放大到填充屏幕,并能返回原大小。讲解了getGlobalVisibleRect方法的作用,以及如何通过动画效果实现图片的放大和缩小。
摘要由CSDN通过智能技术生成

注:本篇文章是对官方开发文档的翻译,加上自己的理解和分析。

地址:https://developer.android.com/training/animation/zoom

本篇文章所实现的功能:

触摸一张ImageView后开始展示缩放动画,将视图从缩略图放大为填充屏幕的全尺寸图像,并且点击大图后还能够原路返回,缩放回去。无图无真相,先来看看效果图:

实现的代码中你需要知道的一些理论知识(大神请忽略):

首先是View的两个方法: 

getGlobalVisibleRect(Rect r)  →  得到这个view的可视区域的矩形

getGlobalVisibleRect(Rect r, Point globalOffset)  →  比上面的那个多了个参数,这个参数的内容是view的左上角相对屏幕左上角的偏移量

一开始看到这我是有点迷糊的,getGlobalVisibleRect中第二个参数到底是干啥用的?抱着这个疑问,我决定打个Log来看看,先看看用了哪些代码来打印:

//下面这个container就是当前的根布局的id,finalBounds就是新建的一个Rect,
findViewById(R.id.container)
                .getGlobalVisibleRect(finalBounds, globalOffset);

        //globalOffset里的偏移量
        Log.i("TAG", "globalOffset.x:" + globalOffset.x);
        Log.i("TAG", "globalOffset.y:" + globalOffset.y);
        startBounds.offset(-globalOffset.x, -globalOffset.y);
        finalBounds.offset(-globalOffset.x, -globalOffset.y);

        //获取状态栏高度
        int height = 0;
        int resourceId = getApplicationContext().getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            height = getApplicationContext().getResources().getDimensionPixelSize(resourceId);
        }

        Log.i("TAG", "状态栏高度:" + height);

        //获取actionBar的高度
        int actionBarHeight = 0;
        TypedValue tv = new TypedValue();
        if (this.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
            actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
            Log.i("TAG", "actionBar高度:" + actionBarHeight);

        }

打印的log:

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android ConstraintLayout可以通过缩放动画实现视图放大或缩小效果。可以使用属性动画或者逐帧动画实现缩放动画效果。 以下是使用属性动画实现缩放动画的示例代码: ```java // 导入必要的类 import android.animation.ObjectAnimator; import android.view.View; // 获取要进行缩放动画视图 View view = findViewById(R.id.your_view_id); // 创建X轴方向的缩放动画 ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(view, "scaleX", 1.0f, 2.0f); scaleXAnimator.setDuration(1000); // 设置动画持续时间为1秒 // 创建Y轴方向的缩放动画 ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(view, "scaleY", 1.0f, 2.0f); scaleYAnimator.setDuration(1000); // 设置动画持续时间为1秒 // 同时播放X轴和Y轴的缩放动画 AnimatorSet animatorSet = new AnimatorSet(); animatorSet.playTogether(scaleXAnimator, scaleYAnimator); animatorSet.start(); ``` 以上代码中,首先通过findViewById方法获取要进行缩放动画视图,然后使用ObjectAnimator类创建X轴和Y轴方向的缩放动画,设置动画的起始值和结束值,以及动画的持续时间。最后使用AnimatorSet类将两个缩放动画同时播放。 另外,还可以使用逐帧动画实现缩放动画。逐帧动画是一系列静态图片按照一定的时间间隔连续播放,从而形成动画效果。可以通过在res/drawable目录下创建xml文件来定义逐帧动画。 以下是使用逐帧动画实现缩放动画的示例代码: 1. 在res/drawable目录下创建scale_animation.xml文件,内容如下: ```xml <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/scale_frame1" android:duration="100" /> <item android:drawable="@drawable/scale_frame2" android:duration="100" /> <item android:drawable="@drawable/scale_frame3" android:duration="100" /> <!-- 添加更多的帧图片 --> </animation-list> ``` 2. 在res/drawable目录下创建scale_frame1.png、scale_frame2.png、scale_frame3.png等图片文件,作为逐帧动画的帧图片。 3. 在布局文件中添加一个ImageView视图,并设置其背景为scale_animation.xml: ```xml <ImageView android:id="@+id/animation_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/scale_animation" /> ``` 4. 在代码中获取ImageView视图,并启动逐帧动画: ```java ImageView imageView = findViewById(R.id.animation_view); AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground(); animationDrawable.start(); ``` 以上代码中,首先通过findViewById方法获取ImageView视图,然后将其背景设置为scale_animation.xml,最后通过AnimationDrawable类获取逐帧动画对象,并启动动画
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值