博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ViewPager背景实现Parallax(平差化)效果
阅读量:6925 次
发布时间:2019-06-27

本文共 7924 字,大约阅读时间需要 26 分钟。

hot3.png

import android.annotation.SuppressLint;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Rect;import android.graphics.drawable.BitmapDrawable;import android.graphics.drawable.Drawable;import android.support.v4.view.ViewPager;import android.util.AttributeSet;import android.util.Log;@SuppressLint("NewApi")public class ParallaxViewPager extends ViewPager {    public static final int FIT_WIDTH = 0;    public static final int FIT_HEIGHT = 1;    public static final float OVERLAP_FULL = 1f;    public static final float OVERLAP_HALF = 0.5f;    public static final float OVERLAP_QUARTER = 0.25f;    private static final float CORRECTION_PERCENTAGE = 0.01f;    public Bitmap bitmap;    private Rect source, destination;    private int scaleType;    private int chunkWidth;    private int projectedWidth;    private float overlap;    private OnPageChangeListener secondOnPageChangeListener;    public ParallaxViewPager(Context context) {        super(context);        init();    }    public ParallaxViewPager(Context context, AttributeSet attrs) {        super(context, attrs);        init();    }    private void init() {        source = new Rect();        destination = new Rect();        scaleType = FIT_HEIGHT;        overlap = OVERLAP_HALF;        setOnPageChangeListener(new OnPageChangeListener() {            @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {                if (bitmap != null) {                    source.left = (int) Math.floor((position + positionOffset - CORRECTION_PERCENTAGE) * chunkWidth);                    source.right = (int) Math.ceil((position + positionOffset + CORRECTION_PERCENTAGE) * chunkWidth + projectedWidth);                    destination.left = (int) Math.floor((position + positionOffset - CORRECTION_PERCENTAGE) * getWidth());                    destination.right = (int) Math.ceil((position + positionOffset + 1 + CORRECTION_PERCENTAGE) * getWidth());                    invalidate();                }                if (secondOnPageChangeListener != null) {                    secondOnPageChangeListener.onPageScrolled(position, positionOffset, positionOffsetPixels);                }            }            @Override public void onPageSelected(int position) {                if (secondOnPageChangeListener != null) {                    secondOnPageChangeListener.onPageSelected(position);                }            }            @Override public void onPageScrollStateChanged(int state) {                if (secondOnPageChangeListener != null) {                    secondOnPageChangeListener.onPageScrollStateChanged(state);                }            }        });    }    @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) {        super.onSizeChanged(w, h, oldw, oldh);        destination.top = 0;        destination.bottom = h;        if (getAdapter() != null && bitmap != null)            calculateParallaxParameters();    }    private void calculateParallaxParameters() {        if (bitmap.getWidth() < getWidth() && bitmap.getWidth() < bitmap.getHeight() && scaleType == FIT_HEIGHT) {            Log.w(ParallaxViewPager.class.getName(), "Invalid bitmap bounds for the current device, parallax effect will not work.");        }        final float ratio = (float) getHeight() / bitmap.getHeight();        if (ratio != 1) {            switch (scaleType) {                case FIT_WIDTH:                    source.top = (int) ((bitmap.getHeight() - bitmap.getHeight() / ratio) / 2);                    source.bottom = bitmap.getHeight() - source.top;                    chunkWidth = (int) Math.ceil((float) bitmap.getWidth() / (float) getAdapter().getCount());                    projectedWidth = chunkWidth;                    break;                case FIT_HEIGHT:                default:                    source.top = 0;                    source.bottom = bitmap.getHeight();                    projectedWidth = (int) Math.ceil(getWidth() / ratio);                    chunkWidth = (int) Math.ceil((bitmap.getWidth() - projectedWidth) / (float) getAdapter().getCount() * overlap);                    break;            }        }    }    /**     * Sets the background from a resource file.     *     * @param resid     */    @Override public void setBackgroundResource(int resid) {        bitmap = BitmapFactory.decodeResource(getResources(), resid);    }    /**     * Sets the background from a Drawable.     *     * @param background     */    @Override public void setBackground(Drawable background) {        bitmap = ((BitmapDrawable) background).getBitmap();    }    /**     * Deprecated.     * Sets the background from a Drawable.     *     * @param background     */    @Override public void setBackgroundDrawable(Drawable background) {        bitmap = ((BitmapDrawable) background).getBitmap();    }    /**     * Sets the background from a bitmap.     *     * @param bitmap     * @return The ParallaxViewPager object itself.     */    public ParallaxViewPager setBackground(Bitmap bitmap) {        this.bitmap = bitmap;        return this;    }    /**     * Sets how the view should scale the background. The available choices are:     * 
         * 
  • FIT_HEIGHT - the height of the image is resized to matched the height of the View, also stretching the width to keep the aspect ratio. The non-visible part of the bitmap is divided into equal parts, each of them sliding in at the proper position.
  •      * 
  • FIT_WIDTH - the width of the background image is divided into equal chunks, each taking up the whole width of the screen.
  •      * 
     *     * @param scaleType     * @return     */    public ParallaxViewPager setScaleType(final int scaleType) {        if (scaleType != FIT_WIDTH && scaleType != FIT_HEIGHT)            throw new IllegalArgumentException("Illegal argument: scaleType must be FIT_WIDTH or FIT_HEIGHT");        this.scaleType = scaleType;        return this;    }    /**     * Sets the amount of overlapping with the setOverlapPercentage(final float percentage) method. This is a number between 0 and 1, the smaller it is, the slower is the background scrolling.     *     * @param percentage     * @return The ParallaxViewPager object itself.     */    public ParallaxViewPager setOverlapPercentage(final float percentage) {        if (percentage <= 0 || percentage >= 1)            throw new IllegalArgumentException("Illegal argument: percentage must be between 0 and 1");        overlap = percentage;        return this;    }    /**     * Recalculates the parameters of the parallax effect, useful after changes in runtime.     *     * @return The ParallaxViewPager object itself.     */    public ParallaxViewPager invalidateParallaxParameters() {        calculateParallaxParameters();        return this;    }    @Override protected void onDraw(Canvas canvas) {        if (bitmap != null)            canvas.drawBitmap(bitmap, source, destination, null);    }    public void addOnPageChangeListener(OnPageChangeListener listener) {        secondOnPageChangeListener = listener;    }}

1、创建布局XML parallaxviewpager。

        通过XML或下列方法设置背景:

               

  • setBackgroundResource(int resid)

  • setBackground(Drawable background) or setBackgroundDrawable(Drawable background)

  • setBackground(Bitmap bitmap)

    指定视图的规模应该与setscaletype背景(scaletype)方法。

选择下列参数:

fit_height    意味着图像的高度调整到相匹配的观点的高度,同时拉伸宽度保持纵横比。 

                  位图的不可见的部分分成相等的部分,他们每个人都在适当的位置的滑动。这是默认值。

fit_width     背景图像的宽度被分成相等的块,每个占据整个屏幕宽度。

                   这种模式是不寻常的视差效应,为背景的滚动速度等于视图的速度。

设定的setoverlappercentage重叠(最后的浮动比例)的方法。这是一个0和1之间的数字,小,速度慢的背景滚动。默认值是百分之50。这只适用于fit_height。

    final ParallaxViewPager parallaxViewPager = new ParallaxViewPager(this);    parallaxViewPager.setAdapter(new MyPagerAdapter());    parallaxViewPager.setBackgroundResource(R.drawable.nagy);    setContentView(parallaxViewPager);

14151247_a4jP.gif

转载于:https://my.oschina.net/oppo4545/blog/330474

你可能感兴趣的文章
【Visual C++】游戏开发四十八 浅墨DirectX教程十六 三维地形系统的实现
查看>>
程序的流程控制(C++)
查看>>
C++应用程序性能优化(五)——操作系统的内存管理
查看>>
嵌入式 Linux C语言(十)——静态库函数和动态库函数
查看>>
LVS之VS/DR搭建web集群实战!!!
查看>>
Java基础学习总结(20)——基础语法
查看>>
大型网站技术架构(四)网站的高性能架构
查看>>
jquery 刮奖插件
查看>>
es6--export default
查看>>
Java JNI 调用C++ API及中文编码问题
查看>>
Springmvc的开发流程--附带实例
查看>>
Java23中设计模式(Design Patterns)详解
查看>>
Java基础学习总结(3)——抽象类
查看>>
win7还未用熟 已经win10系统的身影了
查看>>
c#中的internal关键字
查看>>
Redis未授权访问之反弹shell
查看>>
C#中使用split分割字符串的几种方法
查看>>
使用Nginx如何配置Tomcat访问日志记录真实IP
查看>>
我的友情链接
查看>>
利用Zabbix的自动注册功能添加局域网中的服务器进行监控
查看>>