Android Open Source - Dreamer Dream Adapter






From Project

Back to project page Dreamer.

License

The source code is released under:

Apache License

If you think the Android project Dreamer 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

package com.android.fancyblurdemo.app;
/*w  w w.  j  a v  a 2 s .c om*/
import android.content.Context;
import android.graphics.Point;
import android.os.Build;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.android.fancyblurdemo.volley.VolleyError;
import com.android.fancyblurdemo.volley.toolbox.ImageLoader;
import com.android.fancyblurdemo.volley.toolbox.NetworkImageView;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>The adapter used by the {@link android.widget.AdapterViewFlipper}
 * in the {@link FlickrDream}.</p>
 *
 * <p>This adapter is responsible for downloading and displaying the Flickr
 * images asynchronously.</p>
 */
public class DreamAdapter extends PagerAdapter {

    private final Animation mFadeInWithDelay;
    private Context mContext;
    private List<FlickrPhoto> photos = new ArrayList<FlickrPhoto>();
    private ArrayList<View> mViews = new ArrayList<View>();
    private Set<TextSizeTask> mCurrentTasks = new HashSet<TextSizeTask>();
    private int[] textSizes;
    private int screenWidth;
    private int screenHeight;
    private boolean mIsFirstRunThrough = false;

    @SuppressWarnings("deprecation")
    public DreamAdapter(Context context, List<FlickrPhoto> photos) {
        mContext = context;
        this.photos = photos;
        textSizes = new int[photos.size()];
        mFadeInWithDelay = AnimationUtils.loadAnimation(context, R.anim.fade_in_with_delay);

        Display display = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        int titleTextMargin = (int) context.getResources().getDimension(R.dimen.title_text_margin);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
            Point size = new Point();
            display.getSize(size);
            screenWidth = size.x - 2 * titleTextMargin;
            screenHeight = size.y - 2 * titleTextMargin;
        } else {
            screenWidth = display.getWidth() - 2 * titleTextMargin;
            screenHeight = display.getHeight() - 2 * titleTextMargin;
        }
    }

    @Override
    public int getCount() {
        return photos.size();
    }

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

    public FlickrPhoto getItem(int position) {
        return photos.get(position);
    }

    @Override
    public View instantiateItem(ViewGroup container, int position) {
        View v = getView(position, mViews.size() < position ? mViews.get(position) : null, container);
        while (mViews.size() <= position) {
            mViews.add(null);
        }
        mViews.set(position, v);
        ((ViewPager) container).addView(v, 0);
        return v;
    }

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

    public View getView(int position, View convertView, ViewGroup parent) {

        final DreamViewHolder holder;
        if (convertView == null) {
            convertView = ((LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.service_inner_dream, null);

            holder = new DreamViewHolder();
            holder.mImageView = (NetworkImageView) convertView.findViewById(R.id.flickrDreamView);
            holder.mBlurImageView = (BlurImageView) convertView.findViewById(R.id.blurView);
            holder.mProgressBar = (ProgressBar) convertView.findViewById(R.id.progressBar);
            holder.mOverlay = convertView.findViewById(R.id.overlay);

            holder.mTitleText = (RobotoTextView) convertView.findViewById(R.id.titleText);

            convertView.setTag(holder);
        } else {
            holder = (DreamViewHolder) convertView.getTag();
        }

        if (textSizes[position] == 0) {
            TextSizeTask task = new TextSizeTask(holder.mTitleText, screenWidth, screenHeight, position);
            mCurrentTasks.add(task);
            task.execute(getItem(position).title, getItem(position).preferredWidthStr);
        } else {
            holder.mTitleText.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSizes[position]);
        }

        holder.mTitleText.setText(getItem(position).title);
        holder.mTitleText.setVisibility(View.GONE);
        holder.mImageView.setImageListener(new ImageLoader.ImageListener() {
            @Override
            public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
                if (response.getBitmap() != null && !TextUtils.isEmpty(response.getRequestUrl())) {
                    holder.mOverlay.setVisibility(View.VISIBLE);
                    Animation fullFadeIn = AnimationUtils.loadAnimation(mContext, android.R.anim.fade_in);
                    fullFadeIn.setFillAfter(true);
                    holder.mOverlay.startAnimation(fullFadeIn);
//                    mBlurImageView.setImageToBlur(response.getBitmap(), response.getRequestUrl(), BlurManager.getImageBlurrer());
//                    mBlurImageView.setImageAlpha(0);
                    holder.mProgressBar.setVisibility(View.GONE);
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                // Do nothing.
            }
        });

        holder.mImageView.setImageUrl(FlickrDream.sUseHighRes ? getItem(position).highResUrl : getItem(position).photoUrl, VolleyManager.getImageLoader(), true);

        if (mIsFirstRunThrough) {
            final int width = holder.mImageView.getWidth();
            final int height = holder.mImageView.getHeight();
            VolleyManager.getImageLoader().get((FlickrDream.sUseHighRes ? getItem((position + 1) % getCount()).highResUrl : getItem((position + 1) % getCount()).photoUrl), new ImageLoader.ImageListener() {
                @Override
                public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
                    // Do Nothing. We are just caching early.
                }

                @Override
                public void onErrorResponse(VolleyError error) {
                    // Do Nothing. We are just caching early.
                }
            }, width, height);
        }

        if (position == getCount() - 1) {
            mIsFirstRunThrough = false;
        }

        return convertView;
    }

    public void cancelAllTasks() {
        for (TextSizeTask task : mCurrentTasks) {
            task.cancel(true);
        }
    }

    public void showTitle(int currentItem) {
        if (mViews.get(currentItem) != null) {
            DreamViewHolder holder = (DreamViewHolder) mViews.get(currentItem).getTag();
            holder.mTitleText.setVisibility(View.VISIBLE);
            holder.mTitleText.startAnimation(mFadeInWithDelay);
        }
    }

    private class DreamViewHolder {
        public NetworkImageView mImageView;
        public BlurImageView mBlurImageView;
        public ProgressBar mProgressBar;
        public RobotoTextView mTitleText;
        public View mOverlay;
    }

    private class TextSizeTask extends PreCompositeTextTask {

        private TextView mTextView;
        private int mPosition;

        public TextSizeTask(TextView titleText, int maxWidth, int maxHeight, int position) {
            super(titleText.getPaint(), maxWidth, maxHeight);
            mTextView = titleText;
            mPosition = position;
        }

        @Override
        public void doOnComplete(int textSize) {
            mTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
            textSizes[mPosition] = textSize;
//            Log.i("TAG", "Text size " + textSize);
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + mPosition;
            return result;
        }
    }
}




Java Source Code List

com.android.fancyblurdemo.app.BitmapLruImageCache.java
com.android.fancyblurdemo.app.BlurImageView.java
com.android.fancyblurdemo.app.ContextWrapperEdgeEffect.java
com.android.fancyblurdemo.app.DreamAdapter.java
com.android.fancyblurdemo.app.EdgeEffectViewPager.java
com.android.fancyblurdemo.app.FlickrDream.java
com.android.fancyblurdemo.app.FlickrPhoto.java
com.android.fancyblurdemo.app.FlickrRequest.java
com.android.fancyblurdemo.app.FlickrScroller.java
com.android.fancyblurdemo.app.MainActivity.java
com.android.fancyblurdemo.app.MainApplication.java
com.android.fancyblurdemo.app.PhotoFragment.java
com.android.fancyblurdemo.app.PhotoSizeRequest.java
com.android.fancyblurdemo.app.PreCompositeTextTask.java
com.android.fancyblurdemo.app.RobotoTextView.java
com.android.fancyblurdemo.app.TopSwipeLayout.java
com.android.fancyblurdemo.app.VolleyManager.java
com.android.fancyblurdemo.app.imageblur.BlurDispatcher.java
com.android.fancyblurdemo.app.imageblur.BlurError.java
com.android.fancyblurdemo.app.imageblur.BlurManager.java
com.android.fancyblurdemo.app.imageblur.BlurQueue.java
com.android.fancyblurdemo.app.imageblur.BlurRequest.java
com.android.fancyblurdemo.app.imageblur.BlurResponseDelivery.java
com.android.fancyblurdemo.app.imageblur.BlurResponse.java
com.android.fancyblurdemo.app.imageblur.DiskLruImageCache.java
com.android.fancyblurdemo.app.imageblur.ImageBlurrer.java
com.android.fancyblurdemo.volley.AuthFailureError.java
com.android.fancyblurdemo.volley.CacheDispatcher.java
com.android.fancyblurdemo.volley.Cache.java
com.android.fancyblurdemo.volley.DefaultRetryPolicy.java
com.android.fancyblurdemo.volley.ExecutorDelivery.java
com.android.fancyblurdemo.volley.NetworkDispatcher.java
com.android.fancyblurdemo.volley.NetworkError.java
com.android.fancyblurdemo.volley.NetworkResponse.java
com.android.fancyblurdemo.volley.Network.java
com.android.fancyblurdemo.volley.NoConnectionError.java
com.android.fancyblurdemo.volley.ParseError.java
com.android.fancyblurdemo.volley.RequestQueue.java
com.android.fancyblurdemo.volley.Request.java
com.android.fancyblurdemo.volley.ResponseDelivery.java
com.android.fancyblurdemo.volley.Response.java
com.android.fancyblurdemo.volley.RetryPolicy.java
com.android.fancyblurdemo.volley.ServerError.java
com.android.fancyblurdemo.volley.TimeoutError.java
com.android.fancyblurdemo.volley.VolleyError.java
com.android.fancyblurdemo.volley.VolleyLog.java
com.android.fancyblurdemo.volley.toolbox.AndroidAuthenticator.java
com.android.fancyblurdemo.volley.toolbox.Authenticator.java
com.android.fancyblurdemo.volley.toolbox.BasicNetwork.java
com.android.fancyblurdemo.volley.toolbox.ByteArrayPool.java
com.android.fancyblurdemo.volley.toolbox.ClearCacheRequest.java
com.android.fancyblurdemo.volley.toolbox.DiskBasedCache.java
com.android.fancyblurdemo.volley.toolbox.HttpClientStack.java
com.android.fancyblurdemo.volley.toolbox.HttpHeaderParser.java
com.android.fancyblurdemo.volley.toolbox.HttpStack.java
com.android.fancyblurdemo.volley.toolbox.HurlStack.java
com.android.fancyblurdemo.volley.toolbox.ImageLoader.java
com.android.fancyblurdemo.volley.toolbox.ImageRequest.java
com.android.fancyblurdemo.volley.toolbox.JsonArrayRequest.java
com.android.fancyblurdemo.volley.toolbox.JsonObjectRequest.java
com.android.fancyblurdemo.volley.toolbox.JsonRequest.java
com.android.fancyblurdemo.volley.toolbox.NetworkImageView.java
com.android.fancyblurdemo.volley.toolbox.NoCache.java
com.android.fancyblurdemo.volley.toolbox.PoolingByteArrayOutputStream.java
com.android.fancyblurdemo.volley.toolbox.RequestFuture.java
com.android.fancyblurdemo.volley.toolbox.StringRequest.java
com.android.fancyblurdemo.volley.toolbox.Volley.java