android 在缩放动画后设置新的大小

标签 android animation android-framelayout scaletransform

我正在使用翻译和缩放动画。首先,我将框架布局转换到屏幕中心,然后使用布局参数将其位置参数设置为屏幕中心。那很好用!在翻译动画结束时,我运行缩放动画,我的布局缩放为原始大小的 2 倍。实际上我的框架布局(我正在制作动画)由按钮和 ImageView 组成。与在 android 中一样,动画不会转换 View ,它只会更改必须绘制到的位置。现在我的问题是我无法让我的按钮工作。因为他们实际上并不存在!

我通过在动画结束后设置其位置参数找到了平移动画的解决方案。将 View 永久移动到新位置。

但是在缩放动画的情况下,我必须更改布局的大小以及其中的子项。但它不起作用,因为我将原始高度宽度乘以缩放因子。这是我的比例动画代码。

ScaleAnimation scaleAnim = new ScaleAnimation(1.0f, 2.0f, 1.0f,
                    2.0f, Animation.RELATIVE_TO_SELF, 0.5f,
                    Animation.RELATIVE_TO_SELF, 0.5f);

            scaleAnim.setDuration(600);
            // scaleAnim.setFillEnabled(true);
            scaleAnim.setFillAfter(true);

            view.setAnimation(scaleAnim);
            view.startAnimation(scaleAnim);

            scaleAnim.setAnimationListener(new AnimationListener() {

                public void onAnimationStart(Animation animation) {


                }

                public void onAnimationRepeat(Animation animation) {


                }

                public void onAnimationEnd(Animation animation) {
                    FrameLayout.LayoutParams par = (FrameLayout.LayoutParams) view
                            .getLayoutParams();

                    par.height = view.getMeasuredHeight() * 2;
                    par.width = view.getMeasuredWidth() * 2;

                     view.setLayoutParams(par);
                    view.requestLayout();

                }
            });

请注意,setFillAfter(true) 和 setFillEnabled(true) 不是解决方案。

最佳答案

我会在上面发表评论,但我没有足够的代表。文档中的这一页解释了 View 动画系统和属性动画系统之间的区别。由此您可以制作 ObjectAnimator 和 AnimatorSet 对象,它们将移动按钮并编辑实际 View ,而不仅仅是 View 的绘制。

http://developer.android.com/guide/topics/graphics/prop-animation.html#views

The property animation system allow streamlined animation of View objects and offers a few advantages over the view animation system. The view animation system transformed View objects by changing the way that they were drawn. This was handled in the container of each View, because the View itself had no properties to manipulate. This resulted in the View being animated, but caused no change in the View object itself. This led to behavior such as an object still existing in its original location, even though it was drawn on a different location on the screen. In Android 3.0, new properties and the corresponding getter and setter methods were added to eliminate this drawback.

The property animation system can animate Views on the screen by changing the actual properties in the View objects. In addition, Views also automatically call the invalidate() method to refresh the screen whenever its properties are changed.

关于android 在缩放动画后设置新的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13698660/

相关文章:

android - SignalR 将在 android 应用程序和支持 Websocket 的 IIS 服务器之间使用哪种传输?

安卓模拟器声音——ubuntu

android - 如何更改 TextInputLayout 中的提示文本大小

android - 在android中设置 ImageView 的高度

Android:SurfaceView 和 Z 级问题

Android 应用程序出现错误 "BatteryStatsImpl: reading network stats"

iOS 10、Swift 3.0、动画不流畅

iOS Swift 如何同步图表绘制直线、渐变和圆形动画?

wpf - 如何防止动画被打断[WPF]

android - FrameLayout 单击事件未触发