Android设置自定义动画 [英] Android set Custom Animations
问题描述
我一直在尝试遵循android 翻转卡教程。按照指示,我创建了四个自定义动画集
card_flip_right_in.xml:
< set xmlns:android =http://schemas.android.com/apk/res/android>
android:valueFrom =1.0
android:valueTo =0.0
android:propertyName =alpha
android:duration =0 />
android:valueFrom =180
android:valueTo =0
android:propertyName =rotationY
android: interpolator =@ android:interpolator / accelerate_decelerate
android:duration =300/>
android:valueFrom =0.0
android:valueTo =1.0
android:propertyName =alpha
android: startOffset =150
android:duration =1/>
< / set>
card_flip_right_out.xml:
< set xmlns:android =http://schemas.android.com/apk/res/android>
android:valueFrom =0
android:valueTo = - 180
android:propertyName =rotationY
android:interpolator = @android:interpolator / accelerate_decelerate
android:duration =300/>
android:valueFrom =1.0
android:valueTo =0.0
android:propertyName =alpha
android: startOffset =150
android:duration =1/>
< / set> b
$ b > < set xmlns:android =http://schemas.android.com/apk/res/android>
android:valueFrom =1.0
android:valueTo =0.0
android:propertyName =alpha
android: duration =0/>
< objectAnimator
android:valueFrom = - 180
android:valueTo =0
android:propertyName =rotationY
android :interpolator =@ android:interpolator / accelerate_decelerate
android:duration =300/>
android:valueFrom =0.0
android:valueTo =1.0
android:propertyName =alpha
android: startOffset =150
android:duration =1/>
< / set> b
$ b > < set xmlns:android =http://schemas.android.com/apk/res/android>
android:valueFrom =0
android:valueTo =180
android:propertyName =rotationY
android:interpolator =@ android:interpolator / accelerate_decelerate
android:duration =300/>
android:valueFrom =1.0
android:valueTo =0.0
android:propertyName =alpha
android: startOffset =150
android:duration =1/>
< / set>
而且,我在片段中提供了这个:
public class ContestantInfoFragment extends Fragment {
private context context;
私人字符串cName,cCountry,cDesc;
私人TextView contestant_name,contestant_country,contestant_desc;
私人ImageButton flip_btn;
public ContestantInfoFragment(){
}
$ b $ @覆盖
public View onCreateView(LayoutInflater inflater,ViewGroup容器,
Bundled savedInstanceState){
查看v = inflater.inflate(R.layout.fragment_contestant_info,container,false);
context = inflater.getContext();
Bundle bundle = this.getArguments();
if(bundle!= null){
cName = bundle.getString(NAME,Problem loading contestant's name);
cCountry = bundle.getString(COUNTRY,Problem loading contestant's country);
cDesc = bundle.getString(DESC,问题加载参赛者的描述);
contestant_name =(TextView)v.findViewById(R.id.contestant_name);
contestant_country =(TextView)v.findViewById(R.id.contestant_country);
contestant_desc =(TextView)v.findViewById(R.id.contestant_desc);
contestant_name.setText(cName);
contestant_country.setText(cCountry);
contestant_desc.setText(cDesc);
}
flip_btn =(ImageButton)v.findViewById(R.id.contestant_flip_btn);
flip_btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
getActivity()。getSupportFragmentManager()
.beginTransaction ()
.setCustomAnimations(R.animator.card_flip_right_in,R.animator.card_flip_right_out,
R.animator.card_flip_left_in,R.animator.card_flip_left_out)
.replace(R.id.mainContent,新的ContestantsFragment())
.addToBackStack(null)
.commit();
}
});
return v;
}
}
但是,使用
.setCustomAnimations(R.animator.card_flip_right_in,R.animator。 card_flip_right_out,
R.animator.card_flip_left_in,R.animator.card_flip_left_out)
反馈I得到的是期望资源的类型anim
任何人有任何想法,为什么这不起作用?
>根本原因:
Google的示例使用 getFragmentManager()
,我们合理的程序员试图使用 getSupportFragmentManager()
。
不幸的是, getSupportFragmentManager()。setCustomAnimations()
签名是不同的。
此外, objectAnimator
不兼容 getSupportFragmentManager()。setCustomAnimations()
总之, Google创建了一个无用的卡片翻转演示。如果你四处搜索,你会发现很多人都遇到了同样的问题。
解决方案:使用带有NineOldAndroids的Android支持库v4库
https://stackoverflow.com/a/ 18511350/550471
当然,Google的支持库需要支持暴露功能。 LOL
I've been trying to follow the android flip card tutorial. As instructed, I created four custom animator sets
card_flip_right_in.xml:
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:valueFrom="1.0"
android:valueTo="0.0"
android:propertyName="alpha"
android:duration="0" />
<objectAnimator
android:valueFrom="180"
android:valueTo="0"
android:propertyName="rotationY"
android:interpolator="@android:interpolator/accelerate_decelerate"
android:duration="300" />
<objectAnimator
android:valueFrom="0.0"
android:valueTo="1.0"
android:propertyName="alpha"
android:startOffset="150"
android:duration="1" />
</set>
card_flip_right_out.xml:
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:valueFrom="0"
android:valueTo="-180"
android:propertyName="rotationY"
android:interpolator="@android:interpolator/accelerate_decelerate"
android:duration="300" />
<objectAnimator
android:valueFrom="1.0"
android:valueTo="0.0"
android:propertyName="alpha"
android:startOffset="150"
android:duration="1" />
</set>
card_flip_left_in.xml:
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:valueFrom="1.0"
android:valueTo="0.0"
android:propertyName="alpha"
android:duration="0" />
<objectAnimator
android:valueFrom="-180"
android:valueTo="0"
android:propertyName="rotationY"
android:interpolator="@android:interpolator/accelerate_decelerate"
android:duration="300" />
<objectAnimator
android:valueFrom="0.0"
android:valueTo="1.0"
android:propertyName="alpha"
android:startOffset="150"
android:duration="1" />
</set>
card_flip_left_out.xml:
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:valueFrom="0"
android:valueTo="180"
android:propertyName="rotationY"
android:interpolator="@android:interpolator/accelerate_decelerate"
android:duration="300" />
<objectAnimator
android:valueFrom="1.0"
android:valueTo="0.0"
android:propertyName="alpha"
android:startOffset="150"
android:duration="1" />
</set>
And, I have this in my fragment:
public class ContestantInfoFragment extends Fragment {
private Context context;
private String cName, cCountry, cDesc;
private TextView contestant_name, contestant_country, contestant_desc;
private ImageButton flip_btn;
public ContestantInfoFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_contestant_info, container, false);
context = inflater.getContext();
Bundle bundle = this.getArguments();
if(bundle != null) {
cName = bundle.getString("NAME", "Problem loading contestant's name");
cCountry = bundle.getString("COUNTRY", "Problem loading contestant's country");
cDesc = bundle.getString("DESC", "Problem loading contestant's description");
contestant_name = (TextView) v.findViewById(R.id.contestant_name);
contestant_country = (TextView) v.findViewById(R.id.contestant_country);
contestant_desc = (TextView) v.findViewById(R.id.contestant_desc);
contestant_name.setText(cName);
contestant_country.setText(cCountry);
contestant_desc.setText(cDesc);
}
flip_btn = (ImageButton) v.findViewById(R.id.contestant_flip_btn);
flip_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getActivity().getSupportFragmentManager()
.beginTransaction()
.setCustomAnimations(R.animator.card_flip_right_in, R.animator.card_flip_right_out,
R.animator.card_flip_left_in, R.animator.card_flip_left_out)
.replace(R.id.mainContent, new ContestantsFragment())
.addToBackStack(null)
.commit();
}
});
return v;
}
}
However, I got syntax errors (red zip lines) for using
.setCustomAnimations(R.animator.card_flip_right_in, R.animator.card_flip_right_out,
R.animator.card_flip_left_in, R.animator.card_flip_left_out)
The feedback I got was "Expected resource of type anim" Anyone has any idea why this is not working?
root cause:
Google's example uses getFragmentManager()
and we sensible programmers are trying to use getSupportFragmentManager()
.
Unfortunately, getSupportFragmentManager().setCustomAnimations()
signature is different.
Additionally, objectAnimator
is incompatible with getSupportFragmentManager().setCustomAnimations()
In short, Google created a useless card-flip demo. If you search around, you'll see that a lot of people have encountered the same problem.
solution: use of "Android Support Library v4 with NineOldAndroids" library https://stackoverflow.com/a/18511350/550471
of course it makes sense that Google's support library needs support with exposing functionality. LOL
这篇关于Android设置自定义动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!