且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android如何根据视图在屏幕上的位置缩放动画并将视图集中在中心?

更新时间:2023-10-24 10:53:52

您需要获取要位于中心的视图/内容的位置,因为您需要进行一些翻译

you need to get position of the view/content that you want to be in center, because you need to do some translation

此处示例将ConstraintView内具有随机位置的文本缩放到TextView的中心

here example zoom to center of TextView with random position inside a ConstraintView

要放大到需要平移视图的特定位置,以便该点位于中心,可以通过更改difX和difY公式来实现

to zoom in to the specific position you need to translate view so the point is in the center, you can do it by change the difX and difY formula

float layoutWitdh = layout.getWidth();
        float layoutHeight = layout.getHeight();

        float x = view.getX();
        float y = view.getY();
        float w = view.getWidth();
        float h = view.getHeight();

        float difX = (layoutWitdh - w) / 2 - x;
        float difY = (layoutHeight - h) / 2 - y;

        view.animate().translationX(difX).translationY(difY).scaleY(5).scaleX(5).setDuration(1000);

检查此项目 https://github.com/aiwiguna/AndroidZoom