Tabhost + framgent + ViewPager sliding effect, tabhostframgent
I learned about the page sliding effect of Tabhost + framgent + ViewPager
: Https://github.com/asijack/TabhostDemo-framgent-ViewPager
Effect
Pay attention to the following points:
1. offset calculation:
// Initialize the image's displacement pixel public void InitImage () {image = (ImageView) findViewById (R. id. cursor); // the image can be viewed and modified only when the Bitmap object is loaded. decodeResource obtains the Bitmap object BMP = BitmapFactory. decodeResource (getResources (), R. drawable. cours ). getWidth (); // Android can be set to adjust the zoom ratio with the window size, but even so, the mobile phone program designer must still know the boundary of the mobile phone screen, to avoid layout deformation caused by scaling. // The resolution information of the mobile phone is an important information of the mobile phone. It is good that the DisplayMetircs class has been provided by Android to conveniently obtain the resolution. // GetWindowManager (). getdefadisplay display (). getMetrics; // The constructor DisplayMetrics does not need to pass any parameters. After getWindowManager () is called, the Handle of the existing Activity is obtained. // getDefaultDisplay () the method stores the obtained width and height dimensions in the DisplayMetrics object, and the obtained width and height dimensions are measured in pixels) // "pixels" refer to "absolute pixels" rather than "relative pixels ". DisplayMetrics dm = new DisplayMetrics (); getWindowManager (). getdefadisplay display (). getMetrics (dm); // get the width of the device. int screenW = dm. widthPixels; offset = (screenW/4-bmp w)/2; // offset of 4 items // offset = (screenW-2 * bmp w)/4; // offset of the two items // imageView sets the translation so that the underline is translated to the initial position (Translating an offset) Matrix matrix = new Matrix (); // The operation of the Matrix, there are four types of transformations: translate (translation), rotate (rotation), scale (scaling), and skew (skew). // each transform provides set in the Android API, post and pre operations, except trans Late. You can specify the center point for all the other three operations. // Set directly sets the value of the Matrix. The array of the entire Matrix is changed every time the Matrix is set. // Post is the posterior multiplication. The current matrix is multiplied by the matrix given by the parameter. You can use post multiple times in a row to complete the entire transformation. Matrix. postTranslate (offset, 0); image. setImageMatrix (matrix );}
2. Implementation of sliding Animation
// View page sliding listener public class MyOnPageChanceListener implements OnPageChangeListener {// this method is called when the status changes. The arg0 parameter has three states: 0, 1, 2 = 1 indicates sliding. = 2 indicates sliding is complete. = 0 indicates nothing is done. // when the page starts to slide, the change sequence of the three States is (1, 2, 0) @ Override public void onPageScrollStateChanged (int arg0) {}// this method is called when the page slides. Before the sliding stops, this method will always get the call // arg0 current page, and the percentage of the arg1 current page that you click to slide the arg2 current page offset pixel position @ Override public void onPageScrolled (int arg0, float arg1, int arg2) {}// this method is used to obtain the position (position number) of the currently selected page after the page Jump) @ Override public void onPageSelected (int arg0) {int one = offset * 2 + bmp w; // offset of the two adjacent pages Animation animation = new TranslateAnimation (currIndex * one, arg0 * one, 0, 0); // translation animation currIndex = arg0; animation. setFillAfter (true); // when the animation is aborted, it stays at the last frame. Otherwise, it returns to the animation state before execution. setDuration (200); // animation Duration: 0.2 seconds image. startAnimation (animation); int I = currIndex + 1; Toast. makeText (MainActivity. this, "you have selected the page card" + I + ", Toast. LENGTH_SHORT ). show ();}}
If you are interested, you can achieve the animation you want here.
3. The last one is the MyFragmentPagerAdapter class.
/*** 1. Google officially believes that ViewPager should be used together with Fragment. In this case, ViewPager's adapter is FragmentPagerAdapter * When you implement a FragmentPagerAdapter, you must cover at least the following methods: getCount () getItem () 2. If ViewPager is not associated with Fragment, the ViewPager adapter is PagerAdapter. It is a base class that provides an adapter to fill the page inside ViewPager. When you implement a PagerAdapter, you must cover at least the following methods: instantiateItem (ViewGroup, int) destroyItem (ViewGroup, int, Object) getCount () isViewFromObject (View, Object) */public class MyFragmentPagerAdapter extends FragmentPagerAdapter {ArrayList <Fragment> list; public MyFragmentPagerAdapter (FragmentManager fm, ArrayList <Fragment> list) {super (fm); this. list = list ;}@ Override public Fragment getItem (int arg0) {return list. get (arg0) ;}@ Override public int getCount () {return list. size ();}}
However, this is not what I want. I will post a demo of the effect I want later.
If you have any questions, please feel free to contact us.
:
Https://github.com/asijack/TabhostDemo-framgent-ViewPager