2015-12-21 28 views
1

我一直在試圖關注android flip card tutorial。按照指示,我創建了四個自定義動畫設置Android套裝自定義動畫

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> 

而且,我有這個在我的片段:

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; 
} 

}

但是,我得到了使用

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

我得到的反饋語法錯誤(紅色拉鍊線)是「預期的類型anim資源」 任何人有任何想法,爲什麼這不工作?

+0

我得到了相同的結果,可能是因爲他們的例子使用了'getFragmentManager()'而我們合理的程序員正試圖使用​​'getSupportFragmentManager()' –

+0

[Android中的自定義動畫](http://www.singhajit.com/ Android的定製動畫/) –

回答

1

根源:

谷歌的例子使用getFragmentManager(),我們明智的程序員正在嘗試使用getSupportFragmentManager()

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

總之不兼容,谷歌創建了一個無用的卡翻轉演示。如果你四處搜尋,你會發現很多人遇到了同樣的問題。


解決方案:使用「Android的支持庫V4與NineOldAndroids」庫 https://stackoverflow.com/a/18511350/550471

當然這是有道理的,谷歌的支持庫的需求與暴露功能的支持。 LOL