【问题标题】:ParallaxViewPager Right to Left视差 ViewPager 从右到左
【发布时间】:2014-06-02 21:20:27
【问题描述】:

我正在 ParallaxViewPager 上开展一个项目。我有 3 页。一切正常,背景是应有的视差。但是当我添加 parallaxViewPager.setCurrentItem(2);背景没有出现,它得到默认的android背景。完整代码如下:

主活动:

package com.example.myapplication3.app;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.RelativeLayout;

import com.andraskindler.parallaxviewpager.ParallaxViewPager;

import java.io.FileNotFoundException;
import java.io.InputStream;


public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ParallaxViewPager parallaxViewPager = ((ParallaxViewPager) findViewById(R.id.parallaxviewpager));
        parallaxViewPager.setOverlapPercentage(0.25f);
        parallaxViewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
        parallaxViewPager.setCurrentItem(2);

        }
    }

PagerAdapter:

package com.example.myapplication3.app;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;




public class MyPagerAdapter extends FragmentPagerAdapter {

    private List<Fragment> fragments;

    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
        this.fragments = new ArrayList<Fragment>();

        fragments.add(new view1());
        fragments.add(new view2());
        fragments.add(new view3());
    }



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

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

查看1、2、3:

package com.example.myapplication3.app;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class view1 extends Fragment { //view2, view3 on the others

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.view1, container, false); //same here view 2,3

        return rootView;
    }
}

布局如下:

MainActivity 的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:ignore="MergeRootFrame" >

    <com.andraskindler.parallaxviewpager.ParallaxViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/parallaxviewpager"
        android:layout_width="match_parent"
        android:background="@drawable/sanfran"
        android:layout_height="match_parent"/>

    </RelativeLayout>

查看 1,2,3' 布局:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

</LinearLayout>

提前感谢您的时间和帮助。

【问题讨论】:

    标签: android android-viewpager parallax right-to-left wallpaper


    【解决方案1】:

    我对这个组件有同样的问题。这有点像画布和目标矩形之间的冲突,所以任何东西都被绘制在应该完成的地方。

    我创建了一个原始源存储库的分支来更新它。调用 setCurrentItem 时,我会检查位置并存储稍后在 onDraw 方法中使用的调整值。

    您可以在https://github.com/yuraksisa/parallaxviewpager查看它

    【讨论】:

      【解决方案2】:

      这里是 ParallaxViewPager 的修改:

      import android.annotation.SuppressLint;
      import android.content.Context;
      import android.graphics.*;
      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;
          public Bitmap bitmap;
          private Rect source, destination;
          private int scaleType;
          private Paint paint;
          private OnPageChangeListener secondOnPageChangeListener;
      
          public ParallaxViewPager(Context context) {
              super(context);
              init();
          }
      
          public ParallaxViewPager(Context context, AttributeSet attrs) {
              super(context, attrs);
              init();
          }
      
          private void init() {
              paint = new Paint(Paint.ANTI_ALIAS_FLAG);
              source = new Rect();
              destination = new Rect();
              scaleType = FIT_HEIGHT;
              setOnPageChangeListener(new OnPageChangeListener() {
                  @Override
                  public void onPageScrolled(int position, float positionOffset,
                                             int positionOffsetPixels) {
                      if (bitmap != null) {
                          if (position == 0) {
                              source.left = (int) ((getWidth() / 8) * positionOffset);
                              source.right = getWidth() + source.left;
                              destination.left = (int) ((getWidth() * positionOffset) - getWidth());
                              destination.right = (int) (0 + (getWidth() * positionOffset));
      
                          } else {
                              source.left = (int) ((getWidth() / 8) - (getWidth() / 8)
                                      * positionOffset);
                              source.right = getWidth() + source.left;
                              destination.left = (int) ((0 - getWidth()
                                      * positionOffset));
                              destination.right = (int) (getWidth() - (getWidth() * positionOffset));
                          }
      
                          postInvalidate();
                      }
      
                      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.");
              }
              source.top = 0;
              source.bottom = bitmap.getHeight();
      
          }
      
          /**
           * 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;
          }
      
          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");
              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, paint);
              }
          }
      
          public void addOnPageChangeListener(OnPageChangeListener listener) {
              secondOnPageChangeListener = listener;
          }
      }
      

      这里是如何使用:

      viewPager = (ParallaxViewPager)findViewById(R.id.lock_viewpager);
      viewPager.setBackgroundDrawable(new BitmapDrawable());
      viewPager.setAdapter(new MyViewPagerAdapter());
      viewPager.addOnPageChangeListener(this);
      viewPager.setCurrentItem(mList.size());
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-01-30
        • 1970-01-01
        • 1970-01-01
        • 2019-06-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多