Search in sources :

Example 31 with ImageLoadingListener

use of com.nostra13.universalimageloader.core.listener.ImageLoadingListener in project Slide by ccrama.

the class MediaFragment method displayImage.

public void displayImage(final String urlB) {
    final String url = StringEscapeUtils.unescapeHtml4(urlB);
    if (!imageShown) {
        actuallyLoaded = url;
        final SubsamplingScaleImageView i = rootView.findViewById(R.id.submission_image);
        i.setMinimumDpi(70);
        i.setMinimumTileDpi(240);
        final ProgressBar bar = rootView.findViewById(R.id.progress);
        bar.setIndeterminate(false);
        LogUtil.v("Displaying image " + url);
        bar.setProgress(0);
        final Handler handler = new Handler();
        final Runnable progressBarDelayRunner = new Runnable() {

            public void run() {
                bar.setVisibility(View.VISIBLE);
            }
        };
        handler.postDelayed(progressBarDelayRunner, 500);
        ImageView fakeImage = new ImageView(getActivity());
        fakeImage.setLayoutParams(new LinearLayout.LayoutParams(i.getWidth(), i.getHeight()));
        fakeImage.setScaleType(ImageView.ScaleType.CENTER_CROP);
        File f = ((Reddit) getActivity().getApplicationContext()).getImageLoader().getDiskCache().get(url);
        if (f != null && f.exists()) {
            imageShown = true;
            try {
                i.setImage(ImageSource.uri(f.getAbsolutePath()));
            } catch (Exception e) {
            // todo  i.setImage(ImageSource.bitmap(loadedImage));
            }
            (rootView.findViewById(R.id.progress)).setVisibility(View.GONE);
            handler.removeCallbacks(progressBarDelayRunner);
            previous = i.scale;
            final float base = i.scale;
            i.setOnStateChangedListener(new SubsamplingScaleImageView.DefaultOnStateChangedListener() {

                @Override
                public void onScaleChanged(float newScale, int origin) {
                    if (newScale > previous && !hidden && newScale > base) {
                        hidden = true;
                        final View base = rootView.findViewById(R.id.base);
                        ValueAnimator va = ValueAnimator.ofFloat(1.0f, 0.2f);
                        // in millis
                        int mDuration = 250;
                        va.setDuration(mDuration);
                        va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

                            public void onAnimationUpdate(ValueAnimator animation) {
                                Float value = (Float) animation.getAnimatedValue();
                                base.setAlpha(value);
                            }
                        });
                        va.start();
                    // hide
                    } else if (newScale <= previous && hidden) {
                        hidden = false;
                        final View base = rootView.findViewById(R.id.base);
                        ValueAnimator va = ValueAnimator.ofFloat(0.2f, 1.0f);
                        // in millis
                        int mDuration = 250;
                        va.setDuration(mDuration);
                        va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

                            public void onAnimationUpdate(ValueAnimator animation) {
                                Float value = (Float) animation.getAnimatedValue();
                                base.setAlpha(value);
                            }
                        });
                        va.start();
                    // unhide
                    }
                    previous = newScale;
                }
            });
        } else {
            ((Reddit) getActivity().getApplicationContext()).getImageLoader().displayImage(url, new ImageViewAware(fakeImage), new DisplayImageOptions.Builder().resetViewBeforeLoading(true).cacheOnDisk(true).imageScaleType(ImageScaleType.NONE).cacheInMemory(false).build(), new ImageLoadingListener() {

                @Override
                public void onLoadingStarted(String imageUri, View view) {
                    imageShown = true;
                }

                @Override
                public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                    Log.v(LogUtil.getTag(), "LOADING FAILED");
                }

                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                    imageShown = true;
                    File f = null;
                    if (getActivity() != null) {
                        f = ((Reddit) getActivity().getApplicationContext()).getImageLoader().getDiskCache().get(url);
                    }
                    if (f != null && f.exists()) {
                        i.setImage(ImageSource.uri(f.getAbsolutePath()));
                    } else {
                        i.setImage(ImageSource.bitmap(loadedImage));
                    }
                    (rootView.findViewById(R.id.progress)).setVisibility(View.GONE);
                    handler.removeCallbacks(progressBarDelayRunner);
                    previous = i.scale;
                    final float base = i.scale;
                    i.setOnStateChangedListener(new SubsamplingScaleImageView.DefaultOnStateChangedListener() {

                        @Override
                        public void onScaleChanged(float newScale, int origin) {
                            if (newScale > previous && !hidden && newScale > base) {
                                hidden = true;
                                final View base = rootView.findViewById(R.id.base);
                                ValueAnimator va = ValueAnimator.ofFloat(1.0f, 0.2f);
                                // in millis
                                int mDuration = 250;
                                va.setDuration(mDuration);
                                va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

                                    public void onAnimationUpdate(ValueAnimator animation) {
                                        Float value = (Float) animation.getAnimatedValue();
                                        base.setAlpha(value);
                                    }
                                });
                                va.start();
                            // hide
                            } else if (newScale <= previous && hidden) {
                                hidden = false;
                                final View base = rootView.findViewById(R.id.base);
                                ValueAnimator va = ValueAnimator.ofFloat(0.2f, 1.0f);
                                // in millis
                                int mDuration = 250;
                                va.setDuration(mDuration);
                                va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

                                    public void onAnimationUpdate(ValueAnimator animation) {
                                        Float value = (Float) animation.getAnimatedValue();
                                        base.setAlpha(value);
                                    }
                                });
                                va.start();
                            // unhide
                            }
                            previous = newScale;
                        }
                    });
                }

                @Override
                public void onLoadingCancelled(String imageUri, View view) {
                    Log.v(LogUtil.getTag(), "LOADING CANCELLED");
                }
            }, new ImageLoadingProgressListener() {

                @Override
                public void onProgressUpdate(String imageUri, View view, int current, int total) {
                    ((ProgressBar) rootView.findViewById(R.id.progress)).setProgress(Math.round(100.0f * current / total));
                }
            });
        }
    }
}
Also used : ImageLoadingProgressListener(com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener) FailReason(com.nostra13.universalimageloader.core.assist.FailReason) ValueAnimator(android.animation.ValueAnimator) Bitmap(android.graphics.Bitmap) Reddit(me.ccrama.redditslide.Reddit) ImageView(android.widget.ImageView) SubsamplingScaleImageView(me.ccrama.redditslide.Views.SubsamplingScaleImageView) ProgressBar(android.widget.ProgressBar) ImageViewAware(com.nostra13.universalimageloader.core.imageaware.ImageViewAware) ImageLoadingListener(com.nostra13.universalimageloader.core.listener.ImageLoadingListener) Handler(android.os.Handler) SubsamplingScaleImageView(me.ccrama.redditslide.Views.SubsamplingScaleImageView) MediaView(me.ccrama.redditslide.Activities.MediaView) ImageView(android.widget.ImageView) ExoVideoView(me.ccrama.redditslide.Views.ExoVideoView) View(android.view.View) SubsamplingScaleImageView(me.ccrama.redditslide.Views.SubsamplingScaleImageView) IOException(java.io.IOException) File(java.io.File) LinearLayout(android.widget.LinearLayout)

Example 32 with ImageLoadingListener

use of com.nostra13.universalimageloader.core.listener.ImageLoadingListener in project Slide by ccrama.

the class MediaView method displayImage.

public void displayImage(final String urlB) {
    LogUtil.v("Displaying " + urlB);
    final String url = StringEscapeUtils.unescapeHtml4(urlB);
    if (!imageShown) {
        actuallyLoaded = url;
        final SubsamplingScaleImageView i = (SubsamplingScaleImageView) findViewById(R.id.submission_image);
        i.setMinimumDpi(70);
        i.setMinimumTileDpi(240);
        final ProgressBar bar = (ProgressBar) findViewById(R.id.progress);
        bar.setIndeterminate(false);
        bar.setProgress(0);
        final Handler handler = new Handler();
        final Runnable progressBarDelayRunner = new Runnable() {

            public void run() {
                bar.setVisibility(View.VISIBLE);
            }
        };
        handler.postDelayed(progressBarDelayRunner, 500);
        ImageView fakeImage = new ImageView(MediaView.this);
        fakeImage.setLayoutParams(new LinearLayout.LayoutParams(i.getWidth(), i.getHeight()));
        fakeImage.setScaleType(ImageView.ScaleType.CENTER_CROP);
        File f = ((Reddit) getApplicationContext()).getImageLoader().getDiskCache().get(url);
        if (f != null && f.exists()) {
            imageShown = true;
            i.setOnImageEventListener(new SubsamplingScaleImageView.DefaultOnImageEventListener() {

                @Override
                public void onImageLoadError(Exception e) {
                    imageShown = false;
                    LogUtil.v("No image displayed");
                }
            });
            try {
                i.setImage(ImageSource.uri(f.getAbsolutePath()));
            } catch (Exception e) {
                imageShown = false;
            // todo  i.setImage(ImageSource.bitmap(loadedImage));
            }
            (findViewById(R.id.progress)).setVisibility(View.GONE);
            handler.removeCallbacks(progressBarDelayRunner);
            previous = i.scale;
            final float base = i.scale;
            i.postDelayed(new Runnable() {

                @Override
                public void run() {
                    i.setOnStateChangedListener(new SubsamplingScaleImageView.DefaultOnStateChangedListener() {

                        @Override
                        public void onScaleChanged(float newScale, int origin) {
                            if (newScale > previous && !hidden && newScale > base) {
                                hidden = true;
                                final View base = findViewById(R.id.gifheader);
                                ValueAnimator va = ValueAnimator.ofFloat(1.0f, 0.2f);
                                // in millis
                                int mDuration = 250;
                                va.setDuration(mDuration);
                                va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

                                    public void onAnimationUpdate(ValueAnimator animation) {
                                        Float value = (Float) animation.getAnimatedValue();
                                        base.setAlpha(value);
                                    }
                                });
                                va.start();
                            // hide
                            } else if (newScale <= previous && hidden) {
                                hidden = false;
                                final View base = findViewById(R.id.gifheader);
                                ValueAnimator va = ValueAnimator.ofFloat(0.2f, 1.0f);
                                // in millis
                                int mDuration = 250;
                                va.setDuration(mDuration);
                                va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

                                    public void onAnimationUpdate(ValueAnimator animation) {
                                        Float value = (Float) animation.getAnimatedValue();
                                        base.setAlpha(value);
                                    }
                                });
                                va.start();
                            // unhide
                            }
                            previous = newScale;
                        }
                    });
                }
            }, 2000);
        } else {
            final TextView size = (TextView) findViewById(R.id.size);
            ((Reddit) getApplication()).getImageLoader().displayImage(url, new ImageViewAware(fakeImage), new DisplayImageOptions.Builder().resetViewBeforeLoading(true).cacheOnDisk(true).imageScaleType(ImageScaleType.NONE).cacheInMemory(false).build(), new ImageLoadingListener() {

                @Override
                public void onLoadingStarted(String imageUri, View view) {
                    imageShown = true;
                    size.setVisibility(View.VISIBLE);
                }

                @Override
                public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                    Log.v(LogUtil.getTag(), "LOADING FAILED");
                    imageShown = false;
                }

                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                    imageShown = true;
                    size.setVisibility(View.GONE);
                    File f = ((Reddit) getApplicationContext()).getImageLoader().getDiskCache().get(url);
                    if (f != null && f.exists()) {
                        i.setImage(ImageSource.uri(f.getAbsolutePath()));
                    } else {
                        i.setImage(ImageSource.bitmap(loadedImage));
                    }
                    (findViewById(R.id.progress)).setVisibility(View.GONE);
                    handler.removeCallbacks(progressBarDelayRunner);
                    previous = i.scale;
                    final float base = i.scale;
                    i.setOnStateChangedListener(new SubsamplingScaleImageView.DefaultOnStateChangedListener() {

                        @Override
                        public void onScaleChanged(float newScale, int origin) {
                            if (newScale > previous && !hidden && newScale > base) {
                                hidden = true;
                                final View base = findViewById(R.id.gifheader);
                                ValueAnimator va = ValueAnimator.ofFloat(1.0f, 0.2f);
                                // in millis
                                int mDuration = 250;
                                va.setDuration(mDuration);
                                va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

                                    public void onAnimationUpdate(ValueAnimator animation) {
                                        Float value = (Float) animation.getAnimatedValue();
                                        base.setAlpha(value);
                                    }
                                });
                                va.start();
                            // hide
                            } else if (newScale <= previous && hidden) {
                                hidden = false;
                                final View base = findViewById(R.id.gifheader);
                                ValueAnimator va = ValueAnimator.ofFloat(0.2f, 1.0f);
                                // in millis
                                int mDuration = 250;
                                va.setDuration(mDuration);
                                va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

                                    public void onAnimationUpdate(ValueAnimator animation) {
                                        Float value = (Float) animation.getAnimatedValue();
                                        base.setAlpha(value);
                                    }
                                });
                                va.start();
                            // unhide
                            }
                            previous = newScale;
                        }
                    });
                }

                @Override
                public void onLoadingCancelled(String imageUri, View view) {
                    Log.v(LogUtil.getTag(), "LOADING CANCELLED");
                }
            }, new ImageLoadingProgressListener() {

                @Override
                public void onProgressUpdate(String imageUri, View view, int current, int total) {
                    size.setText(FileUtil.readableFileSize(total));
                    ((ProgressBar) findViewById(R.id.progress)).setProgress(Math.round(100.0f * current / total));
                }
            });
        }
    }
}
Also used : ImageLoadingProgressListener(com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener) FailReason(com.nostra13.universalimageloader.core.assist.FailReason) ValueAnimator(android.animation.ValueAnimator) Bitmap(android.graphics.Bitmap) Reddit(me.ccrama.redditslide.Reddit) TextView(android.widget.TextView) ImageView(android.widget.ImageView) SubsamplingScaleImageView(me.ccrama.redditslide.Views.SubsamplingScaleImageView) ProgressBar(android.widget.ProgressBar) ImageViewAware(com.nostra13.universalimageloader.core.imageaware.ImageViewAware) ImageLoadingListener(com.nostra13.universalimageloader.core.listener.ImageLoadingListener) Handler(android.os.Handler) SubsamplingScaleImageView(me.ccrama.redditslide.Views.SubsamplingScaleImageView) ImageView(android.widget.ImageView) SubmissionsView(me.ccrama.redditslide.Fragments.SubmissionsView) ExoVideoView(me.ccrama.redditslide.Views.ExoVideoView) View(android.view.View) TextView(android.widget.TextView) SubsamplingScaleImageView(me.ccrama.redditslide.Views.SubsamplingScaleImageView) URISyntaxException(java.net.URISyntaxException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) File(java.io.File) LinearLayout(android.widget.LinearLayout)

Aggregations

FailReason (com.nostra13.universalimageloader.core.assist.FailReason)30 Bitmap (android.graphics.Bitmap)29 View (android.view.View)29 ImageView (android.widget.ImageView)23 ImageLoadingListener (com.nostra13.universalimageloader.core.listener.ImageLoadingListener)22 TextView (android.widget.TextView)14 ProgressBar (android.widget.ProgressBar)8 File (java.io.File)8 Reddit (me.ccrama.redditslide.Reddit)8 LinearLayout (android.widget.LinearLayout)7 ImageLoadingListener (com.nostra13.universalimageloader.core.assist.ImageLoadingListener)7 ImageViewAware (com.nostra13.universalimageloader.core.imageaware.ImageViewAware)7 ImageLoadingProgressListener (com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener)7 Handler (android.os.Handler)5 OnClickListener (android.view.View.OnClickListener)5 IOException (java.io.IOException)5 SubmissionsView (me.ccrama.redditslide.Fragments.SubmissionsView)5 ExoVideoView (me.ccrama.redditslide.Views.ExoVideoView)5 SubsamplingScaleImageView (me.ccrama.redditslide.Views.SubsamplingScaleImageView)5 LayoutInflater (android.view.LayoutInflater)4