Android视差效果和视图寻呼机

Android Parallax Effect and View Pager(Android视差效果和视图寻呼机)

本文介绍了Android视差效果和视图寻呼机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的应用程序中实现视差效果.我有一个 FragmentActivity 实现 OnPageChangeListener 接口并监听我的 ViewPager 滚动事件.

I'm trying to achieve a parallax effect in my application. I have a FragmentActivity that implements the OnPageChangeListener interface and listens to my ViewPager scrolling events.

为了在我的 XML 中实现这种效果,我在所有其他视图的后面都有一个 LinearLayout,并在 onPageScrolled 回调中移动它.如果我只是滑动就可以了,效果会起作用并且背景位置会发生变化.但是当我的手指离开屏幕时,背景会在他原来的位置重新绘制(即使我在新页面中).我不明白为什么会这样.这是我的 FragmentActivity 的代码:

To achieve this effect in my XML I have a LinearLayout behind all the other views with my background, and I move it in the onPageScrolled callback. If I simply swipe it's all ok, the effect works and the background position change. But when my finger leaves the screen, the background is redrawn at his original position (even if I'm in a new page). I can't understand why this happens. Here the code of my FragmentActivity :

public class MainActivity extends FragmentActivity implements OnPageChangeListener {

// DEFINE THE PAGEADAPTER
private ViewPager viewPager;
private com.angtrim.ecomilano.PagerAdapter pagerAdapter;
private int oldPosition = 0;
private int offSet = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // CREATE VIEWPAGER
    viewPager = (ViewPager) findViewById(R.id.viewpager);
    pagerAdapter = new PagerAdapter(getApplicationContext(),getSupportFragmentManager());
    // SET THE ADAPTER
    viewPager.setAdapter(pagerAdapter);        
    // SET FIRST ITEM
    viewPager.setCurrentItem(0);   
    // SET CHANGE PAGE LISTENER
    viewPager.setOnPageChangeListener(this);        
}    

@Override
public void onPageScrollStateChanged(int arg0) {
    // TODO Auto-generated method stub      
}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {

    // RIGHT SWIPE
    if((oldPosition < arg2))
    {       
        offSet = (int)((arg2 - oldPosition)*0.5);               
        oldPosition = arg2;
    }
    // LEFT SWIPE
    else if( (oldPosition > arg2))
    {   
        offSet = (int) (-(oldPosition - arg2)*0.5);         
        oldPosition = arg2;
    }

    findViewById(R.id.backi).offsetLeftAndRight(offSet);        
}

@Override
public void onPageSelected(int arg0) {
    // TODO Auto-generated method stub

}
}

谢谢.

推荐答案

我知道它有点老了,但是看看那个 https://github.com/xgc1986/ParallaxPagerLibrary

I know that it's a bit old but take a look to that https://github.com/xgc1986/ParallaxPagerLibrary

它不会覆盖 onDraw 方法,并且效果不仅适用于图像,它适用于各种视图

It not overrides the onDraw method, and the effect not only with images, it works with every kind of view

mPager.setPageTransformer(false, new ParallaxTransformer(R.id.parallaxContent));

R.id.paraallaxContent 是你想要这个效果的View的id

R.id.paraallaxContent is the id of the View you want to have this effect

除非其他解决方案,不需要任何具体结构即可工作,并且独立于布局

unless the other solutions, don't need any any concrete structure to work, and also is layout independant

演示:youtube

这篇关于Android视差效果和视图寻呼机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Android视差效果和视图寻呼机

基础教程推荐