Advertisement
Guest User

ViewPagerParallax.java

a guest
Feb 7th, 2013
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.23 KB | None | 0 0
  1. package com.matthieu;
  2.  
  3. import android.annotation.SuppressLint;
  4. import android.content.Context;
  5. import android.graphics.*;
  6. import android.support.v4.view.ViewPager;
  7. import android.util.AttributeSet;
  8. import android.util.Log;
  9.  
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12.  
  13. public class ViewPagerParallax extends ViewPager {
  14.     int background_id =-1;
  15.     int background_saved_id =-1;
  16.     int saved_width=-1, saved_height=-1, saved_max_num_pages =-1;
  17.     Bitmap saved_bitmap;
  18.  
  19.     int max_num_pages=0;
  20.     int imageHeight;
  21.     int imageWidth;
  22.     float zoom_level;
  23.    
  24.     float overlap_level;
  25.  
  26.     Rect r = new Rect();
  27.  
  28.     private final static String TAG="ViewPagerParallax";
  29.  
  30.     public ViewPagerParallax(Context context) {
  31.         super(context);
  32.     }
  33.  
  34.     public ViewPagerParallax(Context context, AttributeSet attrs) {
  35.         super(context, attrs);
  36.     }
  37.  
  38.     @SuppressLint({"NewApi"})
  39.     private void set_new_background() {
  40.         if (background_id == -1)
  41.             return;
  42.  
  43.         if (max_num_pages == 0)
  44.             return;
  45.  
  46.         if (getWidth()==0 || getHeight()==0)
  47.             return;
  48.  
  49.         if ((saved_height == getHeight()) && (saved_width == getWidth()) &&
  50.                 (background_saved_id==background_id) &&
  51.                 (saved_max_num_pages == max_num_pages))
  52.             return;
  53.  
  54.         InputStream is;
  55.  
  56.         try {
  57.             is = getContext().getResources().openRawResource(background_id);
  58.  
  59.             BitmapFactory.Options options = new BitmapFactory.Options();
  60.             options.inJustDecodeBounds = true;
  61.             BitmapFactory.decodeStream(is, null, options);
  62.  
  63.             imageHeight = options.outHeight;
  64.             imageWidth = options.outWidth;
  65.  
  66.             zoom_level = ((float) imageHeight) / getHeight();  // we are always in 'fitY' mode
  67.             Log.e(TAG, "" + zoom_level);
  68.             is.reset();
  69.  
  70.             overlap_level = (imageWidth / zoom_level - getWidth() * max_num_pages) / (max_num_pages - 1);
  71.             //if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD_MR1) {
  72.             //    BitmapRegionDecoder brd = BitmapRegionDecoder.newInstance(is, true);
  73.  
  74.             //    options.inJustDecodeBounds = false;
  75.             //    r.set(0, 0, Math.min((int) (getWidth() * ((max_num_pages + 4.0) / 5) * zoom_level), imageWidth), imageHeight);
  76.             //    saved_bitmap = brd.decodeRegion(r, options);
  77.             //    brd.recycle();
  78.             //} else {
  79.             //    saved_bitmap = Bitmap.createBitmap(BitmapFactory.decodeStream(is), 0, 0, Math.min((int) (getWidth() * ((max_num_pages + 4.0) / 5) * zoom_level), imageWidth), imageHeight);
  80.             //}
  81.             saved_bitmap = BitmapFactory.decodeStream(is);
  82.  
  83.             is.close();
  84.         } catch (IOException e) {
  85.             Log.e(TAG, "Cannot decode: " + e.getMessage());
  86.             background_id = -1;
  87.             return;
  88.         }
  89.  
  90.         saved_height = getHeight();
  91.         saved_width = getWidth();
  92.         background_saved_id = background_id;
  93.         saved_max_num_pages = max_num_pages;
  94.     }
  95.  
  96.     int current_position, first_position=-1;
  97.     float current_offset;
  98.  
  99.     @Override
  100.     protected void onPageScrolled(int position, float offset, int offsetPixels) {
  101.         super.onPageScrolled(position, offset, offsetPixels);
  102.         current_position = position;
  103.         current_offset = offset;
  104.     }
  105.  
  106.     Rect src = new Rect(), dst = new Rect();
  107.  
  108.     @Override
  109.     protected void onDraw(Canvas canvas) {
  110.         super.onDraw(canvas);
  111.  
  112.         if (first_position == -1) {
  113.             first_position = getCurrentItem();
  114.             current_position = first_position;
  115.             current_offset = 0.0f;
  116.         }
  117.        
  118.         //src.set((int) (((current_position + current_offset) * getWidth() * zoom_level) / 5 ), 0,
  119.         //        (int) ((((current_position + current_offset) * getWidth() * zoom_level) / 5)  + (getWidth() * zoom_level)), imageHeight);
  120.  
  121.         //dst.set((int) ((current_position - first_position + current_offset) * getWidth()), 0,
  122.         //        (int) ((current_position - first_position + current_offset) * getWidth()) + canvas.getWidth(), canvas.getHeight());
  123.         // still confused why we need to shift also in the destination canvas
  124.  
  125.         src.set((int) (((current_position + current_offset) * getWidth() + overlap_level * (current_position + current_offset)) * zoom_level), 0,
  126.             (int) (((current_position + current_offset + 1) * getWidth() + overlap_level * current_position) * zoom_level), imageHeight);
  127.    
  128.         dst.set((int) ((current_position + current_offset) * canvas.getWidth()), 0,
  129.             (int) ((current_position + current_offset + 1) * canvas.getWidth() - overlap_level * current_offset), canvas.getHeight());
  130.        
  131.         canvas.drawBitmap(saved_bitmap, src, dst, null);
  132.     }
  133.  
  134.     public void set_max_pages(int num_max_pages) {
  135.         max_num_pages = num_max_pages;
  136.         set_new_background();
  137.     }
  138.  
  139.     public void setBackgroundAsset(int res_id) {
  140.         background_id = res_id;
  141.         set_new_background();
  142.     }
  143.  
  144.     @Override
  145.     protected void onLayout(boolean changed, int l, int t, int r, int b) {
  146.         super.onLayout(changed, l, t, r, b);
  147.         set_new_background();
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement