Android Open Source - PhotoView View Pager Activity






From Project

Back to project page PhotoView.

License

The source code is released under:

Apache License

If you think the Android project PhotoView listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*******************************************************************************
 * Copyright 2011, 2012 Chris Banes.//w w  w.ja v a2  s  . c  om
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package uk.co.senab.photoview.sample;

import uk.co.senab.photoview.PhotoView;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;

/**
 * Lock/Unlock button is added to the ActionBar.
 * Use it to temporarily disable ViewPager navigation in order to correctly interact with ImageView by gestures.
 * Lock/Unlock state of ViewPager is saved and restored on configuration changes.
 * 
 * Julia Zudikova
 */

public class ViewPagerActivity extends Activity {

  private static final String ISLOCKED_ARG = "isLocked";
  
  private ViewPager mViewPager;
  private MenuItem menuLockItem;
  
    @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_pager);
        mViewPager = (HackyViewPager) findViewById(R.id.view_pager);
    setContentView(mViewPager);

    mViewPager.setAdapter(new SamplePagerAdapter());
    
    if (savedInstanceState != null) {
      boolean isLocked = savedInstanceState.getBoolean(ISLOCKED_ARG, false);
      ((HackyViewPager) mViewPager).setLocked(isLocked);
    }
  }

  static class SamplePagerAdapter extends PagerAdapter {

    private static final int[] sDrawables = { R.drawable.wallpaper, R.drawable.wallpaper, R.drawable.wallpaper,
        R.drawable.wallpaper, R.drawable.wallpaper, R.drawable.wallpaper };

    @Override
    public int getCount() {
      return sDrawables.length;
    }

    @Override
    public View instantiateItem(ViewGroup container, int position) {
      PhotoView photoView = new PhotoView(container.getContext());
      photoView.setImageResource(sDrawables[position]);

      // Now just add PhotoView to ViewPager and return it
      container.addView(photoView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

      return photoView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
      container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
      return view == object;
    }

  }

  @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.viewpager_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        menuLockItem = menu.findItem(R.id.menu_lock);
        toggleLockBtnTitle();
        menuLockItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
      @Override
      public boolean onMenuItemClick(MenuItem item) {
        toggleViewPagerScrolling();
        toggleLockBtnTitle();
        return true;
      }
    });

        return super.onPrepareOptionsMenu(menu);
    }
    
    private void toggleViewPagerScrolling() {
      if (isViewPagerActive()) {
        ((HackyViewPager) mViewPager).toggleLock();
      }
    }
    
    private void toggleLockBtnTitle() {
      boolean isLocked = false;
      if (isViewPagerActive()) {
        isLocked = ((HackyViewPager) mViewPager).isLocked();
      }
      String title = (isLocked) ? getString(R.string.menu_unlock) : getString(R.string.menu_lock);
      if (menuLockItem != null) {
        menuLockItem.setTitle(title);
      }
    }

    private boolean isViewPagerActive() {
      return (mViewPager != null && mViewPager instanceof HackyViewPager);
    }
    
  @Override
  protected void onSaveInstanceState(Bundle outState) {
    if (isViewPagerActive()) {
      outState.putBoolean(ISLOCKED_ARG, ((HackyViewPager) mViewPager).isLocked());
      }
    super.onSaveInstanceState(outState);
  }
    
}




Java Source Code List

uk.co.senab.photoview.Compat.java
uk.co.senab.photoview.DefaultOnDoubleTapListener.java
uk.co.senab.photoview.IPhotoView.java
uk.co.senab.photoview.PhotoViewAttacher.java
uk.co.senab.photoview.PhotoView.java
uk.co.senab.photoview.gestures.CupcakeGestureDetector.java
uk.co.senab.photoview.gestures.EclairGestureDetector.java
uk.co.senab.photoview.gestures.FroyoGestureDetector.java
uk.co.senab.photoview.gestures.GestureDetector.java
uk.co.senab.photoview.gestures.OnGestureListener.java
uk.co.senab.photoview.gestures.VersionedGestureDetector.java
uk.co.senab.photoview.log.LogManager.java
uk.co.senab.photoview.log.LoggerDefault.java
uk.co.senab.photoview.log.Logger.java
uk.co.senab.photoview.sample.AUILSampleActivity.java
uk.co.senab.photoview.sample.HackyViewPager.java
uk.co.senab.photoview.sample.LauncherActivity.java
uk.co.senab.photoview.sample.RotationSampleActivity.java
uk.co.senab.photoview.sample.SimpleSampleActivity.java
uk.co.senab.photoview.sample.ViewPagerActivity.java
uk.co.senab.photoview.scrollerproxy.GingerScroller.java
uk.co.senab.photoview.scrollerproxy.IcsScroller.java
uk.co.senab.photoview.scrollerproxy.PreGingerScroller.java
uk.co.senab.photoview.scrollerproxy.ScrollerProxy.java