天天看點

超簡單實作ViewPage引導頁動畫

先上效果

超簡單實作ViewPage引導頁動畫
public class MainActivity extends FragmentActivity {

    private ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = (ViewPager) findViewById(R.id.vp);

        MyFragmentStatePagerAdapter adapter = new MyFragmentStatePagerAdapter(getSupportFragmentManager());

        viewPager.setPageTransformer(true, new MyTrans());
        viewPager.setAdapter(adapter);
    }

    class MyFragmentStatePagerAdapter extends FragmentStatePagerAdapter{

        public MyFragmentStatePagerAdapter(FragmentManager fm) {
            super(fm);
        }
        @Override
        public Fragment getItem(int arg0) {
            //執行個體化5個fragment
            Fragment f = new TranslatePageFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("position", arg0);
            f.setArguments(bundle);

            return f;
        }
        @Override
        public int getCount() {
            return ;
        }
    }
}
           
public class TranslatePageFragment extends Fragment {

    private int[] icons = {
        R.drawable.mm0,
        R.drawable.mm1,
        R.drawable.mm2,
        R.drawable.mm3
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.viewpager_main, null);
        ImageView iv = (ImageView) view.findViewById(R.id.iv);
        Bundle bundle = getArguments();
        int position = bundle.getInt("position");

        iv.setImageResource(icons[position]);

        return view;
    }

}
           
public class MyTrans implements PageTransformer {

    /***
     * 每一個view在滑動的過程當中都會回調這個方法
     * view:目前滑動的
     * 
     */
    @SuppressLint("NewApi")
    @Override
    public void transformPage(View view, float position) {

        if(position < -){
            //不可見
            view.setAlpha();

        }else if(position <= ){
            //可見
            view.setAlpha();
            ImageView iv = (ImageView) view.findViewById(R.id.iv);
            //第一種效果:朝反方向行駛一段距離
            //iv.setTranslationX(-position*0.6f*view.getWidth());

            //第二種效果:縮放 position 0~-1
            //view.setScaleX(Math.max(0.9f,1-Math.abs(position)));
            //view.setScaleY(Math.max(0.9f,1-Math.abs(position)));

            //第三種效果:立體翻轉----外翻
            //旋轉角度90f----旋轉中心點
            /*view.setPivotX(position<0f?view.getWidth():0f);
            view.setPivotY(view.getHeight()*0.5f);
            view.setRotationY(position*90f);
            */

            //第四種效果:繞着正中間旋轉
            view.setPivotX(view.getWidth()*f);
            view.setPivotY(view.getHeight()*f);
            view.setRotationY(-position*f);


        }else{
            //不可見
            view.setAlpha();
        }

    }

}
           

就是這麼簡單,就是這麼任性輕松搞定,做個記錄