Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Viewpager de imagenes en Andorid
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:background="#000"
android:fitsSystemWindows="true"
tools:context="com.webserveis.app.testtransitions.PhotoViewActivity">
<com.webserveis.app.testtransitions.ViewPagerFixed
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#88676767"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_photo"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.webserveis.app.testtransitions.PhotoImageView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/iv_photo"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:AutoRotateCategory="2" />
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
package com.webserveis.app.testtransitions;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import com.allenxuan.xuanyihuang.xuanimageview.XuanImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import java.util.concurrent.TimeUnit;
public class PhotoViewActivity extends AppCompatActivity {
private static final String TAG = PhotoViewActivity.class.getSimpleName();
private ActionBar actionBar;
private MyCountDownTimer countDownTimer;
private GestureDetectorCompat mDetector;
private String[] images = new String[]{
"http://blog.lenycom.com/wp-content/uploads/2009/07/wallpaper-1080p-1.jpg",
"http://blog.lenycom.com/wp-content/uploads/2009/07/wallpaper-1080p-2.jpg",
"http://blog.lenycom.com/wp-content/uploads/2009/07/wallpaper-1080p-3.jpg",
"http://blog.lenycom.com/wp-content/uploads/2009/07/wallpaper-1080p-4.jpg",
"http://blog.lenycom.com/wp-content/uploads/2009/07/wallpaper-1080p-5.jpg",
"http://blog.lenycom.com/wp-content/uploads/2009/07/wallpaper-1080p-6.jpg",
"http://blog.lenycom.com/wp-content/uploads/2009/07/wallpaper-1080p-7.jpg",
"http://blog.lenycom.com/wp-content/uploads/2009/07/wallpaper-1080p-8.jpg",
"http://blog.lenycom.com/wp-content/uploads/2009/07/wallpaper-1080p-9.jpg",
"http://blog.lenycom.com/wp-content/uploads/2009/07/wallpaper-1080p-10.jpg",
"http://blog.lenycom.com/wp-content/uploads/2009/07/wallpaper-1080p-11.jpg",
"http://blog.lenycom.com/wp-content/uploads/2009/07/wallpaper-1080p-12.jpg",
"http://blog.lenycom.com/wp-content/uploads/2009/07/wallpaper-1080p-13.jpg",
"http://blog.lenycom.com/wp-content/uploads/2009/07/wallpaper-1080p-14.jpg",
"http://blog.lenycom.com/wp-content/uploads/2009/07/wallpaper-1080p-15.jpg",
"http://blog.lenycom.com/wp-content/uploads/2009/07/wallpaper-1080p-16.jpg",
"http://blog.lenycom.com/wp-content/uploads/2009/07/wallpaper-1080p-17.jpg",
"http://blog.lenycom.com/wp-content/uploads/2009/07/wallpaper-1080p-18.jpg",
"http://blog.lenycom.com/wp-content/uploads/2009/07/wallpaper-1080p-19.jpg",
"http://blog.lenycom.com/wp-content/uploads/2009/07/wallpaper-1080p-20.jpg",
"http://blog.lenycom.com/wp-content/uploads/2009/07/wallpaper-1080p-21.jpg",
"http://blog.lenycom.com/wp-content/uploads/2009/07/wallpaper-1080p-22.jpg",
"http://blog.lenycom.com/wp-content/uploads/2009/07/wallpaper-1080p-23.jpg",
"http://blog.lenycom.com/wp-content/uploads/2009/07/wallpaper-1080p-24.jpg",
"http://blog.lenycom.com/wp-content/uploads/2009/07/wallpaper-1080p-25.jpg",
"http://blog.lenycom.com/wp-content/uploads/2009/07/wallpaper-1080p-26.jpg",
"http://blog.lenycom.com/wp-content/uploads/2009/07/wallpaper-1080p-27.jpg",
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "onCreate: ");
setContentView(R.layout.activity_photo_view);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
/* getWindow().setFlags(
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);*/
actionBar = getSupportActionBar();
toggleHideyBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
mDetector = new GestureDetectorCompat(this, new MyGestureListener());
countDownTimer = new MyCountDownTimer(TimeUnit.SECONDS.toMillis(8), TimeUnit.SECONDS.toMillis(1));
ViewPagerFixed mViewPager = (ViewPagerFixed) findViewById(R.id.view_pager);
mViewPager.setPageTransformer(true, new ZoomOutPageTransformer());
mViewPager.setOffscreenPageLimit(1);
//MyPagerAdapter adapterViewPager = new MyPagerAdapter(getSupportFragmentManager(), images);
FullScreenImageGalleryAdapter adapterViewPager2 = new FullScreenImageGalleryAdapter(images);
mViewPager.setAdapter(adapterViewPager2);
// mViewPager.setAdapter(new PagerAdapter() {}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
protected void onStart() {
super.onStart();
countDownTimer.start();
}
@Override
protected void onStop() {
countDownTimer.cancel();
super.onStop();
}
@Override
protected void onDestroy() {
countDownTimer.cancel();
super.onDestroy();
}
private class MyCountDownTimer extends CountDownTimer {
MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
@Override
public void onFinish() {
//DO WHATEVER YOU WANT HERE
Log.d(TAG, "MyCountDownTimer onFinish: ");
if (getSupportActionBar() != null) {
if (getSupportActionBar().isShowing()) {
getSupportActionBar().hide();
Log.i(TAG, "Hide ActionBar: ");
}
}
}
@Override
public void onTick(long millisUntilFinished) {
}
}
public void toggleHideyBar() {
int uiOptions = getWindow().getDecorView().getSystemUiVisibility();
int newUiOptions = uiOptions;
boolean isImmersiveModeEnabled =
((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == uiOptions);
if (isImmersiveModeEnabled) {
Log.i(TAG, "Turning immersive mode mode off. ");
} else {
Log.i(TAG, "Turning immersive mode mode on.");
}
// Navigation bar hiding: Backwards compatible to ICS.
if (Build.VERSION.SDK_INT >= 14) {
newUiOptions ^= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
}
// Status bar hiding: Backwards compatible to Jellybean
if (Build.VERSION.SDK_INT >= 16) {
newUiOptions ^= View.SYSTEM_UI_FLAG_FULLSCREEN;
}
if (Build.VERSION.SDK_INT >= 18) {
newUiOptions ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
}
getWindow().getDecorView().setSystemUiVisibility(newUiOptions);
//END_INCLUDE (set_ui_flags)
}
/*
java.lang.IllegalArgumentException: pointerIndex out of range
https://github.com/chrisbanes/PhotoView/issues/31
*/
private class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
private static final String DEBUG_TAG = "Gestures";
@Override
public boolean onSingleTapConfirmed(MotionEvent event) {
Log.i(TAG, "onSingleTap: " + event.toString());
if (actionBar.isShowing()) {
if (actionBar.isShowing()) actionBar.hide();
countDownTimer.cancel();
} else {
actionBar.show();
countDownTimer.cancel();
countDownTimer.start();
}
return true;
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
super.dispatchTouchEvent(ev);
return mDetector.onTouchEvent(ev);
}
private class ZoomOutPageTransformer implements ViewPager.PageTransformer {
private static final float MIN_SCALE = 0.85f;
private static final float MIN_ALPHA = 0.5f;
public void transformPage(View view, float position) {
int pageWidth = view.getWidth();
int pageHeight = view.getHeight();
if (position < -1) { // [-Infinity,-1)
// This page is way off-screen to the left.
view.setAlpha(0);
} else if (position <= 1) { // [-1,1]
// Modify the default slide transition to shrink the page as well
float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
float vertMargin = pageHeight * (1 - scaleFactor) / 2;
float horzMargin = pageWidth * (1 - scaleFactor) / 2;
if (position < 0) {
view.setTranslationX(horzMargin - vertMargin / 2);
} else {
view.setTranslationX(-horzMargin + vertMargin / 2);
}
// Scale the page down (between MIN_SCALE and 1)
view.setScaleX(scaleFactor);
view.setScaleY(scaleFactor);
// Fade the page relative to its size.
view.setAlpha(MIN_ALPHA +
(scaleFactor - MIN_SCALE) /
(1 - MIN_SCALE) * (1 - MIN_ALPHA));
} else { // (1,+Infinity]
// This page is way off-screen to the right.
view.setAlpha(0);
}
}
}
public static class FullScreenImageGalleryAdapter extends PagerAdapter {
private final String[] images;
public FullScreenImageGalleryAdapter(String[] images) {
this.images = images;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Log.d(TAG, "instantiateItem: ");
LayoutInflater inflater = (LayoutInflater) container.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.draw_item_photoview, container, false);
final ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
XuanImageView xuanImageView = (XuanImageView) view.findViewById(R.id.iv_photo);
progressBar.setVisibility(View.VISIBLE);
String image = images[position];
Context context = xuanImageView.getContext();
Glide.with(context)
.load(image)
.listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
progressBar.setVisibility(View.GONE);
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
progressBar.setVisibility(View.GONE);
return false;
}
})
.thumbnail(0.1f)
.diskCacheStrategy(DiskCacheStrategy.RESULT)
.crossFade(1000)
.into(xuanImageView);
container.addView(view, 0);
return view;
}
@Override
public int getCount() {
return images.length;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
Log.i(TAG, "destroyItem: ");
container.removeView((View) object);
unbindDrawables((View) object);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
private void unbindDrawables(View view) {
Log.d(TAG, "unbindDrawables: ");
if (view.getBackground() != null) {
view.getBackground().setCallback(null);
}
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
unbindDrawables(((ViewGroup) view).getChildAt(i));
Log.d(TAG, "unbindDrawables: child" + i);
}
((ViewGroup) view).removeAllViews();
}
}
}
}
<style name="AppTheme.DisplayImage" parent="Theme.AppCompat">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
package com.webserveis.app.testtransitions;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
public class ViewPagerFixed extends android.support.v4.view.ViewPager {
public ViewPagerFixed(Context context) {
super(context);
}
public ViewPagerFixed(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
try {
return super.onTouchEvent(ev);
} catch (IllegalArgumentException ex) {
ex.printStackTrace();
}
return false;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
try {
return super.onInterceptTouchEvent(ev);
} catch (IllegalArgumentException ex) {
ex.printStackTrace();
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.