Advertisement
DuongTuHuy

viewpager animation

Jan 13th, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.94 KB | None | 0 0
  1. /*
  2.  * Copyright 2012 The Android Open Source Project
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *     http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16.  
  17. package com.example.animationdemo;
  18.  
  19.  
  20. import android.content.Context;
  21. import android.content.Intent;
  22. import android.os.Bundle;
  23. import android.support.v4.app.Fragment;
  24. import android.support.v4.app.FragmentActivity;
  25. import android.support.v4.app.FragmentManager;
  26. import android.support.v4.app.FragmentStatePagerAdapter;
  27. import android.support.v4.app.NavUtils;
  28. import android.support.v4.view.PagerAdapter;
  29. import android.support.v4.view.ViewPager;
  30. import android.view.LayoutInflater;
  31. import android.view.Menu;
  32. import android.view.MenuItem;
  33. import android.view.View;
  34. import android.view.ViewGroup;
  35. import android.widget.ImageView;
  36.  
  37. /**
  38.  * Demonstrates a "screen-slide" animation using a {@link ViewPager}. Because {@link ViewPager}
  39.  * automatically plays such an animation when calling {@link ViewPager#setCurrentItem(int)}, there
  40.  * isn't any animation-specific code in this sample.
  41.  *
  42.  * <p>This sample shows a "next" button that advances the user to the next step in a wizard,
  43.  * animating the current screen out (to the left) and the next screen in (from the right). The
  44.  * reverse animation is played when the user presses the "previous" button.</p>
  45.  *
  46.  * @see ScreenSlidePageFragment
  47.  */
  48. public class ScreenSlideActivity extends FragmentActivity {
  49.     /**
  50.      * The number of pages (wizard steps) to show in this demo.
  51.      */
  52.     private static final int NUM_PAGES = 5;
  53.  
  54.     /**
  55.      * The pager widget, which handles animation and allows swiping horizontally to access previous
  56.      * and next wizard steps.
  57.      */
  58.     private ViewPager mPager;
  59.  
  60.     /**
  61.      * The pager adapter, which provides the pages to the view pager widget.
  62.      */
  63.     private PagerAdapter mPagerAdapter;
  64.  
  65.     @Override
  66.     protected void onCreate(Bundle savedInstanceState) {
  67.         super.onCreate(savedInstanceState);
  68.         setContentView(R.layout.activity_screen_slide);
  69.  
  70.         // Instantiate a ViewPager and a PagerAdapter.
  71.         mPager = (ViewPager) findViewById(R.id.pager);
  72. //        mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
  73.         mPagerAdapter = new MyPagerAdapter(this);
  74.         mPager.setAdapter(mPagerAdapter);
  75.         mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
  76.             @Override
  77.             public void onPageSelected(int position) {
  78.                 // When changing pages, reset the action bar actions since they are dependent
  79.                 // on which page is currently active. An alternative approach is to have each
  80.                 // fragment expose actions itself (rather than the activity exposing actions),
  81.                 // but for simplicity, the activity provides the actions in this sample.
  82.                 invalidateOptionsMenu();
  83.             }
  84.         });
  85.         mPager.setPageTransformer(true, new ZoomOutPageTransformer());
  86.     }
  87.  
  88.     @Override
  89.     public boolean onCreateOptionsMenu(Menu menu) {
  90.         super.onCreateOptionsMenu(menu);
  91.         getMenuInflater().inflate(R.menu.activity_screen_slide, menu);
  92.  
  93.         menu.findItem(R.id.action_previous).setEnabled(mPager.getCurrentItem() > 0);
  94.  
  95.         // Add either a "next" or "finish" button to the action bar, depending on which page
  96.         // is currently selected.
  97.         MenuItem item = menu.add(Menu.NONE, R.id.action_next, Menu.NONE,
  98.                 (mPager.getCurrentItem() == mPagerAdapter.getCount() - 1)
  99.                         ? R.string.action_finish
  100.                         : R.string.action_next);
  101.         item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
  102.         return true;
  103.     }
  104.  
  105.     @Override
  106.     public boolean onOptionsItemSelected(MenuItem item) {
  107.         switch (item.getItemId()) {
  108.             case android.R.id.home:
  109.                 // Navigate "up" the demo structure to the launchpad activity.
  110.                 // See http://developer.android.com/design/patterns/navigation.html for more.
  111.                 NavUtils.navigateUpTo(this, new Intent(this, MainActivity.class));
  112.                 return true;
  113.  
  114.             case R.id.action_previous:
  115.                 // Go to the previous step in the wizard. If there is no previous step,
  116.                 // setCurrentItem will do nothing.
  117.                 mPager.setCurrentItem(mPager.getCurrentItem() - 1);
  118.                 return true;
  119.  
  120.             case R.id.action_next:
  121.                 // Advance to the next step in the wizard. If there is no next step, setCurrentItem
  122.                 // will do nothing.
  123.                 mPager.setCurrentItem(mPager.getCurrentItem() + 1);
  124.                 return true;
  125.         }
  126.  
  127.         return super.onOptionsItemSelected(item);
  128.     }
  129.  
  130.     /**
  131.      * A simple pager adapter that represents 5 {@link ScreenSlidePageFragment} objects, in
  132.      * sequence.
  133.      */
  134.     private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
  135.         public ScreenSlidePagerAdapter(FragmentManager fm) {
  136.             super(fm);
  137.         }
  138.  
  139.         @Override
  140.         public Fragment getItem(int position) {
  141.             return ScreenSlidePageFragment.create(position);
  142.         }
  143.  
  144.         @Override
  145.         public int getCount() {
  146.             return NUM_PAGES;
  147.         }
  148.     }
  149.    
  150.     private class MyPagerAdapter extends PagerAdapter{
  151.         private Context mContext;
  152.         private android.support.v4.view.ViewPager.LayoutParams mParams;
  153.        
  154.         public MyPagerAdapter(Context context){
  155.             mContext = context;
  156.             mParams = new android.support.v4.view.ViewPager.LayoutParams();        
  157.         }
  158.        
  159.         @Override
  160.         public int getCount() {
  161.             // TODO Auto-generated method stub
  162.             return 3;
  163.         }
  164.  
  165.         @Override
  166.         public boolean isViewFromObject(View view, Object object) {
  167.             // TODO Auto-generated method stub
  168.             return view == object;
  169.         }
  170.        
  171.         @Override
  172.         public void destroyItem(ViewGroup container, int position, Object object) {
  173.             // TODO Auto-generated method stub
  174. //          super.destroyItem(container, position, object);
  175.         }
  176.        
  177.         @Override
  178.         public Object instantiateItem(ViewGroup container, int position) {
  179.             // TODO Auto-generated method stub
  180.  
  181.             View view = LayoutInflater.from(mContext).inflate(R.layout.program_item, null);
  182.             view.setLayoutParams(mParams);
  183.             ((ImageView) view.findViewById(R.id.img)).setImageResource(R.drawable.default_program);
  184.             container.addView(view);
  185.             return view;
  186.         }
  187.        
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement