com.meet.ychmusic.activity.PhotoDetailsActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.meet.ychmusic.activity.PhotoDetailsActivity.java

Source

/*******************************************************************************
 * Copyright 2011, 2012 Chris Banes.
 *
 * 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 com.meet.ychmusic.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.meet.api.PFInterface;
import com.meet.common.HackyViewPager;
import com.meet.ychmusic.BaseActivity;
import com.meet.ychmusic.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

import uk.co.senab.photoview.PhotoView;

public class PhotoDetailsActivity extends BaseActivity implements ViewPager.OnPageChangeListener {
    private static final String TAG = "PhotoDetailsActivity";
    int index = 0;
    private String[] album;
    SamplePagerAdapter mAdapter;
    TextView pageText;
    protected ImageLoader imageLoader = ImageLoader.getInstance();// jar
    //    private DisplayImageOptions options;
    private DisplayImageOptions options;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        imageLoader.init(ImageLoaderConfiguration.createDefault(PhotoDetailsActivity.this));
        options = new DisplayImageOptions.Builder().showImageForEmptyUri(R.drawable.pic_load_hd)
                .showImageOnFail(R.drawable.ic_delete).resetViewBeforeLoading().cacheOnDisc()
                .imageScaleType(ImageScaleType.EXACTLY).bitmapConfig(Bitmap.Config.RGB_565)
                .displayer(new FadeInBitmapDisplayer(300)).build();

        album = getIntent().getStringArrayExtra("album");
        index = getIntent().getIntExtra("index", 0);
        setContentView(R.layout.activity_image_brower);
        if (album != null) {
            pageText = (TextView) findViewById(R.id.pageText);
            pageText.setText(String.format("%d/%d", index + 1, album.length));
        }
        ViewPager mViewPager = (HackyViewPager) findViewById(R.id.view_pager);
        mAdapter = new SamplePagerAdapter(album);
        mViewPager.setAdapter(mAdapter);
        mViewPager.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mViewPager.setClickable(true);

        mViewPager.setCurrentItem(index, false);
        mViewPager.setOnPageChangeListener(this);

    }

    @Override
    protected void initViews() {

    }

    @Override
    protected void initEvents() {

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (album != null) {
            pageText.setText(String.format("%d/%d", position + 1, album.length));
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    class SamplePagerAdapter extends PagerAdapter {

        //      private static int[] sDrawables = { R.drawable.pic_people_1_hd, R.drawable.pic_people_1_hd, R.drawable.pic_people_1_hd,
        //            R.drawable.pic_people_1_hd, R.drawable.pic_people_1_hd, R.drawable.pic_people_1_hd };
        String[] album;

        public SamplePagerAdapter(String[] album) {
            this.album = album;
        }

        @Override
        public int getCount() {
            return album.length;
        }

        @Override
        public View instantiateItem(ViewGroup container, int position) {

            String uri = PFInterface.imageAttachmentUrl(album[position]);
            View imageLayout = getLayoutInflater().inflate(R.layout.pro_imageshow, container, false);
            final PhotoView photoView = (PhotoView) imageLayout.findViewById(R.id.image);

            final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);

            final String imgUrl = uri;
            imageLoader.displayImage(imgUrl, photoView, options, new SimpleImageLoadingListener() {
                @Override
                public void onLoadingStarted(String imageUri, View view) {
                    spinner.setVisibility(View.VISIBLE);
                }

                @Override
                public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                    String message = null;
                    switch (failReason.getType()) {
                    case IO_ERROR:
                        message = "Input/Output error";
                        break;
                    case DECODING_ERROR:
                        message = "Image can't be decoded";
                        break;
                    case NETWORK_DENIED:
                        message = "Downloads are denied";
                        break;
                    case OUT_OF_MEMORY:
                        message = "Out Of Memory error";
                        break;
                    case UNKNOWN:
                        message = "Unknown error";
                        break;
                    }
                    showCustomToast(message);

                    spinner.setVisibility(View.GONE);
                }

                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                    spinner.setVisibility(View.GONE);
                }
            });
            ((ViewPager) container).addView(imageLayout, 0);
            return imageLayout;

        }

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

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

    }

    public static Intent createIntent(Context context, String[] array, int index) {
        Intent intent = new Intent();
        intent.setClass(context, PhotoDetailsActivity.class);
        intent.putExtra("album", array);
        intent.putExtra("index", index);
        return intent;
    }

    public static Intent createIntent(Context context, String item) {
        String[] array = { item };
        Intent intent = new Intent();
        intent.setClass(context, PhotoDetailsActivity.class);
        intent.putExtra("album", array);
        intent.putExtra("index", 0);
        return intent;
    }
}