Android——支持水平和垂直视差移动的ParallaxBackgroundView(使用篇)

horizontal_parallax_bg.gif

vertical_parallax_bg.gif

概述

Orientation

ParallaxBackgroundView是一个支持水平或者垂直方向视差移动的view。水平或者垂直方向可通过以下两种方式设置:

代码:

setParallaxOrientation(ParallaxBackgroundView.PARALLAX_VERTICAL);

xml属性:

app:parallaxOrientation="horizontal"

Parallax Background

视差背景可以通过代码或者xml属性设置:

代码:

// setParallaxBackground(Drawable)
setParallaxBackgroundResource(int);

xml属性:

app:parallaxBackground="@drawable/horizontal_parallax_bg"

切记不要和View的setBackground()搞混

Parallax Offset

视差偏移通过以下方式设置:

setParallaxPercent(float)

NOTE

ParallaxBackgroundView有两种模式:

  • MODE_PRE_SCALE——使用更多的内存,但视差滑动会更平滑。因为,bitmap已经在内存中预先缩放好了,在onDraw()的绘制中只是移动bitmap即可,不需要进行缩放。
  • MODE_POST_SCALE——使用更少的内存,但会加大处理器的运算。因为缩放是在onDraw()的绘制中实时进行。

默认属性

  • isParallax——true
  • parallaxMode——postScale
  • parallaxOrientation——horizontal

lib依赖

使用gradle:

compile 'com.xpleemoon.view:parallaxbackgroundview:1.0.1'

或者使用maven

<dependency>
  <groupId>com.xpleemoon.view</groupId>
  <artifactId>parallaxbackgroundview</artifactId>
  <version>1.0.1</version>
  <type>pom</type>
</dependency>

ParallaxBackgroundView如何使用

视差效果需要使用两层layer实现:

  • 一层作为content,比如ScrollView、ListView or RecyclerView等可以滑动的view
  • 另一层作为background,即ParallaxBackgroundView

下面用ViewPager作content,以xml属性和代码分别设置ParallaxBackgroundView来实现视差效果。

通过xml属性设置ParallaxBackgroundView

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:context="com.xpleemoon.demo.parallaxbackgroundview.HorizontalParallaxActivity">

    <com.xpleemoon.view.ParallaxBackgroundView
        android:id="@+id/parallax_bg"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:isParallax="true"
        app:parallaxMode="postScale"
        app:parallaxOrientation="horizontal"
        app:parallaxBackground="@drawable/horizontal_parallax_bg"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>
        final ParallaxBackgroundView bg = (ParallaxBackgroundView) findViewById(R.id.parallax_bg);

        ViewPager pager = (ViewPager) findViewById(R.id.pager);
        final PagerAdapter adapter = new MyAdapter();
        pager.setAdapter(adapter);
        pager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                // this is called while user's flinging with:
                // position is the page number
                // positionOffset is the percentage scrolled (0...1)
                // positionOffsetPixels is the pixel offset related to that percentage
                // so we got everything we need ....
                float finalPercentage = ((position + positionOffset) * 100 / adapter.getCount()); // percentage of this page+offset respect the total pages
                // now you have to scroll the background layer to this position. You can either adjust the clipping or
                // the background X coordinate, or a scroll position if you use an image inside an scrollview ...
                // I personally like to extend View and draw a scaled bitmap with a clipping region (drawBitmap with Rect parameters), so just modifying the X position then calling invalidate will do. See attached source ParallaxBackground
                bg.setParallaxPercent(finalPercentage);
            }
        });

通过代码设置ParallaxBackgroundView

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:context="com.xpleemoon.demo.parallaxbackgroundview.VerticalParallaxActivity">

    <com.xpleemoon.view.ParallaxBackgroundView
        android:id="@+id/parallax_bg"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <fr.castorflex.android.verticalviewpager.VerticalViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>
        final ParallaxBackgroundView bg = (ParallaxBackgroundView) findViewById(R.id.parallax_bg);
        bg.setParallax(true);
        bg.setParallaxMode(ParallaxBackgroundView.MODE_POST_SCALE);
        bg.setParallaxOrientation(ParallaxBackgroundView.PARALLAX_VERTICAL);
        bg.setParallaxBackgroundResource(R.drawable.vertical_parallax_bg);

        VerticalViewPager pager = (VerticalViewPager) findViewById(R.id.pager);
        final PagerAdapter adapter = new MyAdapter();
        pager.setAdapter(adapter);
        pager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                // this is called while user's flinging with:
                // position is the page number
                // positionOffset is the percentage scrolled (0...1)
                // positionOffsetPixels is the pixel offset related to that percentage
                // so we got everything we need ....
                float finalPercentage = ((position + positionOffset) * 100 / adapter.getCount()); // percentage of this page+offset respect the total pages
                // now you have to scroll the background layer to this position. You can either adjust the clipping or
                // the background X coordinate, or a scroll position if you use an image inside an scrollview ...
                // I personally like to extend View and draw a scaled bitmap with a clipping region (drawBitmap with Rect parameters), so just modifying the X position then calling invalidate will do. See attached source ParallaxBackground
                bg.setParallaxPercent(finalPercentage);
            }
        });

代码猛戳这里

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,614评论 25 709
  • 1. Outline 本文主要从以下三个大的方面来说明一下2D Graphic 绘图的一些相关函数及应用。 Col...
    lee_3do阅读 3,103评论 0 11
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,296评论 4 61
  • 风沙沙的做响 来向我告别 我知道你要离开了 这别离是必然的 我都知道 我早已做好了准备 把心事藏在天涯海角 一半天...
    未明花香阅读 146评论 0 0
  • 要说上个月最热门的基金,无疑要数某只抱农药大腿的QDII。 热门到啥程度?不仅以40%+的涨幅录得半年业绩之王,还...
    拖天航线图阅读 382评论 0 0