前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ScaleAnimation开始结束位置分析[通俗易懂]

ScaleAnimation开始结束位置分析[通俗易懂]

作者头像
全栈程序员站长
发布2022-11-04 17:22:55
4360
发布2022-11-04 17:22:55
举报
文章被收录于专栏:全栈程序员必看

做项目的时候,需要用到动画,大小和位置都不一样。刚开始想到的是ScaleAnimation和TranslateAnimation进行组合,但实验后发现,目标位置始终不对,只用TranslateAnimation是没有问题,所以ScaleAnimation应该不只是进行了缩放

经过查找资料,发现ScaleAnimation还进行起始位置的移动。ScaleAnimation分为两种情况,从本身的位置缩放到另一个位置和从另一个位置缩放到本身的位置

先看一下处理后的效果

ScaleAnimation开始结束位置分析[通俗易懂]
ScaleAnimation开始结束位置分析[通俗易懂]

看一下ScaleAnimation的构造函数

代码语言:javascript
复制
    /**
     * fromX 在x轴方向,起始缩放比例
     * toX 在x轴上,目标缩放比例
     * fromY 在y轴方向,起始缩放比例
     * toY 在y轴上,目标缩放比例
     * pivotX 缩放的中心轴位置,这个跟我们自己的理解不一样,要通过算法算出来,这两种情况的算法还不一样
     * pivotY
     */
    public ScaleAnimation(float fromX, float toX, float fromY, float toY,
            float pivotX, float pivotY) {
        mResources = null;
        mFromX = fromX;
        mToX = toX;
        mFromY = fromY;
        mToY = toY;

        mPivotXType = ABSOLUTE;
        mPivotYType = ABSOLUTE;
        mPivotXValue = pivotX;
        mPivotYValue = pivotY;
        initializePivotPoint();
    }

fromX, toX, fromY, toY这4个参数很好理解,我们重点看一下pivotX,pivotY是怎么计算的

– 从本身的位置缩放到另一个位置 这种情况下,我们关心的是缩放后的目标位置,这里有几个值需要先了解一些,目标view的右边(targetRight),初始view左边的距离(sourceLeft),pivotX,初始view的宽(sourceWidth),放大的值(toX),他们的关系如下 targetRight – sourceLeft = pivotX – (pivotX – sourceWidth) * toX,那么pivotX的值是pivotX = (targetRight – sourceLeft – sourceWidth * toX) / (1 – toX)

pivotY的值类似,就不在描述了。

– 从另一个位置缩放到本身的位置 这种情况我们关心的是开始的位置,它们的关系是sourceLeft – targetLeft = pivotX * (1 – scaleX),那么pivotX = (sourceLeft – targetLeft) / (1 – fromX)理清楚这个后,动画效果有缩放和移动的,只需要一个ScaleAnimation就能完成。

为了方便后期使用写了一个帮助类

代码语言:javascript
复制
public class TranslateAnimHelper {
    public static Animation tanslateScaleAnim(boolean fromOrigin, Rect sourceRect, Rect targetRect){
        Animation anim = null;
        float sx = targetRect.width() * 1.0f / sourceRect.width();
        float sy = targetRect.height() * 1.0f / sourceRect.height();
        boolean isScale = sx != 1 || sy != 1;
        if(isScale){
            anim = scaleAnim(fromOrigin, sourceRect, targetRect);
        }else{
            if(fromOrigin){
                int fromDeltaX = 0;
                int toDeltaX = targetRect.left - sourceRect.left;
                int fromDeltaY = 0;
                int toDeltaY = targetRect.top - sourceRect.top;
                anim = new TranslateAnimation(fromDeltaX, toDeltaX, fromDeltaY, toDeltaY);
            }else {
                int fromDeltaX = -(targetRect.left - sourceRect.left);
                int toDeltaX = 0;
                int fromDeltaY = -(targetRect.top - sourceRect.top);
                int toDeltaY = 0;
                anim = new TranslateAnimation(fromDeltaX, toDeltaX, fromDeltaY, toDeltaY);
            }
        }
        return anim;
    }
    public static Animation scaleAnim(boolean fromOrigin, Rect sourceRect, Rect targetRect){
        float sx = targetRect.width() * 1.0f / sourceRect.width();
        float sy = targetRect.height() * 1.0f / sourceRect.height();
        Animation animation = null;
        if(fromOrigin){
            float fromX = 1;
            float toX = sx;
            float fromY = 1;
            float toY = sy;
            float px = (targetRect.right - sourceRect.left - sourceRect.width() * sx) / (1 - toX);
            float py = (targetRect.bottom - sourceRect.top - sourceRect.height() * sy) / (1 - toY);
            animation = new ScaleAnimation(fromX, toX, fromY, toY, px, py);
        }else{
            float fromX =  1 / sx;
            float toX = 1;
            float fromY = 1 / sy;
            float toY = 1;
            float px = (sourceRect.left - targetRect.left) / (1 - fromX);
            float py = (sourceRect.top - targetRect.top) / (1 - fromY);
            animation = new ScaleAnimation(fromX, toX, fromY, toY, px, py);
        }
        return animation;
    }
}

github下载地址

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/181773.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年10月15日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档