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

ScaleAnimation开始结束位置分析[通俗易懂]做项目的时候,需要用到动画,大小和位置都不一样。刚开始想到的是ScaleAnimation和TranslateAnimation进行组合,但实验后发现,目标位置始终不对,只用TranslateAnimation是没有问题,所以ScaleAnimation应该不只是进行了缩放经过查找资料,发现ScaleAnimation还进行起始位置的移动。ScaleAnimation分为两种情况,从本身的位置…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

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

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

先看一下处理后的效果
ScaleAnimation开始结束位置分析[通俗易懂]
看一下ScaleAnimation的构造函数

    /**
     * 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就能完成。

 

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

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.net/181773.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 利用python、tensorflow、opencv实现人脸识别(包会)!「建议收藏」

    利用python、tensorflow、opencv实现人脸识别(包会)!「建议收藏」 一,前言本人是机械专业在读硕士,在完成暑假实践的时候接触到了人脸识别,对这一实现很感兴趣,所以花了大概十天时间做出了自己的人脸识别。这篇文章应该是很详细的了所以帮你实现人脸识别应该没什么问题。先说本博文的最终要达到的效果:通过一系列操作,在摄像头的视频流中识别特定人的人脸,并且予以标记。本人通过网上资料的查询发现这类人脸识别,大多参考了一位日本程序员小哥的文章。链接:http…

    2025年7月25日
    3
  • html改色_国际色标代码

    html改色_国际色标代码在header标签内添加以下style样式即可使网页呈现灰色:<style>html{filter:progid:DXImageTransform.Microsoft.BasicImage(grayscale=1);filter:grayscale(100%);-webkit-filter:grayscale(100%);-moz-fi…

    2022年9月28日
    2
  • Java基础之增强型for循环

    Java基础之增强型for循环目录增强型for循环语法:for(ElementTypeelement:arrayName){};增强for循环的原理增强型for循环注意事项增强型for循环语法:for(ElementTypeelement:arrayName){};举个例子:(1)int[]numArray={1,2,3,4,5,6};for(inti:numArray…

    2022年6月17日
    43
  • docker镜像和docker容器的关系_docker基础镜像和项目镜像

    docker镜像和docker容器的关系_docker基础镜像和项目镜像一、docker常用命令#镜像名版本标签镜像id创建时间镜像大小REPOSITORYTAGIMAGEIDCREATEDSIZEhello-worldlatestfce289

    2022年8月16日
    8
  • haxm device is not found

    haxm device is not foundandriodstudio刚装完,都会忍不住跑一个helloworld~但是AVDManager里的虚拟设备会提示错误:haxmdeviceisnotfound.androidstudio–>Tools–>AVDManager–>+CreateVirtualDevice…–>VirtualDeviceConfiguration–>Phone–>随便点一个–>Next。出现这个问题:EnableVT-xin

    2022年6月28日
    53
  • 网站优化网络推广怎么做_网站推广公司

    网站优化网络推广怎么做_网站推广公司如何优化网站,网站推广优化一般流程

    2022年4月21日
    58

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号