Android Open Source - connexus-android Image Detail Activity






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   ww  w .  ja  va2s  .  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.activity;

import ginger.connexus.BuildConfig;
import ginger.connexus.R;
import ginger.connexus.fragment.ImageDetailFragment;
import ginger.connexus.util.ImageCache;
import ginger.connexus.util.ImageFetcher;
import ginger.connexus.util.Utils;

import java.util.Arrays;
import java.util.List;

import android.app.ActionBar;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;

public class ImageDetailActivity extends BaseActivity implements OnClickListener {

    @SuppressWarnings("unused")
    private static final String TAG = ImageDetailActivity.class.toString();
    private static final String IMAGE_CACHE_DIR = "images";

    public static final String EXTRA_IMAGE = "extra_image";
    public static final String EXTRA_IMAGE_URLS = "extra_image_urls";

    private ImagePagerAdapter mAdapter;
    private ImageFetcher mImageFetcher;
    private ViewPager mPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        if (BuildConfig.DEBUG) {
            Utils.enableStrictMode();
        }
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pager_image_detail);

        // Fetch screen height and width, to use as our max size when loading
        // images as this
        // activity runs full screen
        final DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        final int height = displayMetrics.heightPixels;
        final int width = displayMetrics.widthPixels;

        // For this sample we'll use half of the longest width to resize our
        // images. As the image scaling ensures the image is larger than this,
        // we should be left with a resolution that is appropriate for both
        // portrait and landscape. For best image quality we shouldn't divide by
        // 2, but this will use more memory and require a larger memory cache.
        final int longest = (height > width ? height : width) / 2;

        ImageCache.ImageCacheParams cacheParams = new ImageCache.ImageCacheParams(this, IMAGE_CACHE_DIR);
        // Set memory cache to 25% of app memory
        cacheParams.setMemCacheSizePercent(0.25f);

        // The ImageFetcher takes care of loading images into our ImageView
        // children asynchronously
        mImageFetcher = new ImageFetcher(this, longest);
        mImageFetcher.addImageCache(getSupportFragmentManager(), cacheParams);
        mImageFetcher.setImageFadeIn(false);

        // Set up ViewPager and backing adapter
        final List<String> imageUrls = Arrays.asList(getIntent().getStringArrayExtra(EXTRA_IMAGE_URLS));
        mAdapter = new ImagePagerAdapter(getSupportFragmentManager(), imageUrls);
        mPager = (ViewPager) findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);
        mPager.setPageMargin((int) getResources().getDimension(R.dimen.image_detail_pager_margin));
        mPager.setOffscreenPageLimit(2);

        // Set up activity to go full screen
        getWindow().addFlags(LayoutParams.FLAG_FULLSCREEN);

        // Enable some additional newer visibility and ActionBar features to
        // create a more immersive photo viewing experience
        if (Utils.hasHoneycomb()) {
            final ActionBar actionBar = getActionBar();

            // Hide title text and set home as up
            actionBar.setDisplayShowTitleEnabled(false);
            actionBar.setDisplayHomeAsUpEnabled(true);

            // Hide and show the ActionBar as the visibility changes
            mPager.setOnSystemUiVisibilityChangeListener(
                    new View.OnSystemUiVisibilityChangeListener() {
                        @Override
                        public void onSystemUiVisibilityChange(int vis) {
                            if ((vis & View.SYSTEM_UI_FLAG_LOW_PROFILE) != 0) {
                                actionBar.hide();
                            } else {
                                actionBar.show();
                            }
                        }
                    });

            // Start low profile mode and hide ActionBar
            mPager.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
            actionBar.hide();
        }

        // Set the current item based on the extra passed in to this activity
        final int extraCurrentItem = getIntent().getIntExtra(EXTRA_IMAGE, -1);
        if (extraCurrentItem != -1) {
            mPager.setCurrentItem(extraCurrentItem);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        mImageFetcher.setExitTasksEarly(false);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mImageFetcher.setExitTasksEarly(true);
        mImageFetcher.flushCache();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mImageFetcher.closeCache();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }

    /**
     * Called by the ViewPager child fragments to load images via the one
     * ImageFetcher
     */
    public ImageFetcher getImageFetcher() {
        return mImageFetcher;
    }

    /**
     * The main adapter that backs the ViewPager. A subclass of
     * FragmentStatePagerAdapter as there could be a large number of items in
     * the ViewPager and we don't want to retain them all in memory at once but
     * create/destroy them on the fly.
     */
    private class ImagePagerAdapter extends FragmentStatePagerAdapter {

        private final List<String> mImageUrls;

        public ImagePagerAdapter(FragmentManager fm, List<String> imageUrls) {
            super(fm);
            mImageUrls = imageUrls;
        }

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

        @Override
        public Fragment getItem(int position) {
            return ImageDetailFragment.newInstance(mImageUrls.get(position));
        }
    }

    /**
     * Set on the ImageView in the ViewPager children fragments, to
     * enable/disable low profile mode when the ImageView is touched.
     */
    @Override
    public void onClick(View v) {
        final int vis = mPager.getSystemUiVisibility();
        if ((vis & View.SYSTEM_UI_FLAG_LOW_PROFILE) != 0) {
            mPager.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
        } else {
            mPager.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
        }
    }

}




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