• ViewPager引导页效果实例源码


           首先大家先找到本地的sdk,然后找到Google提供的API,具体查找方法如下:sdk——>docs——>index.html——>develop——>training——>左边的目录下找到Build Apps with Graphics & Animation——>Adding Animation——>Using ViewPager for Screen Slides,在这里我们要用到两个接口DepthPageTransformer和ZoomOutPageTransformer,这是两个动画效果,大家可以根据需求选择相应的接口。为什么我没有直接把自己实现的代码粘贴出来,因为我建议大家没事的时候就在学一学,看一看Google提供的API。我也是最近才开始发现学会了对自己平时的开发有很大的帮助,下面我就把我的源码给大家分享一下把,布局我就不说了,只有一个ViewPager。

        1、自定义接口DepthPageTransformer实现ViewPager.PageTransformer,也可以把API的全部复制粘贴。

    package com.app.myviewpagercartoon;
    
    import android.view.View;
    import android.annotation.SuppressLint;
    import android.annotation.TargetApi;
    import android.os.Build;
    import android.support.v4.view.ViewPager;
    
    @TargetApi(Build.VERSION_CODES.HONEYCOMB) @SuppressLint("NewApi") public class DepthPageTransformer implements ViewPager.PageTransformer {
        private static final float MIN_SCALE = 0.75f;
    
        @TargetApi(Build.VERSION_CODES.HONEYCOMB) @SuppressLint("NewApi") public void transformPage(View view, float position) {
            int pageWidth = view.getWidth();
    
            if (position < -1) { // [-Infinity,-1)
                // This page is way off-screen to the left.
                view.setAlpha(0);
    
            } else if (position <= 0) { // [-1,0]
                // Use the default slide transition when moving to the left page
                view.setAlpha(1);
                view.setTranslationX(0);
                view.setScaleX(1);
                view.setScaleY(1);
    
            } else if (position <= 1) { // (0,1]
                // Fade the page out.
                view.setAlpha(1 - position);
    
                // Counteract the default slide transition
                view.setTranslationX(pageWidth * -position);
    
                // Scale the page down (between MIN_SCALE and 1)
                float scaleFactor = MIN_SCALE
                        + (1 - MIN_SCALE) * (1 - Math.abs(position));
                view.setScaleX(scaleFactor);
                view.setScaleY(scaleFactor);
    
            } else { // (1,+Infinity]
                // This page is way off-screen to the right.
                view.setAlpha(0);
            }
        }
    }

    2、自定义ZoomOutPageTransformer 实现ViewPager.PageTransformer,也可以把API的全部复制粘贴。

    package com.app.myviewpagercartoon;
    
    import android.annotation.SuppressLint;
    import android.annotation.TargetApi;
    import android.os.Build;
    import android.support.v4.view.ViewPager;
    import android.view.View;
    
    @TargetApi(Build.VERSION_CODES.HONEYCOMB) @SuppressLint("NewApi") public class ZoomOutPageTransformer implements ViewPager.PageTransformer {
        private static final float MIN_SCALE = 0.85f;
        private static final float MIN_ALPHA = 0.5f;
       
        @TargetApi(Build.VERSION_CODES.HONEYCOMB) @SuppressLint("NewApi") public void transformPage(View view, float position) {
            int pageWidth = view.getWidth();
            int pageHeight = view.getHeight();
    
            if (position < -1) { // [-Infinity,-1)
                // This page is way off-screen to the left.
                view.setAlpha(0);
    
            } else if (position <= 1) { // [-1,1]
                // Modify the default slide transition to shrink the page as well
                float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
                float vertMargin = pageHeight * (1 - scaleFactor) / 2;
                float horzMargin = pageWidth * (1 - scaleFactor) / 2;
                if (position < 0) {
                    view.setTranslationX(horzMargin - vertMargin / 2);
                } else {
                    view.setTranslationX(-horzMargin + vertMargin / 2);
                }
    
                // Scale the page down (between MIN_SCALE and 1)
                view.setScaleX(scaleFactor);
                view.setScaleY(scaleFactor);
    
                // Fade the page relative to its size.
                view.setAlpha(MIN_ALPHA +
                        (scaleFactor - MIN_SCALE) /
                        (1 - MIN_SCALE) * (1 - MIN_ALPHA));
    
            } else { // (1,+Infinity]
                // This page is way off-screen to the right.
                view.setAlpha(0);
            }
        }
    }

        3、实现MainActivity类的代码

    package com.app.myviewpagercartoon;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.support.v4.view.PagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.Window;
    import android.widget.ImageView;
    import android.widget.ImageView.ScaleType;
    
    public class MainActivity extends Activity {
    
        private ViewPager viewPager;
        //图片太大卡,我没有对图片进行处理,大家可以处理一下图片
        private int[] imags = new int[] { R.drawable.guide_image1,
                R.drawable.guide_image2, R.drawable.guide_image3 };
        private List<ImageView> list = new ArrayList<ImageView>();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            viewPager = (ViewPager) findViewById(R.id.viewPager);
            //添加两种动画效果,3.0以上有效
            //viewPager.setPageTransformer(true, new ZoomOutPageTransformer());
            viewPager.setPageTransformer(true, new DepthPageTransformer());
            viewPager.setAdapter(new PagerAdapter() {
    
                @Override
                public Object instantiateItem(ViewGroup container, int position) {
                    ImageView imageView = new ImageView(MainActivity.this);
                    imageView.setImageResource(imags[position]);
                    imageView.setScaleType(ScaleType.CENTER_CROP);
                    container.addView(imageView);
                    list.add(imageView);
                    return imageView;
                }
    
                @Override
                public void destroyItem(ViewGroup container, int position,
                        Object object) {
                
                    container.removeView(list.get(position));
                }
    
                @Override
                public boolean isViewFromObject(View arg0, Object arg1) {
    
                    return arg0 == arg1;
                }
    
                @Override
                public int getCount() {
    
                    return imags.length;
                }
            });
        }
    
    }

          附上效果图:

                                                         

      

          

         

  • 相关阅读:
    jmeter—— vars 和 props 用法
    java—把字符串转成list类型,并遍历列表
    fillder—篡改请求参数重新请求
    Jmeter—变量嵌套变量方法
    python——logging日志模块
    python学习——使用excel读写测试数据
    python学习——单元测试unittest
    python学习——类
    python学习——异常
    资源信息汇总
  • 原文地址:https://www.cnblogs.com/chentong/p/5416611.html
Copyright © 2020-2023  润新知