Android Parallax Effect and View Pager(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视差效果和视图寻呼机
基础教程推荐
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01