gpt4 book ai didi

Android 设置自定义动画

转载 作者:行者123 更新时间:2023-11-30 01:41:47 25 4
gpt4 key购买 nike

我一直在努力关注android flip card tutorial .按照指示,我创建了四个自定义动画师集

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>

而且,我的 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;
}

但是,我在使用时遇到语法错误(红色 zipper 线)

.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)

我得到的反馈是“Expected resource of type anim”任何人都知道为什么这不起作用?

最佳答案

根本原因:

Google 的示例使用 getFragmentManager(),我们明智的程序员正在尝试使用 getSupportFragmentManager()

不幸的是,getSupportFragmentManager().setCustomAnimations() 签名不同。此外,objectAnimatorgetSupportFragmentManager().setCustomAnimations()

不兼容

简而言之,Google 创建了一个无用的卡片翻转演示。如果你四处搜索,你会发现很多人都遇到过同样的问题。


解决方案:使用“Android Support Library v4 with NineOldAndroids”库 https://stackoverflow.com/a/18511350/550471

当然,Google 的支持库需要支持公开功能是有道理的。哈哈

关于Android 设置自定义动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34404504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com