diff options
author | Andrew DiMarco <disharko@google.com> | 2016-02-11 15:32:26 -0500 |
---|---|---|
committer | Andrew DiMarco <disharko@google.com> | 2016-02-12 19:24:08 +0000 |
commit | dcdb96ea1e7be078ec55be804d326ef2176235fa (patch) | |
tree | c9c2b1613a51e59ea759676834e651d7414fa1bd | |
parent | 93412db545f56e4fd0623da35bd0b0f394cc22c8 (diff) | |
download | photoviewer-dcdb96ea1e7be078ec55be804d326ef2176235fa.tar.gz |
Allows photoviewer subclasses to modify layouts more freely.
This change allows subclasses of the photoviewer to specify a new
content view layout file with any name, change the name of its root
view, and choose to not include/use the background and/or temporary
image views.
Change-Id: I1227f504311143d3a3ad37f97df1848e912cd3f5
-rw-r--r-- | src/com/android/ex/photo/PhotoViewController.java | 208 |
1 files changed, 143 insertions, 65 deletions
diff --git a/src/com/android/ex/photo/PhotoViewController.java b/src/com/android/ex/photo/PhotoViewController.java index adca9ed..407da45 100644 --- a/src/com/android/ex/photo/PhotoViewController.java +++ b/src/com/android/ex/photo/PhotoViewController.java @@ -12,6 +12,8 @@ import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.Process; +import android.support.annotation.IdRes; +import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.LoaderManager; @@ -144,10 +146,12 @@ public class PhotoViewController implements protected View mRootView; /** Background image that contains nothing, so it can be alpha faded from * transparent to black without affecting any other views. */ + @Nullable protected View mBackground; /** The main pager; provides left/right swipe between photos */ protected PhotoViewPager mViewPager; /** The temporary image so that we can quickly scale up the fullscreen thumbnail */ + @Nullable protected ImageView mTemporaryImage; /** Adapter to create pager views */ protected PhotoPagerAdapter mAdapter; @@ -300,18 +304,18 @@ public class PhotoViewController implements mFullScreen = mActionBarHiddenInitially; } - mActivity.setContentView(R.layout.photo_activity_view); + mActivity.setContentView(getContentViewId()); // Create the adapter and add the view pager mAdapter = createPhotoPagerAdapter(mActivity.getContext(), mActivity.getSupportFragmentManager(), null, mMaxInitialScale); final Resources resources = mActivity.getResources(); - mRootView = findViewById(R.id.photo_activity_root_view); + mRootView = findViewById(getRootViewId()); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { mRootView.setOnSystemUiVisibilityChangeListener(getSystemUiVisibilityChangeListener()); } - mBackground = findViewById(R.id.photo_activity_background); - mTemporaryImage = (ImageView) findViewById(R.id.photo_activity_temporary_image); + mBackground = getBackground(); + mTemporaryImage = getTemporaryImage(); mViewPager = (PhotoViewPager) findViewById(R.id.photo_view_pager); mViewPager.setAdapter(mAdapter); mViewPager.setOnPageChangeListener(this); @@ -325,7 +329,9 @@ public class PhotoViewController implements mActivity.getSupportLoaderManager().initLoader(LOADER_PHOTO_LIST, null, this); // Make the background opaque immediately so that we don't see the activity // behind this one. - mBackground.setVisibility(View.VISIBLE); + if (hasBackground()) { + mBackground.setVisibility(View.VISIBLE); + } } else { // Attempt to load the initial image thumbnail. Once we have the // image, animate it up. Once the animation is complete, we can kick off @@ -397,6 +403,58 @@ public class PhotoViewController implements return mActivity.findViewById(id); } + /** + * Returns the android id of the viewer's root view. Subclasses should override this method if + * they provide their own layout. + */ + @IdRes + protected int getRootViewId() { + return R.id.photo_activity_root_view; + } + + /** + * Returns the android layout id of the root layout that should be inflated for the viewer. + * Subclasses should override this method if they provide their own layout. + */ + @IdRes + protected int getContentViewId() { + return R.layout.photo_activity_view; + } + + /** + * Returns the android view for the viewer's background view, if it has one. Subclasses should + * override this if they have a different (or no) background view. + */ + @Nullable + protected View getBackground() { + return findViewById(R.id.photo_activity_background); + } + + /** + * Returns whether or not the view has a background object. Subclasses should override this if + * they do not contain a background object. + */ + protected boolean hasBackground() { + return mBackground != null; + } + + /** + * Returns the android view for the viewer's temporary image, if it has one. Subclasses should + * override this if they have a different (or no) temporary image view. + */ + @Nullable + protected ImageView getTemporaryImage() { + return (ImageView) findViewById(R.id.photo_activity_temporary_image); + } + + /** + * Returns whether or not the view has a temporary image view. Subclasses should override this + * if they do not use a temporary image. + */ + protected boolean hasTemporaryImage() { + return mTemporaryImage != null; + } + public void onStart() {} public void onResume() { @@ -877,11 +935,13 @@ public class PhotoViewController implements @Override public void onFragmentPhotoLoadComplete(PhotoViewFragment fragment, boolean success) { - if (mTemporaryImage.getVisibility() != View.GONE && + if (hasTemporaryImage() && mTemporaryImage.getVisibility() != View.GONE && TextUtils.equals(fragment.getPhotoUri(), mCurrentPhotoUri)) { if (success) { // The fragment for the current image is now ready for display. - mTemporaryImage.setVisibility(View.GONE); + if (hasTemporaryImage()) { + mTemporaryImage.setVisibility(View.GONE); + } mViewPager.setVisibility(View.VISIBLE); } else { // This means that we are unable to load the fragment's photo. @@ -889,7 +949,9 @@ public class PhotoViewController implements // we display the viewPager, the fragment itself can decide how to // display the failure of its own image. Log.w(TAG, "Failed to load fragment image"); - mTemporaryImage.setVisibility(View.GONE); + if (hasTemporaryImage()) { + mTemporaryImage.setVisibility(View.GONE); + } mViewPager.setVisibility(View.VISIBLE); } mActivity.getSupportLoaderManager().destroyLoader( @@ -929,7 +991,9 @@ public class PhotoViewController implements // FLAG: Need to handle the aspect ratio of the bitmap. If it's a portrait // bitmap, then we need to position the view higher so that the middle // pixels line up. - mTemporaryImage.setVisibility(View.VISIBLE); + if (hasTemporaryImage()) { + mTemporaryImage.setVisibility(View.VISIBLE); + } // We need to take a full screen image, and scale/translate it so that // it appears at exactly the same location onscreen as it is in the // prior activity. @@ -946,60 +1010,68 @@ public class PhotoViewController implements final int version = android.os.Build.VERSION.SDK_INT; if (version >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { - mBackground.setAlpha(0f); - mBackground.animate().alpha(1f).setDuration(ENTER_ANIMATION_DURATION_MS).start(); - mBackground.setVisibility(View.VISIBLE); + if (hasBackground()) { + mBackground.setAlpha(0f); + mBackground.animate().alpha(1f).setDuration(ENTER_ANIMATION_DURATION_MS).start(); + mBackground.setVisibility(View.VISIBLE); + } - mTemporaryImage.setScaleX(scale); - mTemporaryImage.setScaleY(scale); - mTemporaryImage.setTranslationX(translateX); - mTemporaryImage.setTranslationY(translateY); + if (hasTemporaryImage()) { + mTemporaryImage.setScaleX(scale); + mTemporaryImage.setScaleY(scale); + mTemporaryImage.setTranslationX(translateX); + mTemporaryImage.setTranslationY(translateY); - Runnable endRunnable = new Runnable() { - @Override - public void run() { - PhotoViewController.this.onEnterAnimationComplete(); + Runnable endRunnable = new Runnable() { + @Override + public void run() { + PhotoViewController.this.onEnterAnimationComplete(); + } + }; + ViewPropertyAnimator animator = mTemporaryImage.animate().scaleX(1f).scaleY(1f) + .translationX(0).translationY(0).setDuration(ENTER_ANIMATION_DURATION_MS); + if (version >= Build.VERSION_CODES.JELLY_BEAN) { + animator.withEndAction(endRunnable); + } else { + mHandler.postDelayed(endRunnable, ENTER_ANIMATION_DURATION_MS); } - }; - ViewPropertyAnimator animator = mTemporaryImage.animate().scaleX(1f).scaleY(1f) - .translationX(0).translationY(0).setDuration(ENTER_ANIMATION_DURATION_MS); - if (version >= Build.VERSION_CODES.JELLY_BEAN) { - animator.withEndAction(endRunnable); - } else { - mHandler.postDelayed(endRunnable, ENTER_ANIMATION_DURATION_MS); + animator.start(); } - animator.start(); } else { - final Animation alphaAnimation = new AlphaAnimation(0f, 1f); - alphaAnimation.setDuration(ENTER_ANIMATION_DURATION_MS); - mBackground.startAnimation(alphaAnimation); - mBackground.setVisibility(View.VISIBLE); - - final Animation translateAnimation = new TranslateAnimation(translateX, - translateY, 0, 0); - translateAnimation.setDuration(ENTER_ANIMATION_DURATION_MS); - Animation scaleAnimation = new ScaleAnimation(scale, scale, 0, 0); - scaleAnimation.setDuration(ENTER_ANIMATION_DURATION_MS); - - AnimationSet animationSet = new AnimationSet(true); - animationSet.addAnimation(translateAnimation); - animationSet.addAnimation(scaleAnimation); - AnimationListener listener = new AnimationListener() { - @Override - public void onAnimationEnd(Animation arg0) { - PhotoViewController.this.onEnterAnimationComplete(); - } + if (hasBackground()) { + final Animation alphaAnimation = new AlphaAnimation(0f, 1f); + alphaAnimation.setDuration(ENTER_ANIMATION_DURATION_MS); + mBackground.startAnimation(alphaAnimation); + mBackground.setVisibility(View.VISIBLE); + } - @Override - public void onAnimationRepeat(Animation arg0) { - } + if (hasTemporaryImage()) { + final Animation translateAnimation = new TranslateAnimation(translateX, + translateY, 0, 0); + translateAnimation.setDuration(ENTER_ANIMATION_DURATION_MS); + Animation scaleAnimation = new ScaleAnimation(scale, scale, 0, 0); + scaleAnimation.setDuration(ENTER_ANIMATION_DURATION_MS); + + AnimationSet animationSet = new AnimationSet(true); + animationSet.addAnimation(translateAnimation); + animationSet.addAnimation(scaleAnimation); + AnimationListener listener = new AnimationListener() { + @Override + public void onAnimationEnd(Animation arg0) { + PhotoViewController.this.onEnterAnimationComplete(); + } - @Override - public void onAnimationStart(Animation arg0) { - } - }; - animationSet.setAnimationListener(listener); - mTemporaryImage.startAnimation(animationSet); + @Override + public void onAnimationRepeat(Animation arg0) { + } + + @Override + public void onAnimationStart(Animation arg0) { + } + }; + animationSet.setAnimationListener(listener); + mTemporaryImage.startAnimation(animationSet); + } } } @@ -1027,8 +1099,10 @@ public class PhotoViewController implements totalHeight, scale); final int version = android.os.Build.VERSION.SDK_INT; if (version >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { - mBackground.animate().alpha(0f).setDuration(EXIT_ANIMATION_DURATION_MS).start(); - mBackground.setVisibility(View.VISIBLE); + if (hasBackground()) { + mBackground.animate().alpha(0f).setDuration(EXIT_ANIMATION_DURATION_MS).start(); + mBackground.setVisibility(View.VISIBLE); + } Runnable endRunnable = new Runnable() { @Override @@ -1040,7 +1114,7 @@ public class PhotoViewController implements // not yet loaded the fullres image, so we need to animate // the temporary image out. ViewPropertyAnimator animator = null; - if (mTemporaryImage.getVisibility() == View.VISIBLE) { + if (hasTemporaryImage() && mTemporaryImage.getVisibility() == View.VISIBLE) { animator = mTemporaryImage.animate().scaleX(scale).scaleY(scale) .translationX(translateX).translationY(translateY) .setDuration(EXIT_ANIMATION_DURATION_MS); @@ -1061,10 +1135,12 @@ public class PhotoViewController implements } animator.start(); } else { - final Animation alphaAnimation = new AlphaAnimation(1f, 0f); - alphaAnimation.setDuration(EXIT_ANIMATION_DURATION_MS); - mBackground.startAnimation(alphaAnimation); - mBackground.setVisibility(View.VISIBLE); + if (hasBackground()) { + final Animation alphaAnimation = new AlphaAnimation(1f, 0f); + alphaAnimation.setDuration(EXIT_ANIMATION_DURATION_MS); + mBackground.startAnimation(alphaAnimation); + mBackground.setVisibility(View.VISIBLE); + } final Animation scaleAnimation = new ScaleAnimation(1f, 1f, scale, scale); scaleAnimation.setDuration(EXIT_ANIMATION_DURATION_MS); @@ -1086,7 +1162,7 @@ public class PhotoViewController implements // If the temporary image is still visible it means that we have // not yet loaded the fullres image, so we need to animate // the temporary image out. - if (mTemporaryImage.getVisibility() == View.VISIBLE) { + if (hasTemporaryImage() && mTemporaryImage.getVisibility() == View.VISIBLE) { mTemporaryImage.startAnimation(scaleAnimation); } else { mViewPager.startAnimation(scaleAnimation); @@ -1124,7 +1200,9 @@ public class PhotoViewController implements // Forget this, we've already run the animation. return; } - mTemporaryImage.setImageDrawable(drawable); + if (hasTemporaryImage()) { + mTemporaryImage.setImageDrawable(drawable); + } if (drawable != null) { // We have not yet run the enter animation. Start it now. int totalWidth = mRootView.getMeasuredWidth(); |