转载自: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翻页框架,分别对每一页添加动画,源代码会在下面给出链接。
- import java.util.ArrayList;
- import java.util.List;
- import android.app.Activity;
- import android.content.Context;
- import android.graphics.drawable.AnimationDrawable;
- import android.graphics.drawable.BitmapDrawable;
- import android.os.Bundle;
- import android.support.v4.view.PagerAdapter;
- import android.support.v4.view.ViewPager;
- import android.util.DisplayMetrics;
- import android.view.LayoutInflater;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.animation.AccelerateDecelerateInterpolator;
- import android.view.animation.Animation;
- import android.view.animation.AnimationUtils;
- import android.view.animation.LinearInterpolator;
- import android.view.animation.RotateAnimation;
- import android.view.animation.ScaleAnimation;
- import android.view.animation.TranslateAnimation;
- import android.widget.ImageView;
- import android.widget.Toast;
- public class MainActivity extends Activity implements
- ViewPager.OnPageChangeListener {
- public Context context ;
- public static int screenW, screenH;
- private static final int VIEW_NO_1 = 0;
- private static final int VIEW_NO_2 = 1;
- private static final int VIEW_NO_3 = 2;
- private static final int VIEW_NO_4 = 3;
- private static final int VIEW_NO_5 = 4;
- // 第1页的资源,坐标
- static ImageView mOnePointer;
- // 第2页的资源,坐标
- static ImageView mTwoCar;
- // 第3页的资源,坐标
- static ImageView mThreeCloudFast;
- static ImageView mThreeCloudSlow;
- static ImageView mThreeCarShadow;
- static ImageView mThreeCar;
- // 第4页的资源,坐标
- static ImageView mFourPig;
- static ImageView mFourPigShadow;
- static ImageView mFourCoin;
- static ImageView mFourCoinPack;
- // 第5页的资源,坐标
- static ImageView mFiveCar;
- static ImageView mFiveCarShadow;
- static ImageView mFiveCloudFast;
- static ImageView mFiveCloudSlow;
- private int preIndex = 0;
- private ViewPager mPager;
- private MyViewPagerAdapter mPagerAdapter;
- List<View> list = new ArrayList<View>();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- context = this ;
- DisplayMetrics metric = new DisplayMetrics();
- getWindowManager().getDefaultDisplay().getMetrics(metric);
- screenW = metric.widthPixels; // 屏幕宽度(像素)
- screenH = metric.heightPixels; // 屏幕高度(像素)
- LayoutInflater inflater = LayoutInflater.from(this);
- View view0 = inflater.inflate(R.layout.guide_fragment_main_1, null,
- false);
- mOnePointer = (ImageView) view0.findViewById(R.id.one_pointer);
- View view1 = inflater.inflate(R.layout.guide_fragment_main_2, null,
- false);
- View view2 = inflater.inflate(R.layout.guide_fragment_main_3, null,
- false);
- View view3 = inflater.inflate(R.layout.guide_fragment_main_4, null,
- false);
- View view4 = 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 = new MyViewPagerAdapter(list);
- mPager.setAdapter(mPagerAdapter);
- mPager.setOnPageChangeListener(this);
- mPager.setPageTransformer(true, new transforms.StackTransformer());
- animal(VIEW_NO_1);
- }
- public class MyViewPagerAdapter extends PagerAdapter {
- private List<View> mListViews;
- public MyViewPagerAdapter(List<View> mListViews) {
- this.mListViews = mListViews;// 构造方法,参数是我们的页卡,这样比较方便。
- }
- @Override
- public void destroyItem(ViewGroup container, int position, Object object) {
- View view = mListViews.get(position) ;
- BitmapDrawable drawable = (BitmapDrawable)view.getBackground() ;
- if (drawable != null) {
- drawable.getBitmap().recycle() ;
- }
- switch (position) {
- case VIEW_NO_1:
- break;
- case VIEW_NO_2:
- // mTwoCar.getBackground().setCallback(null) ;
- break;
- case VIEW_NO_3:
- BitmapDrawable d3_1 = (BitmapDrawable)mThreeCar.getBackground() ;
- if (d3_1 != null) {
- d3_1.getBitmap().recycle() ;
- }
- BitmapDrawable d3_2 = (BitmapDrawable)mThreeCarShadow.getBackground() ;
- if (d3_2 != null) {
- d3_2.getBitmap().recycle() ;
- }
- break;
- case VIEW_NO_4:
- // mFourCoin.getBackground().setCallback(null) ;
- BitmapDrawable d4_1 = (BitmapDrawable)mFourCoinPack.getBackground() ;
- if (d4_1 != null) {
- d4_1.getBitmap().recycle() ;
- }
- BitmapDrawable d4_2 = (BitmapDrawable)mFourPig.getBackground() ;
- if (d4_2 != null) {
- d4_2.getBitmap().recycle() ;
- }
- BitmapDrawable d4_3 = (BitmapDrawable)mFourPigShadow.getBackground() ;
- if (d4_3 != null) {
- d4_3.getBitmap().recycle() ;
- }
- break;
- case VIEW_NO_5:
- BitmapDrawable d5_1 = (BitmapDrawable)mFiveCar.getBackground() ;
- if (d5_1 != null) {
- d5_1.getBitmap().recycle() ;
- }
- BitmapDrawable d5_2 = (BitmapDrawable)mFiveCarShadow.getBackground() ;
- if (d5_2 != null) {
- d5_2.getBitmap().recycle() ;
- }
- break;
- default:
- break;
- }
- container.removeView(mListViews.get(position));// 删除页卡
- }
- @SuppressWarnings("deprecation")
- @Override
- public Object instantiateItem(ViewGroup container, int position) { // 这个方法用来实例化页卡
- View view = mListViews.get(position) ;
- container.addView(view, 0);// 添加页卡
- switch (position) {
- case VIEW_NO_1:
- mOnePointer = (ImageView) view.findViewById(R.id.one_pointer);
- view.setBackgroundDrawable(
- ImageCompress
- .getInstance()
- .getCompressFromId(context, R.drawable.guide_one_bg, screenW, screenH)) ;
- break;
- case VIEW_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;
- case VIEW_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;
- case VIEW_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;
- case VIEW_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;
- }
- return mListViews.get(position);
- }
- @Override
- public int getCount() {
- return mListViews.size();// 返回页卡的数量
- }
- @Override
- public boolean isViewFromObject(View arg0, Object arg1) {
- return arg0 == arg1;// 官方提示这样写
- }
- }
- @Override
- public void onPageScrolled(int position, float positionOffset,
- int positionOffsetPixels) {
- }
- @Override
- public void onPageSelected(int position) {
- animal(position);
- }
- @Override
- public void onPageScrollStateChanged(int state) {
- }
- private void animal(int position) {
- try {
- switch (position) {
- case VIEW_NO_1:
- AccelerateDecelerateInterpolator interpolator = new AccelerateDecelerateInterpolator();
- Animation animation1_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;
- case VIEW_NO_2:
- AnimationDrawable animation2_1 = (AnimationDrawable) mTwoCar
- .getBackground();
- // animation2_1.unscheduleSelf(null); // 重新将Frame动画设置到第-1帧,也就是重新开始
- animation2_1.setVisible(false, true) ;
- animation2_1.start();
- break;
- case VIEW_NO_3:
- LinearInterpolator linearInterpolator = new LinearInterpolator();
- Animation animation3_1 = new TranslateAnimation(
- 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);
- Animation animation3_2 = new TranslateAnimation(
- 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);
- Animation animation3_3 = new ScaleAnimation(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);
- Animation animation3_4 = new ScaleAnimation(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;
- case VIEW_NO_4:
- // 钱桶的动画
- Animation animation4_1 = new RotateAnimation(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);
- // 硬币掉落的动画
- AnimationDrawable animation4_2 = (AnimationDrawable) mFourCoin
- .getBackground();
- animation4_2.start();
- // 小猪的动画
- Animation animation4_3 = new ScaleAnimation(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);
- // 小猪影子的动画
- Animation animation4_4 = new ScaleAnimation(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;
- case VIEW_NO_5:
- LinearInterpolator linearInterpolator2 = new LinearInterpolator();
- Animation animation5_1 = new TranslateAnimation(
- 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);
- Animation animation5_2 = new TranslateAnimation(
- 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);
- Animation animation5_3 = new ScaleAnimation(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);
- Animation animation5_4 = new ScaleAnimation(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 (Exception e) {
- finish() ;
- }
- }
- View.OnTouchListener mOnTouchListener = new View.OnTouchListener() {
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- if (preIndex == 4) {
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN:
- x1 = (int) event.getX();
- Toast.makeText(MainActivity.this, "X1--->" + x1,
- Toast.LENGTH_SHORT).show();
- break;
- case MotionEvent.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;
- case MotionEvent.ACTION_UP:
- x2 = (int) event.getX();
- Toast.makeText(MainActivity.this, "X2--->" + x2,
- Toast.LENGTH_SHORT).show();
- if ((x2 - x1) < 0) {
- finish();
- }
- break;
- default:
- break;
- }
- }
- return true;
- }
- };
- int x1 = 0, x2 = 0;
- }
第一步:在onCreate函数中初始化每一个子view,然后添加翻页的监听和翻页的动画效果<注意:这是翻页效果,不是子view中的对象的动画效果>
- public Context context ;
- public static int screenW, screenH;
- private static final int VIEW_NO_1 = 0;
- private static final int VIEW_NO_2 = 1;
- private static final int VIEW_NO_3 = 2;
- private static final int VIEW_NO_4 = 3;
- private static final int VIEW_NO_5 = 4;
- // 第1页的资源,坐标
- static ImageView mOnePointer;
- // 第2页的资源,坐标
- static ImageView mTwoCar;
- // 第3页的资源,坐标
- static ImageView mThreeCloudFast;
- static ImageView mThreeCloudSlow;
- static ImageView mThreeCarShadow;
- static ImageView mThreeCar;
- // 第4页的资源,坐标
- static ImageView mFourPig;
- static ImageView mFourPigShadow;
- static ImageView mFourCoin;
- static ImageView mFourCoinPack;
- // 第5页的资源,坐标
- static ImageView mFiveCar;
- static ImageView mFiveCarShadow;
- static ImageView mFiveCloudFast;
- static ImageView mFiveCloudSlow;
- private int preIndex = 0;
- private ViewPager mPager;
- private MyViewPagerAdapter mPagerAdapter;
- List<View> list = new ArrayList<View>();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- context = this ;
- DisplayMetrics metric = new DisplayMetrics();
- getWindowManager().getDefaultDisplay().getMetrics(metric);
- screenW = metric.widthPixels; // 屏幕宽度(像素)
- screenH = metric.heightPixels; // 屏幕高度(像素)
- LayoutInflater inflater = LayoutInflater.from(this);
- View view0 = inflater.inflate(R.layout.guide_fragment_main_1, null,
- false);
- mOnePointer = (ImageView) view0.findViewById(R.id.one_pointer);
- View view1 = inflater.inflate(R.layout.guide_fragment_main_2, null,
- false);
- View view2 = inflater.inflate(R.layout.guide_fragment_main_3, null,
- false);
- View view3 = inflater.inflate(R.layout.guide_fragment_main_4, null,
- false);
- View view4 = 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 = new MyViewPagerAdapter(list);
- mPager.setAdapter(mPagerAdapter);
- mPager.setOnPageChangeListener(this);//设置翻页的监听
- mPager.setPageTransformer(true, new transforms.StackTransformer());//这里设置为堆栈式的翻页效果
- animal(VIEW_NO_1);//这里是为了第一次进入应用时,作出第一页的动画
- }
- @Override
- public void onPageScrolled(int position, float positionOffset,
- int positionOffsetPixels) {
- }
- @Override
- public void onPageSelected(int position) {
- animal(position);//播放第position页的动画
- }
- private void animal(int position) {
- try {
- switch (position) {
- case VIEW_NO_1:
- AccelerateDecelerateInterpolator interpolator = new AccelerateDecelerateInterpolator();
- Animation animation1_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;
- case VIEW_NO_2:
- AnimationDrawable animation2_1 = (AnimationDrawable) mTwoCar
- .getBackground();
- / animation2_1.unscheduleSelf(null); // 重新将Frame动画设置到第-1帧,也就是重新开始
- animation2_1.setVisible(false, true) ;
- animation2_1.start();
- break;
- case VIEW_NO_3:
- LinearInterpolator linearInterpolator = new LinearInterpolator();
- Animation animation3_1 = new TranslateAnimation(
- 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);
- Animation animation3_2 = new TranslateAnimation(
- 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);
- Animation animation3_3 = new ScaleAnimation(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);
- Animation animation3_4 = new ScaleAnimation(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;
- case VIEW_NO_4:
- // 钱桶的动画
- Animation animation4_1 = new RotateAnimation(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);
- // 硬币掉落的动画
- AnimationDrawable animation4_2 = (AnimationDrawable) mFourCoin
- .getBackground();
- animation4_2.start();
- // 小猪的动画
- Animation animation4_3 = new ScaleAnimation(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);
- // 小猪影子的动画
- Animation animation4_4 = new ScaleAnimation(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;
- case VIEW_NO_5:
- LinearInterpolator linearInterpolator2 = new LinearInterpolator();
- Animation animation5_1 = new TranslateAnimation(
- 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);
- Animation animation5_2 = new TranslateAnimation(
- 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);
- Animation animation5_3 = new ScaleAnimation(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);
- Animation animation5_4 = new ScaleAnimation(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 (Exception e) {
- 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了。。。