在ViewPager of Fragments中,碎片无法像卡片那样翻转

时间:2015-06-28 23:06:53

标签: java android animation android-fragments android-viewpager

简介

我正在创建一个具有多个图像/片段的开始菜单的应用程序。 我希望我点击的每个图像都可以进行卡片翻转动画并转换为另一个图像(其中有一些ImageButtons可以引导您进入主应用程序)

问题

我尝试按照this来实施动画 但每当我点击图像/片段时,就会出现一个问题,经过对网络的一些研究后我发现它是因为尝试对android.support.v4.app.Fragments上的Fragments执行一个动作,logcat文件说:

  

java.lang.RuntimeException:未知的动画名称:objectAnimator

主要活动类

public class MainMenuActivity extends FragmentActivity {
/**
 * the pager which handles animation and lets up swipe horizontally to access previous and next memo game
 */
ViewPager viewPager;

/**
 * the pager adapter which provides the pages to the pager widget
 */
PagerAdapter adapter;

/**
 * probably useless but we'll see in the future
 */
int[] game;

/**
 * the number of the memo games provided in the version
 */
public static final int NUM_OF_GAMES=8;
/**
 * the number of the position where the pager is
 */
public static int POSITION;
public Context context;
/**
 * whether or not we're showing the back of the card (otherwise showing the front)
 */
private boolean mShowingBack = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    //remove the title
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    //making the window full screen
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main_menu);

    context=this.getApplicationContext();

    viewPager=(ViewPager)findViewById(R.id.pager);
    adapter = new MemoGameFragmentPagerAdapter(getSupportFragmentManager());
    viewPager.setAdapter(adapter);

    viewPager.setOffscreenPageLimit(6);
    viewPager.setPageMargin(20);
}

private class MemoGameFragmentPagerAdapter extends FragmentStatePagerAdapter{
    public MemoGameFragmentPagerAdapter(android.support.v4.app.FragmentManager fm){
        super(fm);
    }

    @Override
    public Fragment getItem(int position){
        MainMenuActivity.POSITION=position;
        return new MemoFrontFragment();
    }

    @Override
    public int getCount(){
        return NUM_OF_GAMES;
    }

    @Override
    public float getPageWidth(int position){
        return (0.4f);
    }
}


public void flipMemoGame(){
    //the card is on its back side
    if(mShowingBack){
        android.support.v4.app.FragmentManager fm =getSupportFragmentManager();
        fm.popBackStack();
        return;
    }

    //the card is on its front side
    mShowingBack=true;
    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.pager,new MemoBackFragment()).addToBackStack(null).commit();
}

public void setOnClickListeners(View v){
    ImageView img;
    img=(ImageView)v;
    img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            flipMemoGame();
        }
    });
}}

片段Java文件

public class MemoFrontFragment extends Fragment {

static ImageView IMAGE;
int[] game;
int position=MainMenuActivity.POSITION;
ImageView img;
View view;

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){

    game = new int[]{R.drawable.img,R.drawable.img0,R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4,R.drawable.img6,R.drawable.img7};

        view=inflater.inflate(R.layout.fragment_memo_front_game_1,container,false);
    img=(ImageView)view.findViewById(R.id.united_states_flag);
    img.setImageResource(game[position]);
    IMAGE=img;
    return view;
}
}

其他片段文件

public class MemoBackFragment extends Fragment {
    View view;

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
        view=inflater.inflate(R.layout.fragment_memo_back_game_1,container,false);
        return view;
    }
}

如果您需要其他任何内容,请告诉我,我会尽快提供

1 个答案:

答案 0 :(得分:0)

显然,当您使用支持库时,您必须使用较旧的属性/视图动画(R.anim.*),对象动画(R.animator.*)将无法与支持库一起使用。

所以只是为了比较,/ res / anim / card_flip_left_in.xml可能看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:fromAlpha="1.0"
        android:toAlpha="0.0"
        android:duration="0" />
    <rotate
        android:fromDegrees="-180"
        android:toDegrees="0"
        android:interpolator="@android:interpolator/accelerate_decelerate"
        android:duration="@integer/card_flip_time_full"
        />
    <alpha
        android:fromAlpha="0.0"
        android:toAlpha="1.0"
        android:startOffset="@integer/card_flip_time_half"
        android:duration="1" />
</set>