视差动画 - 酷狗音乐引导页

1. 说明


我们这节课就来看下视差动画,其实就是根据当前滚动的位置去设置位移,视差动画一般用作引导页,比如知乎、酷狗音乐等等,那么我们这节课要做的效果就是实现——酷狗音乐引导页。效果如下图所示:

图片.png
图片.png

效果图就上这两张,可以看到:
1.1 滑动第一张到屏幕最左边,然后第二张就会滑动到当前位置,并且第二张图片上边的图标和文字都会移动;
1.2 当第二张图片滑动到屏幕最左边之后,然后第三张图片,也会滑动到当前位置,并且随着滑动第三张图片中的文字及图标都会移动;
这个效果就是视差动画。

2. 思路分析


最外层是ViewPager + 视差动画界面是Fragment

2.1 先把布局和Fragment创建好;

在activity_main.xml布局文件中直接引用 ParallaxViewPager对应的布局文件,里边就是一个自定义的 ParallaxViewPager,如果有需要在最后一个页面添加 "立即体验"的按钮点击跳转主页面的话,可以在该Parallax布局文件下边添加一个 "立即体验"的按钮,监听 ParallaxViewPager的滑动事件,当滑动到最后一页时直接让 "立即体验" 的button按钮显示出来,其余页面让隐藏,然后设置点击事件即可;

2.2 把所有需要移动的属性解析出来

// 存放所有的需要位移的View
    private List<View> mParallaxViews = new ArrayList<>() ;
    // 存放视差动画的属性
    private int[] mParallaxAttrs = new int[]{R.attr.translationXIn,R.attr.translationXOut ,
                            R.attr.translationYIn,R.attr.translationYOut};


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // 获取布局的id
        int layoutId = getArguments().getInt(LAYOUT_ID_KEY);
        // 2.2.2  把所有需要移动的属性解析出来 ,内涵段子插件式换肤有
        // View创建的时候我们去解析,这里传inflater是有问题的,  单例设计模式代表所有View的创建都是该 Fragment去创建的

        // 克隆一个inflater出来
        inflater = inflater.cloneInContext(getActivity()) ;
        LayoutInflaterCompat.setFactory(inflater , this);

        return inflater.inflate(layoutId , container , false);
    }


    /**
     * 这个方法是实现LayoutInflaterFactory接口后重写的该方法
     * @param parent
     * @param name
     * @param context
     * @param attrs
     * @return
     */
    @Override
    public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
        // 所有的View都会在这里创建
        // 拦截到View的创建 获取View之后要去解析

        // 1.创建View
        // If the Factory didn't handle it, let our createView() method try
        View view = createView(parent , name  ,context , attrs) ;

        // 2.1 一个 activity的布局肯定对应多个这样的 SkinView
        if (view != null){
            // 解析所有的 我们关注的属性
            analysisAttrs(view , context , attrs) ;
        }
        return view;
    }


    /**
     * 解析所有的 我们关注的属性
     * @param view
     * @param context
     * @param attrs
     */
    private void analysisAttrs(View view, Context context, AttributeSet attrs) {
        TypedArray array = context.obtainStyledAttributes(attrs, mParallaxAttrs);
        if (array != null && array.getIndexCount() != 0){
            int n = array.getIndexCount();
            ParallaxTag tag = new ParallaxTag();
            for (int i = 0; i < n; i++) {
                int attr = array.getIndex(i);
                switch (attr){
                    case 0:
                         tag.translationXIn = array.getFloat(attr , 0f) ;
                         break;
                    case 1:
                         tag.translationXOut = array.getFloat(attr , 0f) ;
                         break;
                    case 2:
                         tag.translationYIn = array.getFloat(attr , 0f) ;
                         break;
                    case 3:
                         tag.translationYOut = array.getFloat(attr , 0f) ;
                         break;
                }
            }

            // 自定义属性怎么存? 需要一一绑定,在View上边设置tag
            view.setTag(R.id.parallax_tag , tag);
            mParallaxViews.add(view) ;
        }

        array.recycle();

    }

2.3 监听滑动改变位移;

public void setLayoutId(FragmentManager fm  ,int[] layoutIds){
        mFragments.clear();
        for (int layoutId : layoutIds) {
            ParallaxFragment fragment = new ParallaxFragment() ;
            Bundle bundle = new Bundle() ;
            bundle.putInt(ParallaxFragment.LAYOUT_ID_KEY , layoutId);
            fragment.setArguments(bundle);
            mFragments.add(fragment) ;
        }

        // 给我们的ViewPager  设置Adapter
        setAdapter(new ParallaxPagerAdapter(fm));


        // 2.2.3  监听滑动改变位移
        addOnPageChangeListener(new OnPageChangeListener() {

            // 从第一张图 滑动到 第二张图 滑动的过程
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                // positionOffset值是 0-1  positionOffsetPixels值是 0-屏幕的宽度px

                // 获取左边出去的fragment右边进来的fragment
                ParallaxFragment outFragment = mFragments.get(position) ;
                List<View> parallaxViews = outFragment.getParallaxViews();
                for (View parallaxView : parallaxViews) {
                    ParallaxTag tag = (ParallaxTag) parallaxView.getTag(R.id.parallax_tag);

                    // 为什么这样写 ?
                    parallaxView.setTranslationX((-positionOffsetPixels)*tag.translationXOut);
                    parallaxView.setTranslationY((-positionOffsetPixels)*tag.translationYOut);

                }


                try {
                    ParallaxFragment inFragment = mFragments.get(position+1) ;
                    parallaxViews = inFragment.getParallaxViews() ;
                    for (View parallaxView : parallaxViews) {
                        ParallaxTag tag = (ParallaxTag) parallaxView.getTag(R.id.parallax_tag);

                        parallaxView.setTranslationX((getMeasuredWidth()-positionOffsetPixels)*tag.translationXIn);
                        parallaxView.setTranslationY((getMeasuredWidth()-positionOffsetPixels)*tag.translationYIn);

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }


            // 已经切换完毕
            // 已经滑动到具体某一页,比如滑动到第一页、滑动到第二页、滑动到第三页
            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

其实原则就是:
自己先把ViewPager和Fragment创建好,也就是去自定义ParallaxViewPager和ParallaxFragment,最后所需要的地方让开发者直接去给它设置一个布局数组,传递几个layout的布局文件就可以实现左右滑动的视差动画ViewPager

3. 代码如下


3.1 视差动画的ViewPager

/**
 * Email: 2185134304@qq.com
 * Created by JackChen 2018/3/9 8:28
 * Version 1.0
 * Params:
 * Description:  视差动画的ViewPager
*/
public class ParallaxViewPager extends ViewPager {


    private List<ParallaxFragment> mFragments ;

    public ParallaxViewPager(Context context) {
        this(context , null);
    }

    public ParallaxViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        mFragments = new ArrayList<>() ;
    }


    public void setLayoutId(FragmentManager fm  ,int[] layoutIds){
        mFragments.clear();
        for (int layoutId : layoutIds) {
            ParallaxFragment fragment = new ParallaxFragment() ;
            Bundle bundle = new Bundle() ;
            bundle.putInt(ParallaxFragment.LAYOUT_ID_KEY , layoutId);
            fragment.setArguments(bundle);
            mFragments.add(fragment) ;
        }

        // 给我们的ViewPager  设置Adapter
        setAdapter(new ParallaxPagerAdapter(fm));


        // 2.2.3  监听滑动改变位移
        addOnPageChangeListener(new OnPageChangeListener() {

            // 从第一张图 滑动到 第二张图 滑动的过程
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                // positionOffset值是 0-1  positionOffsetPixels值是 0-屏幕的宽度px

                // 获取左边出去的fragment右边进来的fragment
                ParallaxFragment outFragment = mFragments.get(position) ;
                List<View> parallaxViews = outFragment.getParallaxViews();
                for (View parallaxView : parallaxViews) {
                    ParallaxTag tag = (ParallaxTag) parallaxView.getTag(R.id.parallax_tag);

                    // 为什么这样写 ?
                    parallaxView.setTranslationX((-positionOffsetPixels)*tag.translationXOut);
                    parallaxView.setTranslationY((-positionOffsetPixels)*tag.translationYOut);

                }


                try {
                    ParallaxFragment inFragment = mFragments.get(position+1) ;
                    parallaxViews = inFragment.getParallaxViews() ;
                    for (View parallaxView : parallaxViews) {
                        ParallaxTag tag = (ParallaxTag) parallaxView.getTag(R.id.parallax_tag);

                        parallaxView.setTranslationX((getMeasuredWidth()-positionOffsetPixels)*tag.translationXIn);
                        parallaxView.setTranslationY((getMeasuredWidth()-positionOffsetPixels)*tag.translationYIn);

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }


            // 已经切换完毕
            // 已经滑动到具体某一页,比如滑动到第一页、滑动到第二页、滑动到第三页
            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }


    private class ParallaxPagerAdapter extends FragmentPagerAdapter {

        public ParallaxPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }
    }
}

3.2 视差动画的 Fragment

/**
 * Email: 2185134304@qq.com
 * Created by JackChen 2018/3/9 9:09
 * Version 1.0
 * Params:
 * Description:  视差动画的 Fragment
*/
public class ParallaxFragment extends Fragment implements LayoutInflaterFactory{

    public static final String LAYOUT_ID_KEY = "LAYOUT_ID_KEY" ;

    private CompatViewInflater mCompatViewInflater ;

    // 存放所有的需要位移的View
    private List<View> mParallaxViews = new ArrayList<>() ;
    // 存放视差动画的属性
    private int[] mParallaxAttrs = new int[]{R.attr.translationXIn,R.attr.translationXOut ,
                            R.attr.translationYIn,R.attr.translationYOut};


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // 获取布局的id
        int layoutId = getArguments().getInt(LAYOUT_ID_KEY);
        // 2.2.2  把所有需要移动的属性解析出来 ,内涵段子插件式换肤有
        // View创建的时候我们去解析,这里传inflater是有问题的,  单例设计模式代表所有View的创建都是该 Fragment去创建的

        // 克隆一个inflater出来
        inflater = inflater.cloneInContext(getActivity()) ;
        LayoutInflaterCompat.setFactory(inflater , this);

        return inflater.inflate(layoutId , container , false);
    }


    /**
     * 这个方法是实现LayoutInflaterFactory接口后重写的该方法
     * @param parent
     * @param name
     * @param context
     * @param attrs
     * @return
     */
    @Override
    public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
        // 所有的View都会在这里创建
        // 拦截到View的创建 获取View之后要去解析

        // 1.创建View
        // If the Factory didn't handle it, let our createView() method try
        View view = createView(parent , name  ,context , attrs) ;

        // 2.1 一个 activity的布局肯定对应多个这样的 SkinView
        if (view != null){
            // 解析所有的 我们关注的属性
            analysisAttrs(view , context , attrs) ;
        }
        return view;
    }


    /**
     * 解析所有的 我们关注的属性
     * @param view
     * @param context
     * @param attrs
     */
    private void analysisAttrs(View view, Context context, AttributeSet attrs) {
        TypedArray array = context.obtainStyledAttributes(attrs, mParallaxAttrs);
        if (array != null && array.getIndexCount() != 0){
            int n = array.getIndexCount();
            ParallaxTag tag = new ParallaxTag();
            for (int i = 0; i < n; i++) {
                int attr = array.getIndex(i);
                switch (attr){
                    case 0:
                         tag.translationXIn = array.getFloat(attr , 0f) ;
                         break;
                    case 1:
                         tag.translationXOut = array.getFloat(attr , 0f) ;
                         break;
                    case 2:
                         tag.translationYIn = array.getFloat(attr , 0f) ;
                         break;
                    case 3:
                         tag.translationYOut = array.getFloat(attr , 0f) ;
                         break;
                }
            }

            // 自定义属性怎么存? 需要一一绑定,在View上边设置tag
            view.setTag(R.id.parallax_tag , tag);
            mParallaxViews.add(view) ;
        }

        array.recycle();

    }


    private View createView(View parent, String name, Context context, AttributeSet attrs) {

        final boolean isPre21 = Build.VERSION.SDK_INT < 21;
        if (mCompatViewInflater == null){
            mCompatViewInflater = new CompatViewInflater() ;
        }

        // We only want the View to inherit it's context if we're running pre-v21
        final boolean inheritContext = isPre21 && true
                && shouldInheritContext((ViewParent) parent);

        return mCompatViewInflater.createView(parent, name, context, attrs, inheritContext,
                isPre21, /* Only read android:theme pre-L (L+ handles this anyway) */
                true /* Read read app:theme as a fallback at all times for legacy reasons */
        );
    } ;



    private boolean shouldInheritContext(ViewParent parent) {
        if (parent == null) {
            // The initial parent is null so just return false
            return false;
        }
        while (true) {
            if (parent == null) {
                // Bingo. We've hit a view which has a null parent before being terminated from
                // the loop. This is (most probably) because it's the root view in an inflation
                // call, therefore we should inherit. This works as the inflated layout is only
                // added to the hierarchy at the end of the inflate() call.
                return true;
            } else if (!(parent instanceof View)
                    || ViewCompat.isAttachedToWindow((View) parent)) {
                // We have either hit the window's decor view, a parent which isn't a View
                // (i.e. ViewRootImpl), or an attached view, so we know that the original parent
                // is currently added to the view hierarchy. This means that it has not be
                // inflated in the current inflate() call and we should not inherit the context.
                return false;
            }
            parent = parent.getParent();
        }
    }


    public List<View> getParallaxViews(){
        return mParallaxViews ;
    }

}

3.3 CompatViewInflater代码如下

/**
 * Email: 2185134304@qq.com
 * Created by JackChen 2018/3/9 8:30
 * Version 1.0
 * Params:
 * Description:
 */

/**
 * This class is responsible for manually inflating our tinted widgets which are used on devices
 * running {@link android.os.Build.VERSION_CODES#KITKAT KITKAT} or below. As such, this class
 * should only be used when running on those devices.
 * <p>This class two main responsibilities: the first is to 'inject' our tinted views in place of
 * the framework versions in layout inflation; the second is backport the {@code android:theme}
 * functionality for any inflated widgets. This include theme inheritance from it's parent.
 */
public class CompatViewInflater {

    private static final Class<?>[] sConstructorSignature = new Class[]{
            Context.class, AttributeSet.class};
    private static final int[] sOnClickAttrs = new int[]{android.R.attr.onClick};

    private static final String LOG_TAG = "AppCompatViewInflater";

    private static final Map<String, Constructor<? extends View>> sConstructorMap
            = new ArrayMap<>();

    private final Object[] mConstructorArgs = new Object[2];

    public final View createView(View parent, final String name, @NonNull Context context,
                                 @NonNull AttributeSet attrs, boolean inheritContext,
                                 boolean readAndroidTheme, boolean readAppTheme) {
        final Context originalContext = context;

        // We can emulate Lollipop's android:theme attribute propagating down the view hierarchy
        // by using the parent's context
        if (inheritContext && parent != null) {
            context = parent.getContext();
        }
        if (readAndroidTheme || readAppTheme) {
            // We then apply the theme on the context, if specified
            context = themifyContext(context, attrs, readAndroidTheme, readAppTheme);
        }

        View view = null;

        // We need to 'inject' our tint aware Views in place of the standard framework versions
        switch (name) {
            case "TextView":
                view = new AppCompatTextView(context, attrs);
                break;
            case "ImageView":
                view = new AppCompatImageView(context, attrs);
                break;
            case "Button":
                view = new AppCompatButton(context, attrs);
                break;
            case "EditText":
                view = new AppCompatEditText(context, attrs);
                break;
            case "Spinner":
                view = new AppCompatSpinner(context, attrs);
                break;
            case "ImageButton":
                view = new AppCompatImageButton(context, attrs);
                break;
            case "CheckBox":
                view = new AppCompatCheckBox(context, attrs);
                break;
            case "RadioButton":
                view = new AppCompatRadioButton(context, attrs);
                break;
            case "CheckedTextView":
                view = new AppCompatCheckedTextView(context, attrs);
                break;
            case "AutoCompleteTextView":
                view = new AppCompatAutoCompleteTextView(context, attrs);
                break;
            case "MultiAutoCompleteTextView":
                view = new AppCompatMultiAutoCompleteTextView(context, attrs);
                break;
            case "RatingBar":
                view = new AppCompatRatingBar(context, attrs);
                break;
            case "SeekBar":
                view = new AppCompatSeekBar(context, attrs);
                break;
        }

        if (view == null) {
            // If the original context does not equal our themed context, then we need to manually
            // inflate it using the name so that android:theme takes effect.
            view = createViewFromTag(context, name, attrs);
        }

        if (view != null) {
            // If we have created a view, check it's android:onClick
            checkOnClickListener(view, attrs);
        }

        return view;
    }

    private View createViewFromTag(Context context, String name, AttributeSet attrs) {
        if (name.equals("view")) {
            name = attrs.getAttributeValue(null, "class");
        }

        try {
            mConstructorArgs[0] = context;
            mConstructorArgs[1] = attrs;

            if (-1 == name.indexOf('.')) {
                // try the android.widget prefix first...
                return createView(context, name, "android.widget.");
            } else {
                return createView(context, name, null);
            }
        } catch (Exception e) {
            // We do not want to catch these, lets return null and let the actual LayoutInflater
            // try
            return null;
        } finally {
            // Don't retain references on context.
            mConstructorArgs[0] = null;
            mConstructorArgs[1] = null;
        }
    }

    /**
     * android:onClick doesn't handle views with a ContextWrapper context. This method
     * backports new framework functionality to traverse the Context wrappers to find a
     * suitable target.
     */
    private void checkOnClickListener(View view, AttributeSet attrs) {
        final Context context = view.getContext();

        if (!ViewCompat.hasOnClickListeners(view) || !(context instanceof ContextWrapper)) {
            // Skip our compat functionality if: the view doesn't have an onClickListener,
            // or the Context isn't a ContextWrapper
            return;
        }

        final TypedArray a = context.obtainStyledAttributes(attrs, sOnClickAttrs);
        final String handlerName = a.getString(0);
        if (handlerName != null) {
            view.setOnClickListener(new DeclaredOnClickListener(view, handlerName));
        }
        a.recycle();
    }

    private View createView(Context context, String name, String prefix)
            throws ClassNotFoundException, InflateException {
        Constructor<? extends View> constructor = sConstructorMap.get(name);

        try {
            if (constructor == null) {
                // Class not found in the cache, see if it's real, and try to add it
                Class<? extends View> clazz = context.getClassLoader().loadClass(
                        prefix != null ? (prefix + name) : name).asSubclass(View.class);

                constructor = clazz.getConstructor(sConstructorSignature);
                sConstructorMap.put(name, constructor);
            }
            constructor.setAccessible(true);
            return constructor.newInstance(mConstructorArgs);
        } catch (Exception e) {
            // We do not want to catch these, lets return null and let the actual LayoutInflater
            // try
            return null;
        }
    }

    /**
     * Allows us to emulate the {@code android:theme} attribute for devices before L.
     */
    private static Context themifyContext(Context context, AttributeSet attrs,
                                          boolean useAndroidTheme, boolean useAppTheme) {
        final TypedArray a = context.obtainStyledAttributes(attrs, android.support.v7.appcompat.R.styleable.View, 0, 0);
        int themeId = 0;
        if (useAndroidTheme) {
            // First try reading android:theme if enabled
            themeId = a.getResourceId(android.support.v7.appcompat.R.styleable.View_android_theme, 0);
        }
        if (useAppTheme && themeId == 0) {
            // ...if that didn't work, try reading app:theme (for legacy reasons) if enabled
            themeId = a.getResourceId(android.support.v7.appcompat.R.styleable.View_theme, 0);

            if (themeId != 0) {
                Log.i(LOG_TAG, "app:theme is now deprecated. "
                        + "Please move to using android:theme instead.");
            }
        }
        a.recycle();

        if (themeId != 0 && (!(context instanceof ContextThemeWrapper)
                || ((ContextThemeWrapper) context).getThemeResId() != themeId)) {
            // If the context isn't a ContextThemeWrapper, or it is but does not have
            // the same theme as we need, wrap it in a new wrapper
            context = new ContextThemeWrapper(context, themeId);
        }
        return context;
    }

    /**
     * An implementation of OnClickListener that attempts to lazily load a
     * named click handling method from a parent or ancestor context.
     */
    private static class DeclaredOnClickListener implements View.OnClickListener {
        private final View mHostView;
        private final String mMethodName;

        private Method mResolvedMethod;
        private Context mResolvedContext;

        public DeclaredOnClickListener(@NonNull View hostView, @NonNull String methodName) {
            mHostView = hostView;
            mMethodName = methodName;
        }

        @Override
        public void onClick(@NonNull View v) {
            if (mResolvedMethod == null) {
                resolveMethod(mHostView.getContext(), mMethodName);
            }

            try {
                mResolvedMethod.invoke(mResolvedContext, v);
            } catch (IllegalAccessException e) {
                throw new IllegalStateException(
                        "Could not execute non-public method for android:onClick", e);
            } catch (InvocationTargetException e) {
                throw new IllegalStateException(
                        "Could not execute method for android:onClick", e);
            }
        }

        @NonNull
        private void resolveMethod(@Nullable Context context, @NonNull String name) {
            while (context != null) {
                try {
                    if (!context.isRestricted()) {
                        final Method method = context.getClass().getMethod(mMethodName, View.class);
                        if (method != null) {
                            mResolvedMethod = method;
                            mResolvedContext = context;
                            return;
                        }
                    }
                } catch (NoSuchMethodException e) {
                    // Failed to find method, keep searching up the hierarchy.
                }

                if (context instanceof ContextWrapper) {
                    context = ((ContextWrapper) context).getBaseContext();
                } else {
                    // Can't search up the hierarchy, null out and fail.
                    context = null;
                }
            }

            final int id = mHostView.getId();
            final String idText = id == View.NO_ID ? "" : " with id '"
                    + mHostView.getContext().getResources().getResourceEntryName(id) + "'";
            throw new IllegalStateException("Could not find method " + mMethodName
                    + "(View) in a parent or ancestor Context for android:onClick "
                    + "attribute defined on view " + mHostView.getClass() + idText);
        }
    }
}

3.4 视差动画的 tag标记 代码如下

/**
 * Email: 2185134304@qq.com
 * Created by JackChen 2018/3/9 9:01
 * Version 1.0
 * Params:
 * Description:  视差动画的 tag 标记
*/
public class ParallaxTag {
    public float translationXIn ;
    public float translationXOut ;
    public float translationYIn ;
    public float translationYOut ;

    @Override
    public String toString() {
        return "ParallaxTag{" +
                "translationXIn=" + translationXIn +
                ", translationXOut=" + translationXOut +
                ", translationYIn=" + translationYIn +
                ", translationYOut=" + translationYOut +
                '}';
    }
}

3.5 activity_main的布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <cn.uploo.yhh.animation.ParallaxViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/parallax_vp"
        android:background="@android:color/holo_orange_dark"
        >

    </cn.uploo.yhh.animation.ParallaxViewPager>

    <Button
        android:id="@+id/guide_btn_start"
        android:layout_width="@dimen/px220"
        android:layout_height="@dimen/px80"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="60dp"
        android:layout_centerHorizontal="true"
        android:background="@drawable/btn_go_experence_selector"
        android:text="立即体验"
        android:textColor="@color/ic_taobao"
        android:paddingLeft="@dimen/px20"
        android:paddingRight="@dimen/px20"
        android:visibility="gone"
        />

</RelativeLayout>

3.5 3个布局文件如下:
fragment_page_first.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rootFirstPage"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:background="@android:color/holo_orange_dark"
    tools:ignore="ContentDescription">

    <ImageView
        android:id="@+id/ivFirstImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@mipmap/s_0_1"
        app:layout_heightPercent="35%"
        app:layout_widthPercent="50%"
        app:translationXIn="0.4"
        app:translationYIn="0.4"
        app:translationYOut="0.4"
        app:translationXOut="0.4" />

    <ImageView
        android:id="@+id/ivSecondImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:src="@mipmap/s_0_2"
        app:layout_heightPercent="10%"
        app:layout_marginRightPercent="12%"
        app:layout_marginTopPercent="27%"
        app:layout_widthPercent="12%"
        app:translationXIn="0.12"
        app:translationYIn="0.82"
        app:translationYOut="0.82"
        app:translationXOut="0.12" />

    <ImageView
        android:id="@+id/ivThirdImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:src="@mipmap/s_0_3"
        app:layout_heightPercent="25%"

        app:layout_marginLeftPercent="14%"

        app:layout_marginTopPercent="49%"
        app:layout_widthPercent="30%"
        app:translationXIn="0.16"
        app:translationXOut="0.16" />

    <ImageView
        android:id="@+id/ivFourthImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/s_0_4"
        app:layout_heightPercent="15%"
        app:layout_marginLeftPercent="14%"
        app:layout_marginTopPercent="39%"
        app:layout_widthPercent="20%"
        app:translationXIn="0.02"
        app:translationXOut="0.02" />

    <ImageView
        android:id="@+id/ivFifthImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_centerHorizontal="true"
        android:src="@mipmap/s_0_5"
        app:layout_heightPercent="15%"

        app:layout_marginTopPercent="22%"

        app:layout_widthPercent="45%"
        app:translationXIn="0.06"
        app:translationXOut="0.06" />

    <ImageView
        android:id="@+id/ivSixthImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:src="@mipmap/s_0_6"
        app:layout_heightPercent="6%"

        app:layout_marginLeftPercent="4%"

        app:layout_marginTopPercent="26%"
        app:layout_widthPercent="6%"
        app:translationXIn="0.18"
        app:translationXOut="0.18" />

    <ImageView
        android:id="@+id/ivSeventhImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:src="@mipmap/s_0_7"
        app:layout_heightPercent="8%"

        app:layout_marginLeftPercent="14%"

        app:layout_marginTopPercent="25%"
        app:layout_widthPercent="9%"
        app:translationXIn="0.28"
        app:translationXOut="0.28" />

    <ImageView
        android:id="@+id/ivEighthImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:src="@mipmap/s_0_8"
        app:layout_heightPercent="6%"

        app:layout_marginLeftPercent="77%"

        app:layout_marginTopPercent="38%"
        app:layout_widthPercent="8%"
        app:translationXIn="0.34"
        app:translationXOut="0.34" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:text="@string/text_web_ceo"
        android:textColor="@android:color/white"
        android:textSize="@dimen/text_size_large"
        app:layout_heightPercent="15%"
        app:layout_marginBottomPercent="11%"
        app:layout_widthPercent="45%" />
</android.support.percent.PercentRelativeLayout>

fragment_page_second.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentRelativeLayout
    android:id="@+id/rootSecondPage"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:ignore="ContentDescription"
    tools:background="@android:color/holo_green_dark">

    <ImageView
        android:id="@+id/ivFirstImage"
        app:translationXIn="0.4"
        app:translationXOut="0.4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"

        android:src="@mipmap/s_1_1"

        app:layout_heightPercent="25%"
        app:layout_widthPercent="45%"/>

    <ImageView
        android:id="@+id/ivSecondImage"
        app:translationXIn="1.2"
        app:translationXOut="1.2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:alpha=".5"
        android:src="@mipmap/s_1_2"
        app:layout_heightPercent="6%"
        app:layout_marginRightPercent="14%"
        app:layout_marginTopPercent="30%"
        app:layout_widthPercent="19%"/>


    <ImageView
        android:id="@+id/ivThirdImage"
        app:translationXIn="0.16"
        app:translationXOut="0.16"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:src="@mipmap/s_1_3"

        app:layout_heightPercent="8%"
        app:layout_marginLeftPercent="46%"
        app:layout_marginTopPercent="43%"
        app:layout_widthPercent="15%"/>

    <ImageView
        android:id="@+id/ivFourthImage"
        app:translationXIn="0.2"
        app:translationXOut="0.2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:src="@mipmap/s_1_4"

        app:layout_heightPercent="10%"
        app:layout_marginLeftPercent="44%"
        app:layout_marginTopPercent="52%"
        app:layout_widthPercent="13%"/>

    <ImageView
        app:translationXIn="0.06"
        app:translationXOut="0.06"
        android:id="@+id/ivSixthImage"

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:src="@mipmap/s_1_6"

        app:layout_heightPercent="7%"
        app:layout_marginLeftPercent="20%"
        app:layout_marginTopPercent="47%"
        app:layout_widthPercent="20%"/>

    <ImageView
        app:translationXIn="0.18"
        app:translationXOut="0.18"
        android:id="@+id/ivSeventhImage"

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:src="@mipmap/s_1_7"

        app:layout_heightPercent="8%"
        app:layout_marginLeftPercent="22%"
        app:layout_marginTopPercent="35%"
        app:layout_widthPercent="17%"/>

    <ImageView
        app:translationXIn="0.28"
        app:translationXOut="0.28"
        android:id="@+id/ivFifthImage"

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"

        android:src="@mipmap/s_1_5"

        app:layout_heightPercent="10%"
        app:layout_marginTopPercent="33%"
        app:layout_widthPercent="15%"/>

    <ImageView
        app:translationXIn="0.14"
        app:translationXOut="0.14"
        android:id="@+id/ivEighthImage"

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"

        android:src="@mipmap/s_1_8"

        app:layout_heightPercent="10%"
        app:layout_marginTopPercent="22%"
        app:layout_widthPercent="25%"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"

        android:gravity="center"

        android:text="@string/text_web_dev"
        android:textColor="@android:color/white"
        android:textSize="@dimen/text_size_large"

        app:layout_heightPercent="15%"
        app:layout_marginBottomPercent="11%"
        app:layout_widthPercent="45%"/>
</android.support.percent.PercentRelativeLayout>

fragment_page_third.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentRelativeLayout
    android:id="@+id/rootThirdPage"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:ignore="ContentDescription"
    tools:background="@android:color/holo_blue_dark">

    <ImageView
        android:id="@+id/ivThirdImage"
        app:translationXIn="0.4"
        app:translationXOut="0.4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:src="@mipmap/s_2_3"

        app:layout_heightPercent="40%"
        app:layout_marginTopPercent="27%"
        app:layout_widthPercent="100%"/>

    <ImageView
        android:id="@+id/ivSecondImage"
        app:translationXIn="0.12"
        app:translationXOut="0.12"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:alpha=".5"

        android:src="@mipmap/s_2_2"

        app:layout_heightPercent="27%"
        app:layout_marginLeftPercent="20%"
        app:layout_marginRightPercent="12%"
        app:layout_marginTopPercent="20%"
        app:layout_widthPercent="75%"/>

    <ImageView
        android:id="@+id/ivFirstImage"
        app:translationXIn="0.16"
        app:translationXOut="0.16"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"

        android:src="@mipmap/s_2_1"

        app:layout_heightPercent="18%"
        app:layout_widthPercent="55%"/>


    <ImageView
        android:id="@+id/ivFourthImage"
        app:translationXIn="0.2"
        app:translationXOut="0.2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:src="@mipmap/s_2_4"

        app:layout_heightPercent="7%"
        app:layout_marginLeftPercent="33%"
        app:layout_marginTopPercent="34%"
        app:layout_widthPercent="16%"/>

    <ImageView
        android:id="@+id/ivSixthImage"
        app:translationXIn="0.06"
        app:translationXOut="0.06"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:src="@mipmap/s_2_6"

        app:layout_heightPercent="7%"
        app:layout_marginLeftPercent="57%"
        app:layout_marginTopPercent="29%"
        app:layout_widthPercent="14%"/>

    <ImageView
        android:id="@+id/ivSeventhImage"
        app:translationXIn="0.18"
        app:translationXOut="0.18"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:src="@mipmap/s_2_7"

        app:layout_heightPercent="15%"
        app:layout_marginLeftPercent="65%"
        app:layout_marginTopPercent="47%"
        app:layout_widthPercent="25%"/>

    <ImageView
        android:id="@+id/ivFifthImage"
        app:translationXIn="0.28"
        app:translationXOut="0.28"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:src="@mipmap/s_2_5"

        app:layout_heightPercent="10%"
        app:layout_marginLeftPercent="77%"
        app:layout_marginTopPercent="30%"
        app:layout_widthPercent="15%"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"

        android:gravity="center"

        android:text="@string/text_e_commerce"
        android:textColor="@android:color/white"
        android:textSize="@dimen/text_size_large"

        app:layout_heightPercent="15%"
        app:layout_marginBottomPercent="11%"
        app:layout_widthPercent="45%"/>
</android.support.percent.PercentRelativeLayout>

具体代码已上传至github:
https://github.com/shuai999/View_day27.git

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,122评论 6 505
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,070评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,491评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,636评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,676评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,541评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,292评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,211评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,655评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,846评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,965评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,684评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,295评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,894评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,012评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,126评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,914评论 2 355

推荐阅读更多精彩内容