我有问题。我使用以下代码创建了自定义动画:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<objectAnimator
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:propertyName="x"
android:valueFrom="0"
android:valueTo="-1000"
android:valueType="floatType" />
</set>
现在,它几乎可以像我想要的那样工作,因为我缺少屏幕的一部分。所以我的问题是:如何使用百分比而不是值制作动画?
编辑:
这是有关我的片段动画的代码:
var trans = FragmentManager.BeginTransaction();
DisplayMetrics metrics = Resources.DisplayMetrics;
float screenWidth = metrics.WidthPixels;//calculate screen width
double wantValue = 0.5;//set a percentages
ObjectAnimator SlideLeft = ObjectAnimator.OfFloat(trans, "translationX", 0, (float)(screenWidth * wantValue));
SlideLeft.SetDuration(500);
SlideLeft.Start();
ObjectAnimator SlideRight = ObjectAnimator.OfFloat(trans, "translationX", 0, (float)(screenWidth * -wantValue));
SlideLeft.SetDuration(500);
SlideLeft.Start();
trans.SetCustomAnimations(SlideRight, SlideLeft);
出现错误:
无法从“ Android.Animation.ObjectAnimator”转换为“ int”
答案 0 :(得分:0)
这在XML中是不可能做到的。但是,您可以在cs代码中设置百分比。
TextView textView = FindViewById<TextView>(Resource.Id.text_content);
// Here using a textView to have a test
DisplayMetrics metrics = Resources.DisplayMetrics;
float screenWidth = metrics.WidthPixels;//calculate screen width
double wantValue = 0.5;//set a percentages
ObjectAnimator objectAnimator = ObjectAnimator.OfFloat(textView, "translationX", 0, (float)(screenWidth*wantValue));
objectAnimator.SetDuration(3000);
objectAnimator.Start();
更新:
然后如何将其用于片段,因为我尝试使用trans.SetCustomAnimations(SlideRight,SlideLeft);
如果要使用 SetCustomAnimations 设置动画,则应该在 XML 中设置百分比。但是,如果不使用 objectAnimator ,则 >翻译。
slide_left_in.xml :
<?xml version="1.0" encoding="utf-8" ?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="-100%p"
android:toXDelta="0%p"
android:duration="500"/>
<alpha
android:fromAlpha="0.5"
android:toAlpha="1.0"
android:duration="500"/>
</set>
slide_left_out.xml :
<?xml version="1.0" encoding="utf-8" ?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0%p"
android:toXDelta="-100%p"
android:duration="500"/>
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.5"
android:duration="500"/>
</set>
slide_right_in.xml :
<?xml version="1.0" encoding="utf-8" ?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="100%p"
android:toXDelta="0%p"
android:duration="@integer/card_flip_time_full"/>
<alpha
android:fromAlpha="0.5"
android:toAlpha="1.0"
android:duration="@integer/card_flip_time_full"/>
</set>
slide_right_out.xml :
<?xml version="1.0" encoding="utf-8" ?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0%p"
android:toXDelta="100%p"
android:duration="500"/>
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.5"
android:duration="500"/>
</set>
最后,在代码中进行设置:
var details = Android.Support.V4.App.Fragment.FragmentManager.FindFragmentById(Resource.Id.details) as DetailsFragment;
var trans = Android.Support.V4.App.Fragment.FragmentManager.BeginTransaction();
trans.Replace(Resource.Id.details, details);
trans.SetCustomAnimations(Resource.Animation.slide_right_in,Resource.Animation.slide_left_out,Resource.Animation.slide_left_in,Resource.Animation.slide_right_out);
trans.Commit();
如果发生此错误:
动画师姓名未知:发生翻译
您需要更改为V4版本的片段:
using Fragment = Android.Support.V4.App.Fragment;