轉載自:http://blog.csdn.net/ye_scofield/article/details/44831357小夥伴們,時隔很久,我又開始來BB了,勿噴,

,今天繼續上次所說的引導頁之旅。其實想實現一個靜態的引導頁還是很容易的,就是一個ViewPager,但是想對每一頁實現動畫效果,比如第一頁有一幾朵雲在飄啊飄!

,想實現這種效果對只要了解過Animation動畫的人來說也不難實現。基於ViewPager,分別對每一頁<也就是ViewPager的Child View>添加Animation,就可以簡單實現一些動畫效果。今天,我在這裡不多贅述,就將ViewPager結合Animation製作動畫翻頁效果。剛開始還是一樣介紹一下代碼,重點講怎麼優化動畫引用,因為動畫引用的圖片資源很多,單個的動畫demo基本上都不會怎麼OOM,但是一旦整合進項目中,就很容易OOM,至於為什麼會這樣我也不多贅述,想知道的在下面評論也行,我會回答的。這次就拿模仿搜狗地圖6.3版本開啟動畫的demo來講解,先看效果,第一頁就是一個指針在轉動,第二頁那個小車從下面開向上面去,第三頁雲朵在飄動和小車在上下起伏,第四頁錢幣不斷的灑落進存儲罐。。。

代碼分析:引用之前的ViewPager翻頁框架,分別對每一頁添加動畫,源代碼會在下面給出鏈接。[java]view plaincopy

importjava.util.ArrayList;importjava.util.List;importandroid.app.Activity;importandroid.content.Context;importandroid.graphics.drawable.AnimationDrawable;importandroid.graphics.drawable.BitmapDrawable;importandroid.os.Bundle;importandroid.support.v4.view.PagerAdapter;importandroid.support.v4.view.ViewPager;importandroid.util.DisplayMetrics;importandroid.view.LayoutInflater;importandroid.view.MotionEvent;importandroid.view.View;importandroid.view.ViewGroup;importandroid.view.animation.AccelerateDecelerateInterpolator;importandroid.view.animation.Animation;importandroid.view.animation.AnimationUtils;importandroid.view.animation.LinearInterpolator;importandroid.view.animation.RotateAnimation;importandroid.view.animation.ScaleAnimation;importandroid.view.animation.TranslateAnimation;importandroid.widget.ImageView;importandroid.widget.Toast;publicclassMainActivityextendsActivityimplementsViewPager.OnPageChangeListener{publicContextcontext;publicstaticintscreenW,screenH;privatestaticfinalintVIEW_NO_1=0;privatestaticfinalintVIEW_NO_2=1;privatestaticfinalintVIEW_NO_3=2;privatestaticfinalintVIEW_NO_4=3;privatestaticfinalintVIEW_NO_5=4;//第1頁的資源,坐標staticImageViewmOnePointer;//第2頁的資源,坐標staticImageViewmTwoCar;//第3頁的資源,坐標staticImageViewmThreeCloudFast;staticImageViewmThreeCloudSlow;staticImageViewmThreeCarShadow;staticImageViewmThreeCar;//第4頁的資源,坐標staticImageViewmFourPig;staticImageViewmFourPigShadow;staticImageViewmFourCoin;staticImageViewmFourCoinPack;//第5頁的資源,坐標staticImageViewmFiveCar;staticImageViewmFiveCarShadow;staticImageViewmFiveCloudFast;staticImageViewmFiveCloudSlow;privateintpreIndex=0;privateViewPagermPager;privateMyViewPagerAdaptermPagerAdapter;List<View>list=newArrayList<View>();@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);context=this;DisplayMetricsmetric=newDisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(metric);screenW=metric.widthPixels;//屏幕寬度(像素)screenH=metric.heightPixels;//屏幕高度(像素)LayoutInflaterinflater=LayoutInflater.from(this);Viewview0=inflater.inflate(R.layout.guide_fragment_main_1,null,false);mOnePointer=(ImageView)view0.findViewById(R.id.one_pointer);Viewview1=inflater.inflate(R.layout.guide_fragment_main_2,null,false);Viewview2=inflater.inflate(R.layout.guide_fragment_main_3,null,false);Viewview3=inflater.inflate(R.layout.guide_fragment_main_4,null,false);Viewview4=inflater.inflate(R.layout.guide_fragment_main_5,null,false);list.add(view0);list.add(view1);list.add(view2);list.add(view3);list.add(view4);mPager=(ViewPager)findViewById(R.id.container);mPagerAdapter=newMyViewPagerAdapter(list);mPager.setAdapter(mPagerAdapter);mPager.setOnPageChangeListener(this);mPager.setPageTransformer(true,newtransforms.StackTransformer());animal(VIEW_NO_1);}publicclassMyViewPagerAdapterextendsPagerAdapter{privateList<View>mListViews;publicMyViewPagerAdapter(List<View>mListViews){this.mListViews=mListViews;//構造方法,參數是我們的頁卡,這樣比較方便。}@OverridepublicvoiddestroyItem(ViewGroupcontainer,intposition,Objectobject){Viewview=mListViews.get(position);BitmapDrawabledrawable=(BitmapDrawable)view.getBackground();if(drawable!=null){drawable.getBitmap().recycle();}switch(position){caseVIEW_NO_1:break;caseVIEW_NO_2://mTwoCar.getBackground().setCallback(null);break;caseVIEW_NO_3:BitmapDrawabled3_1=(BitmapDrawable)mThreeCar.getBackground();if(d3_1!=null){d3_1.getBitmap().recycle();}BitmapDrawabled3_2=(BitmapDrawable)mThreeCarShadow.getBackground();if(d3_2!=null){d3_2.getBitmap().recycle();}break;caseVIEW_NO_4://mFourCoin.getBackground().setCallback(null);BitmapDrawabled4_1=(BitmapDrawable)mFourCoinPack.getBackground();if(d4_1!=null){d4_1.getBitmap().recycle();}BitmapDrawabled4_2=(BitmapDrawable)mFourPig.getBackground();if(d4_2!=null){d4_2.getBitmap().recycle();}BitmapDrawabled4_3=(BitmapDrawable)mFourPigShadow.getBackground();if(d4_3!=null){d4_3.getBitmap().recycle();}break;caseVIEW_NO_5:BitmapDrawabled5_1=(BitmapDrawable)mFiveCar.getBackground();if(d5_1!=null){d5_1.getBitmap().recycle();}BitmapDrawabled5_2=(BitmapDrawable)mFiveCarShadow.getBackground();if(d5_2!=null){d5_2.getBitmap().recycle();}break;default:break;}container.removeView(mListViews.get(position));//刪除頁卡}@SuppressWarnings("deprecation")@OverridepublicObjectinstantiateItem(ViewGroupcontainer,intposition){//這個方法用來實例化頁卡Viewview=mListViews.get(position);container.addView(view,0);//添加頁卡switch(position){caseVIEW_NO_1:mOnePointer=(ImageView)view.findViewById(R.id.one_pointer);view.setBackgroundDrawable(ImageCompress.getInstance().getCompressFromId(context,R.drawable.guide_one_bg,screenW,screenH));break;caseVIEW_NO_2:mTwoCar=(ImageView)view.findViewById(R.id.two_car);mTwoCar.setBackgroundResource(R.anim.guide_two_car_frame_anim);view.setBackgroundDrawable(ImageCompress.getInstance().getCompressFromId(context,R.drawable.guide_two_bg,screenW,screenH));break;caseVIEW_NO_3:mThreeCar=(ImageView)view.findViewById(R.id.three_car);mThreeCarShadow=(ImageView)view.findViewById(R.id.three_car_shadow);mThreeCloudFast=(ImageView)view.findViewById(R.id.three_cloud_fast);mThreeCloudSlow=(ImageView)view.findViewById(R.id.three_cloud_slow);view.setBackgroundDrawable(ImageCompress.getInstance().getCompressFromId(context,R.drawable.guide_three_bg,screenW,screenH));break;caseVIEW_NO_4:mFourCoinPack=(ImageView)view.findViewById(R.id.four_pack);mFourCoin=(ImageView)view.findViewById(R.id.four_coin);mFourCoin.setBackgroundResource(R.anim.guide_four_coin_frame_anim);mFourPig=(ImageView)view.findViewById(R.id.four_pig);mFourPigShadow=(ImageView)view.findViewById(R.id.four_pig_shadow);view.setBackgroundDrawable(ImageCompress.getInstance().getCompressFromId(context,R.drawable.guide_four_bg,screenW,screenH));break;caseVIEW_NO_5:mFiveCar=(ImageView)view.findViewById(R.id.five_car);mFiveCarShadow=(ImageView)view.findViewById(R.id.five_car_shadow);mFiveCloudFast=(ImageView)view.findViewById(R.id.five_cloud_fast);mFiveCloudSlow=(ImageView)view.findViewById(R.id.five_cloud_slow);view.setOnTouchListener(mOnTouchListener);view.setBackgroundDrawable(ImageCompress.getInstance().getCompressFromId(context,R.drawable.guide_five_bg,screenW,screenH));break;default:break;}returnmListViews.get(position);}@OverridepublicintgetCount(){returnmListViews.size();//返回頁卡的數量}@OverridepublicbooleanisViewFromObject(Viewarg0,Objectarg1){returnarg0==arg1;//官方提示這樣寫}}@OverridepublicvoidonPageScrolled(intposition,floatpositionOffset,intpositionOffsetPixels){}@OverridepublicvoidonPageSelected(intposition){animal(position);}@OverridepublicvoidonPageScrollStateChanged(intstate){}privatevoidanimal(intposition){try{switch(position){caseVIEW_NO_1:AccelerateDecelerateInterpolatorinterpolator=newAccelerateDecelerateInterpolator();Animationanimation1_1=AnimationUtils.loadAnimation(this,R.anim.guide_one_pointer_ratate);animation1_1.setFillAfter(true);animation1_1.setInterpolator(interpolator);mOnePointer.clearAnimation();mOnePointer.startAnimation(animation1_1);break;caseVIEW_NO_2:AnimationDrawableanimation2_1=(AnimationDrawable)mTwoCar.getBackground();//animation2_1.unscheduleSelf(null);//重新將Frame動畫設置到第-1幀,也就是重新開始animation2_1.setVisible(false,true);animation2_1.start();break;caseVIEW_NO_3:LinearInterpolatorlinearInterpolator=newLinearInterpolator();Animationanimation3_1=newTranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_PARENT,1.0f,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0);animation3_1.setDuration(25000);animation3_1.setInterpolator(linearInterpolator);mThreeCloudFast.clearAnimation();mThreeCloudFast.startAnimation(animation3_1);Animationanimation3_2=newTranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_PARENT,1.0f,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0);animation3_2.setDuration(35000);animation3_2.setInterpolator(linearInterpolator);mThreeCloudSlow.clearAnimation();mThreeCloudSlow.startAnimation(animation3_2);Animationanimation3_3=newScaleAnimation(1.0f,1.0f,1.0f,1.05f,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,1.0f);animation3_3.setRepeatCount(-1);animation3_3.setRepeatMode(Animation.REVERSE);animation3_3.setDuration(500);animation3_3.setInterpolator(linearInterpolator);mThreeCar.clearAnimation();mThreeCar.startAnimation(animation3_3);Animationanimation3_4=newScaleAnimation(1.0f,1.05f,1.0f,1.05f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);animation3_4.setRepeatCount(-1);animation3_4.setDuration(500);animation3_4.setRepeatMode(Animation.REVERSE);animation3_4.setInterpolator(linearInterpolator);mThreeCarShadow.clearAnimation();mThreeCarShadow.startAnimation(animation3_4);break;caseVIEW_NO_4://錢桶的動畫Animationanimation4_1=newRotateAnimation(0,5,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_PARENT,0.5f);animation4_1.setRepeatCount(-1);animation4_1.setDuration(300);mFourCoinPack.clearAnimation();mFourCoinPack.startAnimation(animation4_1);//硬幣掉落的動畫AnimationDrawableanimation4_2=(AnimationDrawable)mFourCoin.getBackground();animation4_2.start();//小豬的動畫Animationanimation4_3=newScaleAnimation(1.0f,1.0f,1.0f,1.05f,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,1.0f);animation4_3.setRepeatCount(-1);animation4_3.setDuration(500);mFourPig.clearAnimation();mFourPig.startAnimation(animation4_3);//小豬影子的動畫Animationanimation4_4=newScaleAnimation(1.0f,1.05f,1.0f,1.05f,Animation.RELATIVE_TO_SELF,0.75f,Animation.RELATIVE_TO_SELF,0.95f);animation4_4.setRepeatCount(-1);animation4_4.setDuration(500);mFourPigShadow.clearAnimation();mFourPigShadow.startAnimation(animation4_4);break;caseVIEW_NO_5:LinearInterpolatorlinearInterpolator2=newLinearInterpolator();Animationanimation5_1=newTranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_PARENT,1.0f,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0);animation5_1.setDuration(25000);animation5_1.setInterpolator(linearInterpolator2);mFiveCloudFast.clearAnimation();mFiveCloudFast.startAnimation(animation5_1);Animationanimation5_2=newTranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_PARENT,1.0f,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0);animation5_2.setDuration(35000);animation5_2.setInterpolator(linearInterpolator2);mFiveCloudSlow.clearAnimation();mFiveCloudSlow.startAnimation(animation5_2);Animationanimation5_3=newScaleAnimation(1.0f,1.0f,1.0f,1.1f,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,1.0f);animation5_3.setRepeatCount(-1);animation5_3.setDuration(500);animation5_3.setRepeatMode(Animation.REVERSE);mFiveCar.clearAnimation();mFiveCar.startAnimation(animation5_3);Animationanimation5_4=newScaleAnimation(1.0f,1.1f,1.0f,1.1f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);animation5_4.setRepeatCount(-1);animation5_4.setDuration(500);animation5_4.setRepeatMode(Animation.REVERSE);mFiveCarShadow.clearAnimation();mFiveCarShadow.startAnimation(animation5_4);break;}preIndex=position;}catch(Exceptione){finish();}}View.OnTouchListenermOnTouchListener=newView.OnTouchListener(){@OverridepublicbooleanonTouch(Viewv,MotionEventevent){if(preIndex==4){switch(event.getAction()){caseMotionEvent.ACTION_DOWN:x1=(int)event.getX();Toast.makeText(MainActivity.this,"X1--->"+x1,Toast.LENGTH_SHORT).show();break;caseMotionEvent.ACTION_MOVE:x2=(int)event.getX();Toast.makeText(MainActivity.this,"X2--->"+x2,Toast.LENGTH_SHORT).show();if((x2-x1)<0){finish();}//Toast.makeText(MainActivity.this,"<--->"+(int)//event.getX(),Toast.LENGTH_SHORT).show();break;caseMotionEvent.ACTION_UP:x2=(int)event.getX();Toast.makeText(MainActivity.this,"X2--->"+x2,Toast.LENGTH_SHORT).show();if((x2-x1)<0){finish();}break;default:break;}}returntrue;}};intx1=0,x2=0;}第一步:在onCreate函數中初始化每一個子view,然後添加翻頁的監聽和翻頁的動畫效果<注意:這是翻頁效果,不是子view中的對象的動畫效果>[java]view plaincopy

publicContextcontext;publicstaticintscreenW,screenH;privatestaticfinalintVIEW_NO_1=0;privatestaticfinalintVIEW_NO_2=1;privatestaticfinalintVIEW_NO_3=2;privatestaticfinalintVIEW_NO_4=3;privatestaticfinalintVIEW_NO_5=4;//第1頁的資源,坐標staticImageViewmOnePointer;//第2頁的資源,坐標staticImageViewmTwoCar;//第3頁的資源,坐標staticImageViewmThreeCloudFast;staticImageViewmThreeCloudSlow;staticImageViewmThreeCarShadow;staticImageViewmThreeCar;//第4頁的資源,坐標staticImageViewmFourPig;staticImageViewmFourPigShadow;staticImageViewmFourCoin;staticImageViewmFourCoinPack;//第5頁的資源,坐標staticImageViewmFiveCar;staticImageViewmFiveCarShadow;staticImageViewmFiveCloudFast;staticImageViewmFiveCloudSlow;privateintpreIndex=0;privateViewPagermPager;privateMyViewPagerAdaptermPagerAdapter;List<View>list=newArrayList<View>();@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);context=this;DisplayMetricsmetric=newDisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(metric);screenW=metric.widthPixels;//屏幕寬度(像素)screenH=metric.heightPixels;//屏幕高度(像素)LayoutInflaterinflater=LayoutInflater.from(this);Viewview0=inflater.inflate(R.layout.guide_fragment_main_1,null,false);mOnePointer=(ImageView)view0.findViewById(R.id.one_pointer);Viewview1=inflater.inflate(R.layout.guide_fragment_main_2,null,false);Viewview2=inflater.inflate(R.layout.guide_fragment_main_3,null,false);Viewview3=inflater.inflate(R.layout.guide_fragment_main_4,null,false);Viewview4=inflater.inflate(R.layout.guide_fragment_main_5,null,false);list.add(view0);list.add(view1);list.add(view2);list.add(view3);list.add(view4);mPager=(ViewPager)findViewById(R.id.container);mPagerAdapter=newMyViewPagerAdapter(list);mPager.setAdapter(mPagerAdapter);mPager.setOnPageChangeListener(this);//設置翻頁的監聽mPager.setPageTransformer(true,newtransforms.StackTransformer());//這裡設置為堆棧式的翻頁效果animal(VIEW_NO_1);//這裡是為了第一次進入應用時,作出第一頁的動畫}第二步:添加翻頁監聽後,處理翻頁的回調函數[java]view plaincopy

@OverridepublicvoidonPageScrolled(intposition,floatpositionOffset,intpositionOffsetPixels){}@OverridepublicvoidonPageSelected(intposition){animal(position);//播放第position頁的動畫}第三步:實現animal函數[java]view plaincopy

privatevoidanimal(intposition){try{switch(position){caseVIEW_NO_1:AccelerateDecelerateInterpolatorinterpolator=newAccelerateDecelerateInterpolator();Animationanimation1_1=AnimationUtils.loadAnimation(this,R.anim.guide_one_pointer_ratate);animation1_1.setFillAfter(true);animation1_1.setInterpolator(interpolator);mOnePointer.clearAnimation();mOnePointer.startAnimation(animation1_1);break;caseVIEW_NO_2:AnimationDrawableanimation2_1=(AnimationDrawable)mTwoCar.getBackground();/animation2_1.unscheduleSelf(null);//重新將Frame動畫設置到第-1幀,也就是重新開始animation2_1.setVisible(false,true);animation2_1.start();break;caseVIEW_NO_3:LinearInterpolatorlinearInterpolator=newLinearInterpolator();Animationanimation3_1=newTranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_PARENT,1.0f,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0);animation3_1.setDuration(25000);animation3_1.setInterpolator(linearInterpolator);mThreeCloudFast.clearAnimation();mThreeCloudFast.startAnimation(animation3_1);Animationanimation3_2=newTranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_PARENT,1.0f,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0);animation3_2.setDuration(35000);animation3_2.setInterpolator(linearInterpolator);mThreeCloudSlow.clearAnimation();mThreeCloudSlow.startAnimation(animation3_2);Animationanimation3_3=newScaleAnimation(1.0f,1.0f,1.0f,1.05f,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,1.0f);animation3_3.setRepeatCount(-1);animation3_3.setRepeatMode(Animation.REVERSE);animation3_3.setDuration(500);animation3_3.setInterpolator(linearInterpolator);mThreeCar.clearAnimation();mThreeCar.startAnimation(animation3_3);Animationanimation3_4=newScaleAnimation(1.0f,1.05f,1.0f,1.05f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);animation3_4.setRepeatCount(-1);animation3_4.setDuration(500);animation3_4.setRepeatMode(Animation.REVERSE);animation3_4.setInterpolator(linearInterpolator);mThreeCarShadow.clearAnimation();mThreeCarShadow.startAnimation(animation3_4);break;caseVIEW_NO_4://錢桶的動畫Animationanimation4_1=newRotateAnimation(0,5,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_PARENT,0.5f);animation4_1.setRepeatCount(-1);animation4_1.setDuration(300);mFourCoinPack.clearAnimation();mFourCoinPack.startAnimation(animation4_1);//硬幣掉落的動畫AnimationDrawableanimation4_2=(AnimationDrawable)mFourCoin.getBackground();animation4_2.start();//小豬的動畫Animationanimation4_3=newScaleAnimation(1.0f,1.0f,1.0f,1.05f,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,1.0f);animation4_3.setRepeatCount(-1);animation4_3.setDuration(500);mFourPig.clearAnimation();mFourPig.startAnimation(animation4_3);//小豬影子的動畫Animationanimation4_4=newScaleAnimation(1.0f,1.05f,1.0f,1.05f,Animation.RELATIVE_TO_SELF,0.75f,Animation.RELATIVE_TO_SELF,0.95f);animation4_4.setRepeatCount(-1);animation4_4.setDuration(500);mFourPigShadow.clearAnimation();mFourPigShadow.startAnimation(animation4_4);break;caseVIEW_NO_5:LinearInterpolatorlinearInterpolator2=newLinearInterpolator();Animationanimation5_1=newTranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_PARENT,1.0f,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0);animation5_1.setDuration(25000);animation5_1.setInterpolator(linearInterpolator2);mFiveCloudFast.clearAnimation();mFiveCloudFast.startAnimation(animation5_1);Animationanimation5_2=newTranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_PARENT,1.0f,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0);animation5_2.setDuration(35000);animation5_2.setInterpolator(linearInterpolator2);mFiveCloudSlow.clearAnimation();mFiveCloudSlow.startAnimation(animation5_2);Animationanimation5_3=newScaleAnimation(1.0f,1.0f,1.0f,1.1f,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,1.0f);animation5_3.setRepeatCount(-1);animation5_3.setDuration(500);animation5_3.setRepeatMode(Animation.REVERSE);mFiveCar.clearAnimation();mFiveCar.startAnimation(animation5_3);Animationanimation5_4=newScaleAnimation(1.0f,1.1f,1.0f,1.1f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);animation5_4.setRepeatCount(-1);animation5_4.setDuration(500);animation5_4.setRepeatMode(Animation.REVERSE);mFiveCarShadow.clearAnimation();mFiveCarShadow.startAnimation(animation5_4);break;}preIndex=position;}catch(Exceptione){finish();}}裡面都是對Animation的疊加使用,只要理解了Android的Animation這段代碼應該都可以看得懂,

,實在是看不懂可以參與討論哦最後一步:實現PagerAdapter,因為翻頁動畫加入項目中需要很高的效率性,如果效率不高,很容易導致資源佔用過多,所以這裡分別對destroyItem和instantiateItem函數進行了高效率的重寫。ViewPager的機制是只載入<假如當前頁是B>A、B和C頁,所以對不用的頁面會調用destroyItem函數,所以都在這裡做回收操作,因為有頁面會被回收,所以如果在對象棧中找不到想去的下一頁資源ViewPager的PagerAdapter就會調用instantiateItem函數來創建view,所以需要在這裡分別創建子view。說好的重視優化呢?

,如果你們以為上面這些就是優化,說明你對動畫的使用還不是很了解,也對圖片的使用規則不是很清楚但是接下來,我會淺談一下這些問題滴

,又要被鄙視了

還是OOM的問題,Android OS會對每一個APP分配一個固定大小的內存僅供使用,所以當你的引導頁中使用了許多大背景,比如像幀動畫一下就使用N張大圖,所以一下就OOM了,雖然PNG格式會有很多透明的地方,就像這裡的一個小車,但是UI切的圖片就和背景一樣大,那是因為要對應小車的位置。在Android中Bitmap存儲的流可不管你是什麼格式的圖片,佔用空間有多大,一切都要按照Android的計算方式,那就是 height*width*size,所以過多的使用大圖片就很占內存。當自己使用的圖片太大,那就需要壓縮,這裡我不講圖片壓縮,壓縮圖片以後專門講源碼中附帶了兩個util類,就是不同方法壓縮圖片

,BB完了,感覺也沒有多少東西源碼下載如果有什麼疑問,多多留言哦,小夥伴們!接下來,會出一篇相對高級一點的動畫引導頁教程,使用SurfaceView實現動畫。在SurfaceView的世界裡,只有你想不到的,沒有我實現不了的。

,又開始裝B了。。。
推薦閱讀:

查看原文 >>
相关文章