Android如何根据视图在屏幕上的位置缩放动画并将视图集中在中心?
我想要这样的动画:日历动画
我想在日历上缩放动画,并将当前日期集中在中心. 我是android动画的新手,我做了一些缩放工作:
I want to scale animation on calendar and focus on current date to centre. I am new in android animation, I do some stuff for scaling:
zoom_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<scale
android:duration="1000"
android:fromXScale="1"
android:fromYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="2"
android:toYScale="2" />
</set>
但是使用它,它将日历缩放到中心,但是我希望当前日期在中心. 有人知道如何制作整个动画吗?任何帮助将不胜感激.
but using this, it scaling the calendar to centre but I want the current date in centre. Anybody have an idea how to do this whole animation? any help would be appreciated.
您需要获取要位于中心的视图/内容的位置,因为您需要进行一些翻译
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