如何在Xamarin.Android中使用ObjectAnimator以编程方式使用提供的PathData创建动画?
我尝试使用ObjectAnimator和PathData作为属性名创建动画 我使用的xml代码:如何在Xamarin.Android中使用ObjectAnimator以编程方式使用提供的PathData创建动画?,android,animation,xamarin.android,Android,Animation,Xamarin.android,我尝试使用ObjectAnimator和PathData作为属性名创建动画 我使用的xml代码: <objectAnimator android:duration="166" android:propertyName="pathData" android:valueFrom="M -7,-7 l 14,0 c 0,0 0,0 0,0 l 0,14 c 0,0 0,0 0,0 l -14,0 c 0,0 0,0 0,0 l 0,-14 c 0,0 0,0 0,0 Z M 7,-9 c 0,0
<objectAnimator
android:duration="166"
android:propertyName="pathData"
android:valueFrom="M -7,-7 l 14,0 c 0,0 0,0 0,0 l 0,14 c 0,0 0,0 0,0 l -14,0 c 0,0 0,0 0,0 l 0,-14 c 0,0 0,0 0,0 Z M 7,-9 c 0,0 -14,0 -14,0 c -1.1044921875,0 -2,0.8955078125 -2,2 c 0,0 0,14 0,14 c 0,1.1044921875 0.8955078125,2 2,2 c 0,0 14,0 14,0 c 1.1044921875,0 2,-0.8955078125 2,-2 c 0,0 0,-14 0,-14 c 0,-1.1044921875 -0.8955078125,-2 -2,-2 c 0,0 0,0 0,0 Z"
android:valueTo="M 0,-0.05 l 0,0 c 0.02761423749,0 0.05,0.02238576251 0.05,0.05 l 0,0 c 0,0.02761423749 -0.02238576251,0.05 -0.05,0.05 l 0,0 c -0.02761423749,0 -0.05,-0.02238576251 -0.05,-0.05 l 0,0 c 0,-0.02761423749 0.02238576251,-0.05 0.05,-0.05 Z M 7,-9 c 0,0 -14,0 -14,0 c -1.1044921875,0 -2,0.8955078125 -2,2 c 0,0 0,14 0,14 c 0,1.1044921875 0.8955078125,2 2,2 c 0,0 14,0 14,0 c 1.1044921875,0 2,-0.8955078125 2,-2 c 0,0 0,-14 0,-14 c 0,-1.1044921875 -0.8955078125,-2 -2,-2 c 0,0 0,0 0,0 Z"
android:valueType="pathType"/>
如何使用Xamarin.Android中的ObjectAnimator以编程方式使用提供的PathData创建动画?根据这一点,您需要重写以接受字符串,而不是TypedArray。为此,您需要安装nuget软件包。您可以使用以下方法安装它:
Install-Package VectorCompat -Version 1.0.5
在xamarin中,还需要创建PathDataEvaluator
。
例如,
PathDataEvaluator
:
public class PathDataEvaluator : Java.Lang.Object, ITypeEvaluator
{
private PathParser.PathDataNode[] mNodeArray;
private PathDataEvaluator() { }
public PathDataEvaluator(PathParser.PathDataNode[] nodeArray)
{
mNodeArray = nodeArray;
}
public Java.Lang.Object Evaluate(float fraction, Java.Lang.Object startValue, Java.Lang.Object endValue)
{
PathParser.PathDataNode[] startPathData =startValue.ToArray<PathParser.PathDataNode>();
PathParser.PathDataNode[] endPathData = endValue.ToArray<PathParser.PathDataNode>();
if (!PathParser.CanMorph(startPathData, endPathData))
{
throw new IllegalArgumentException("Can't interpolate between"
+ " two incompatible pathData");
}
if (mNodeArray == null || !PathParser.CanMorph(mNodeArray, startPathData))
{
mNodeArray = PathParser.DeepCopyNodes(startPathData);
}
for (int i = 0; i < startPathData.Length; i++)
{
mNodeArray[i].InterpolatePathDataNode(startPathData[i],
endPathData[i], fraction);
}
return mNodeArray;
}
}
ObjectAnimator objectAnimator = new ObjectAnimator();
objectAnimator.SetTarget(TARGET);
objectAnimator.PropertyName = "pathData";
PathDataEvaluator evaluator = null;
string fromString = "M -7,-7 l 14,0 c 0,0 0,0 0,0 l 0,14 c 0,0 0,0 0,0 l -14,0 c 0,0 0,0 0,0 l 0,-14 c 0,0 0,0 0,0 Z M 7,-9 c 0,0 -14,0 -14,0 c -1.1044921875,0 -2,0.8955078125 -2,2 c 0,0 0,14 0,14 c 0,1.1044921875 0.8955078125,2 2,2 c 0,0 14,0 14,0 c 1.1044921875,0 2,-0.8955078125 2,-2 c 0,0 0,-14 0,-14 c 0,-1.1044921875 -0.8955078125,-2 -2,-2 c 0,0 0,0 0,0 Z";
string toString = "M 0,-0.05 l 0,0 c 0.02761423749,0 0.05,0.02238576251 0.05,0.05 l 0,0 c 0,0.02761423749 -0.02238576251,0.05 -0.05,0.05 l 0,0 c -0.02761423749,0 -0.05,-0.02238576251 -0.05,-0.05 l 0,0 c 0,-0.02761423749 0.02238576251,-0.05 0.05,-0.05 Z M 7,-9 c 0,0 -14,0 -14,0 c -1.1044921875,0 -2,0.8955078125 -2,2 c 0,0 0,14 0,14 c 0,1.1044921875 0.8955078125,2 2,2 c 0,0 14,0 14,0 c 1.1044921875,0 2,-0.8955078125 2,-2 c 0,0 0,-14 0,-14 c 0,-1.1044921875 -0.8955078125,-2 -2,-2 c 0,0 0,0 0,0 Z";
PathParser.PathDataNode[] nodesFrom = PathParser.CreateNodesFromPathData(fromString);
PathParser.PathDataNode[] nodesTo = PathParser.CreateNodesFromPathData(toString);
if (nodesFrom != null)
{
if (nodesTo != null)
{
objectAnimator.SetObjectValues(nodesFrom, nodesTo);
if (!PathParser.CanMorph(nodesFrom, nodesTo))
{
throw new InflateException(" Can't morph from " + fromString + " to " + toString);
}
}
else
{
objectAnimator.SetObjectValues((Object)nodesFrom);
}
evaluator = new PathDataEvaluator(PathParser.DeepCopyNodes(nodesFrom));
}
else if (nodesTo != null)
{
objectAnimator.SetObjectValues((Object)nodesTo);
evaluator = new PathDataEvaluator(PathParser.DeepCopyNodes(nodesTo));
}
objectAnimator.SetEvaluator(evaluator);
objectAnimator.SetDuration(166);
objectAnimator.Start();
你可以参考这篇文章了解更多信息。你好,你的问题没有意义。你能清楚地知道你到底想做什么吗,动画好的,但是什么样的动画?我遇到了这个错误。。。obj\Debug\lp\3\jl\res\values-v21\values.xml:1:错误:检索项的父项时出错:未找到与给定名称“Widget.AppCompat.Button”匹配的资源。是否安装了
Xamarin.Android.Support.v7.AppCompat
软件包?是的,我仍然面临相同的问题issue@yoganathank您的目标api级别和最低api级别是什么?Android 5.1 API22