Android Open Source - connexus-android Image Adapter






From Project

Back to project page connexus-android.

License

The source code is released under:

Apache License

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

/*
 * Copyright (C) 2013 Zach Whaley, Trevor Latson
 */*from w ww  .j av a2s .co  m*/
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ginger.connexus.adapter;

import ginger.connexus.ui.RecyclingImageView;
import ginger.connexus.util.ImageFetcher;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;

/**
 * The main adapter that backs the GridView. This is fairly standard except the
 * number of columns in the GridView is used to create a fake top row of empty
 * views as we use a transparent ActionBar and don't want the real top row of
 * images to start off covered by it.
 */
public class ImageAdapter extends ArrayAdapter<String> {

    private final Context mContext;
    private final ImageFetcher mImageFetcher;
    private int mItemHeight = 0;
    private int mNumColumns = 0;
    private GridView.LayoutParams mImageViewLayoutParams;

    public ImageAdapter(Context context, List<String> imageUrls, ImageFetcher imageFetcher) {
        super(context, -1, -1, imageUrls);
        mContext = context;
        mImageFetcher = imageFetcher;

        mImageViewLayoutParams = new GridView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup container) {
        // ImageView thumbnails
        ImageView imageView;
        if (convertView == null) {
            // if it's not recycled, instantiate and initialize
            imageView = new RecyclingImageView(mContext);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setLayoutParams(mImageViewLayoutParams);
        } else {
            // Otherwise re-use the converted view
            imageView = (ImageView) convertView;
        }

        // Check the height matches our calculated column width
        if (imageView.getLayoutParams().height != mItemHeight) {
            imageView.setLayoutParams(mImageViewLayoutParams);
        }

        // Finally load the image asynchronously into the ImageView, this also
        // takes care of setting a placeholder image while the background thread
        // runs
        mImageFetcher.loadImage(getItem(position), imageView);
        return imageView;
    }

    /**
     * Sets the item height. Useful for when we know the column width so the
     * height can be set to match.
     *
     * @param height
     */
    public void setItemHeight(int height) {
        if (height == mItemHeight) {
            return;
        }
        mItemHeight = height;
        mImageViewLayoutParams = new GridView.LayoutParams(LayoutParams.MATCH_PARENT, mItemHeight);
        mImageFetcher.setImageSize(height);
        notifyDataSetChanged();
    }

    public void setNumColumns(int numColumns) {
        mNumColumns = numColumns;
    }

    public int getNumColumns() {
        return mNumColumns;
    }
}




Java Source Code List

android.UnusedStub.java
ginger.connexus.Connexus.java
ginger.connexus.activity.AuthActivity.java
ginger.connexus.activity.BaseActivity.java
ginger.connexus.activity.ImageDetailActivity.java
ginger.connexus.activity.ImageGridActivity.java
ginger.connexus.activity.MainActivity.java
ginger.connexus.activity.SearchableActivity.java
ginger.connexus.adapter.ImageAdapter.java
ginger.connexus.fragment.ChooseAccountFragment.java
ginger.connexus.fragment.GridFragment.java
ginger.connexus.fragment.ImageDetailFragment.java
ginger.connexus.fragment.ImageGridFragment.java
ginger.connexus.fragment.SpiceFragment.java
ginger.connexus.fragment.StreamGridFragment.java
ginger.connexus.model.ConnexusImage.java
ginger.connexus.model.ConnexusStream.java
ginger.connexus.network.ConnexusApi.java
ginger.connexus.network.RequestAllStreams.java
ginger.connexus.network.RequestNearbyStreams.java
ginger.connexus.network.RequestStreamImages.java
ginger.connexus.network.RequestSubscribedStreams.java
ginger.connexus.network.RequestUploadURL.java
ginger.connexus.network.UploadImage.java
ginger.connexus.service.ConnexusService.java
ginger.connexus.ui.RecyclingBitmapDrawable.java
ginger.connexus.ui.RecyclingImageView.java
ginger.connexus.util.AccountUtils.java
ginger.connexus.util.AsyncTask.java
ginger.connexus.util.DiskLruCache.java
ginger.connexus.util.ImageCache.java
ginger.connexus.util.ImageFetcher.java
ginger.connexus.util.ImageResizer.java
ginger.connexus.util.ImageWorker.java
ginger.connexus.util.Images.java
ginger.connexus.util.Utils.java