如何在android中的viewPager的每个页面中显示/启动动画?

时间:2016-12-31 14:57:28

标签: android animation android-viewpager slideshow

我希望在viewPager中将一些图像显示为非常简单的动画(ViewPager的每一页都有不同的动画)。 我的ViewPager代码:

public class Sport2Activity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sport1);

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);


   // tab layout for circle of view pager
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabDots);
    tabLayout.setupWithViewPager(mViewPager);

}

//********Adapter class*********************************************************
public class SectionsPagerAdapter extends FragmentPagerAdapter {
    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new EyeSportFragment();
                //return FragmentWithOneImage.newInstance("Fragment 1", R.drawable.android_1);
            case 1:
                return new FingerSportFragment();
            case 2:
                return new NeckSportFinger();
            case 3:
                return new WristSportFragment();
            case 4:
                return new ShoulderSportFragment();
            case 5:
                return new BackSportFragment();
            case 6:
                return new WaterFragment();
            default:
                return null;
        }
    }

    @Override
    public int getCount() { // Show 3 total pages.
        return 7;
    }

和动画类:

    public class BackSportFragment extends Fragment {
    private ImageView sportShowing_img;

    public BackSportFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_sport_show, container, false);
        sportShowing_img= (ImageView) view.findViewById(R.id.sport_img);



        AnimationDrawable animation = new AnimationDrawable();
        animation.addFrame(getResources().getDrawable(R.drawable.ic_action_hardware_keyboard_arrow_left), 1000); //1 Sec
        animation.addFrame(getResources().getDrawable(R.drawable.ic_action_action_home), 1000); //1 Sec
        animation.addFrame(getResources().getDrawable(R.drawable.cast_ic_notification_1), 1000); //1 Sec
        animation.addFrame(getResources().getDrawable(R.drawable.common_google_signin_btn_icon_light), 1000); //1 Sec
        animation.setOneShot(true);

        sportShowing_img.setBackgroundDrawable(animation);
        animation.start();

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                sportShowing_img.setVisibility(View.INVISIBLE);
            }
        }, 10000);
        return view;
    }

}

如何ViewPager通过点击/滚动每页,动画开始?

1 个答案:

答案 0 :(得分:1)

OnPageChangeListener

上实施Activity [回调界面,以响应所选页面的更改状态]界面

定义onPageSelected()方法,如下所示:

@Override
public abstract void onPageSelected (int position){
    Toast.makeText(getActivity(),"In the page " + position, 
            Toast.LENGTH_SHORT).show();
// depends on the position display your relevant animation
}

别忘了打电话

viewPager.setOnPageChangeListener(this);