From b92a9d0c58a8f0433fe000a73da72e4571fa55ad Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Tue, 19 Feb 2019 20:08:00 +0400 Subject: [PATCH] Added ability to leaf images in fullscreen mode --- .../FullscreenImagePagerAdapter.java | 98 +++++++++++++++++++ .../additional/ImagePagerAdapter.java | 2 +- .../view/FullScreenImageActivity.java | 65 ++---------- .../res/layout/activity_fullscreenimage.xml | 4 +- 4 files changed, 108 insertions(+), 61 deletions(-) create mode 100644 app/src/main/java/ru/volgorobot/vrcatalog/additional/FullscreenImagePagerAdapter.java diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/additional/FullscreenImagePagerAdapter.java b/app/src/main/java/ru/volgorobot/vrcatalog/additional/FullscreenImagePagerAdapter.java new file mode 100644 index 0000000..b237edf --- /dev/null +++ b/app/src/main/java/ru/volgorobot/vrcatalog/additional/FullscreenImagePagerAdapter.java @@ -0,0 +1,98 @@ +package ru.volgorobot.vrcatalog.additional; + +import android.content.Context; +import android.support.annotation.NonNull; +import android.support.v4.view.PagerAdapter; +import android.support.v7.widget.Toolbar; +import android.view.View; +import android.view.ViewGroup; +import android.view.animation.AccelerateInterpolator; +import android.view.animation.DecelerateInterpolator; +import android.widget.ImageView; + +import com.github.chrisbanes.photoview.OnPhotoTapListener; +import com.github.chrisbanes.photoview.PhotoView; +import com.squareup.picasso.Picasso; + +public class FullscreenImagePagerAdapter extends PagerAdapter { + /** + * This class is designed to control the ViewPager (images of item) in the activity of viewing details of items (DetailActivity). + */ + + private Context context; + private String[] imageUris; + private Toolbar mToolbar; + private boolean mIsFullScreen; + + public FullscreenImagePagerAdapter(Context context, String[] imageUris, Toolbar toolbar, boolean isFullScreen) { + this.context = context; + this.imageUris = imageUris; + this.mToolbar = toolbar; + this.mIsFullScreen = isFullScreen; + } + + @Override + public int getCount() { + return imageUris.length; + } + + @Override + public boolean isViewFromObject(@NonNull View view, @NonNull Object object) { + return view == object; + } + + /** + * This function initializes a new image from a URI. + * @return ImageView + */ + + @NonNull + @Override + public Object instantiateItem(@NonNull ViewGroup container, int position) { + PhotoView photoView = new PhotoView(context); + Picasso.get() + .load(imageUris[position]) + .into(photoView); + photoView.setOnPhotoTapListener(new PhotoTapListener()); + container.addView(photoView); + + return photoView; + } + + @Override + public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) { + container.removeView((View) object); + } + + private class PhotoTapListener implements OnPhotoTapListener, View.OnSystemUiVisibilityChangeListener { + @Override + public void onPhotoTap(ImageView view, float x, float y) { + updateView(); + } + public void updateView() { + mIsFullScreen = !mIsFullScreen; + + if (mIsFullScreen) { + hideSystemUI(); + } else { + showSystemUI(); + } + } + private void hideSystemUI() { + mToolbar.animate().translationY(-mToolbar.getHeight()).setInterpolator(new AccelerateInterpolator(2)).start(); + } + + private void showSystemUI() { + mToolbar.animate().translationY(0).setInterpolator(new DecelerateInterpolator(2)).start(); + } + + @Override + public void onSystemUiVisibilityChange(int visibility) { + if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) { + mIsFullScreen = false; + } + + updateView(); + } + } +} diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/additional/ImagePagerAdapter.java b/app/src/main/java/ru/volgorobot/vrcatalog/additional/ImagePagerAdapter.java index 2f15b0d..befb9fb 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/additional/ImagePagerAdapter.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/additional/ImagePagerAdapter.java @@ -67,7 +67,7 @@ public class ImagePagerAdapter extends PagerAdapter { @Override public void onClick(View view) { Intent intent = new Intent(context, FullScreenImageActivity.class); - intent.putExtra("path", imageUris[position]); + intent.putExtra("uris", imageUris); context.startActivity(intent); } }); diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/view/FullScreenImageActivity.java b/app/src/main/java/ru/volgorobot/vrcatalog/view/FullScreenImageActivity.java index 92eadc6..ff9cacd 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/view/FullScreenImageActivity.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/view/FullScreenImageActivity.java @@ -6,24 +6,20 @@ import android.graphics.drawable.Drawable; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.content.ContextCompat; +import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.MenuItem; import android.view.View; import android.view.Window; import android.view.WindowManager; -import android.view.animation.AccelerateInterpolator; -import android.view.animation.DecelerateInterpolator; -import android.widget.ImageView; - -import com.github.chrisbanes.photoview.OnPhotoTapListener; -import com.github.chrisbanes.photoview.PhotoView; -import com.squareup.picasso.Picasso; import ru.volgorobot.vrcatalog.R; +import ru.volgorobot.vrcatalog.additional.FullscreenImagePagerAdapter; -public class FullScreenImageActivity extends AppCompatActivity implements View.OnSystemUiVisibilityChangeListener { +public class FullScreenImageActivity extends AppCompatActivity { private Toolbar mToolbar; + private ViewPager fullscreenViewPager; private boolean mIsFullScreen; @Override @@ -56,60 +52,14 @@ public class FullScreenImageActivity extends AppCompatActivity implements View.O |View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); - String path = getIntent().getStringExtra("path"); - PhotoView photoView = (PhotoView) findViewById(R.id.photo_view); - - photoView.setOnPhotoTapListener(new OnPhotoTapListener() { - @Override - public void onPhotoTap(ImageView view, float x, float y) { - updateView(); - } - }); - - Picasso.get() - .load(path) - .into(photoView); + fullscreenViewPager = findViewById(R.id.fullscreen_mode_viewpager); + FullscreenImagePagerAdapter adapter = new FullscreenImagePagerAdapter(FullScreenImageActivity.this, getIntent().getExtras().getStringArray("uris"), mToolbar, mIsFullScreen); + fullscreenViewPager.setAdapter(adapter); } - @Override - public void onSystemUiVisibilityChange(int visibility) { - if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) { - mIsFullScreen = false; - } - - updateView(); - } - - public void updateView() { - mIsFullScreen = !mIsFullScreen; - - if (mIsFullScreen) { - hideSystemUI(); - } else { - showSystemUI(); - } - } - - private void hideSystemUI() { - mToolbar.animate().translationY(-mToolbar.getHeight()).setInterpolator(new AccelerateInterpolator(2)).start(); - } - - private void showSystemUI() { - mToolbar.animate().translationY(0).setInterpolator(new DecelerateInterpolator(2)).start(); - } - - public int getStatusBarHeight() { - int result = 0; - int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); - if (resourceId > 0) { - result = getResources().getDimensionPixelSize(resourceId); - } - return result; - } - @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { @@ -117,7 +67,6 @@ public class FullScreenImageActivity extends AppCompatActivity implements View.O finish(); return true; } - return super.onOptionsItemSelected(item); } } diff --git a/app/src/main/res/layout/activity_fullscreenimage.xml b/app/src/main/res/layout/activity_fullscreenimage.xml index 5e2a573..f09d840 100644 --- a/app/src/main/res/layout/activity_fullscreenimage.xml +++ b/app/src/main/res/layout/activity_fullscreenimage.xml @@ -9,8 +9,8 @@ tools:context="ru.volgorobot.vrcatalog.view.FullScreenImageActivity" android:background="@android:color/black"> -