我目前在使用Xamarin(C#)进行开发,但Java解决方案也可以。
我的一个自定义父视图包含一个子视图。父视图具有固定的大小和位置,子视图应该在用户点击父视图处展开以显示出来。反应展开动画是一个简单的淡入淡出和缩放,应该围绕子元素中心旋转。
我当前看到的结果是,子元素总是从父元素的左上角开始动画,似乎沿着适当的比例尺度和淡化效果向用户点击的点移动。这让我想到动画枢轴受到了干扰。
我该如何移动子元素并确保仍然围绕本地中心点进行动画,以及到底是什么原因导致了我当前看到的行为?
编辑:我已经发现导致这种行为的根本原因是动画也会缩放子元素的x和y坐标以及宽度和高度,因此当缩放为0时,子元素出现在父元素的原点,当缩放为1时,它才出现在正确的位置。我的新问题是:我可以使用哪种方法将子元素绝对定位在父元素中,而不受缩放的影响?
谢谢。
父视图类
public class Parent: RelativeLayout
{
private RelativeLayout _child;
private Animation _animation;
private LayoutParams _layoutParams;
public Parent(Context context, IAttributeSet attrs)
: base(context, attrs)
{
_layoutParams = new RelativeLayout.LayoutParams(64, 64);
_child = new RelativeLayout(context);
_child.LayoutParameters = _layoutParams;
_child.SetBackgroundColor(Color.Red);
_child.Visibility = ViewStates.Invisible;
this.AddView(_child);
_animation = AnimationUtils.LoadAnimation(context, Resource.Animation.Expand);
_animation.AnimationEnd += OnAnimationEnd;
this.Touch += OnTouch;
}
private void OnTouch(object sender, View.TouchEventArgs e)
{
// position the top left of the child view such that
// the center point lies over the touch point
var x = e.Event.GetX() - (_child.Width / 2);
var y = e.Event.GetY() - (_child.Height / 2);
_child.SetX(x);
_child.SetY(y);
_child.StartAnimation(_animation);
_child.Visibility = ViewStates.Visible;
}
}
扩展动画资源
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator"
android:shareInterpolator="true"
android:duration="500" >
<scale
android:fromXScale="0"
android:fromYScale="0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1"
android:toYScale="1" >
</scale>
<alpha
android:fromAlpha="0"
android:toAlpha="1" />
</set>