com.cvte.LiZhiDemo.Activity.ImagePagerActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.cvte.LiZhiDemo.Activity.ImagePagerActivity.java

Source

/*******************************************************************************
 * Copyright 2011-2013 Sergey Tarasevich
 *
 * 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.cvte.LiZhiDemo.Activity;

import java.util.ArrayList;

import com.cvte.LiZhiDemo.R;
import com.cvte.LiZhiDemo.BaseClass.Constants.Extra;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.umeng.analytics.MobclickAgent;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

/**
 * ViewPager??Activity
 * @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
 */
public class ImagePagerActivity extends Activity {

    private static final String STATE_POSITION = "STATE_POSITION";

    DisplayImageOptions options;

    //?????? 
    private ImageView[] points;

    //???
    private int currentIndex;

    ViewPager pager;

    private LinearLayout mainLayout;

    private MyPageChangeListener myPageChangeListener;
    private ImageButton btCloss;

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.lizhi_image_pager_layout);

        btCloss = new ImageButton(this);
        btCloss = (ImageButton) findViewById(R.id.close);
        btCloss.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();

            }
        });

        ArrayList<String> imageUrls = getIntent().getStringArrayListExtra(Extra.IMAGES);
        // ??View?   
        int pagerPosition = getIntent().getIntExtra(Extra.IMAGE_POSITION, 0);

        // ??   
        if (savedInstanceState != null) {
            pagerPosition = savedInstanceState.getInt(STATE_POSITION);
        }

        /**
         * ????       */
        initPoint(imageUrls.size());

        options = new DisplayImageOptions.Builder().resetViewBeforeLoading(true).cacheInMemory(true)
                .imageScaleType(ImageScaleType.EXACTLY).bitmapConfig(Bitmap.Config.RGB_565)
                .displayer(new FadeInBitmapDisplayer(300)).build();

        pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(new ImagePagerAdapter(imageUrls));

        pager.setCurrentItem(pagerPosition);
        points[pagerPosition].setEnabled(true);
        pager.setOnPageChangeListener(new MyPageChangeListener());

    }

    /**????    * @param 
     * @return
     */
    private void initPoint(int picNum) {
        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.point_layout);
        points = new ImageView[4];
        for (int i = 0; i < 4; i++) {

            points[i] = (ImageView) linearLayout.getChildAt(i);
            points[i].setEnabled(false);
            //??????                   //points[i].setOnClickListener(this);
            points[i].setTag(i);
        }
        for (int i = picNum; i < 4; i++) {
            points[i] = (ImageView) linearLayout.getChildAt(i);
            points[i].setVisibility(View.GONE);
        }

    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        // ???      outState.putInt(STATE_POSITION, pager.getCurrentItem());
    }

    private class ImagePagerAdapter extends PagerAdapter {

        private ArrayList<String> images;
        private LayoutInflater inflater;

        ImagePagerAdapter(ArrayList<String> images) {
            this.images = images;
            inflater = getLayoutInflater();
        }

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

        @Override
        public void finishUpdate(View container) {
        }

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

        @Override
        public Object instantiateItem(ViewGroup view, int position) {
            View imageLayout = inflater.inflate(R.layout.lizhi_pager_image_layout, view, false);
            ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
            final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);

            ImageLoader.getInstance().displayImage(images.get(position), imageView, 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: // ?I/O                     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;
                            }
                            //Toast.makeText(ImagePagerActivity.this, message, Toast.LENGTH_SHORT).show();

                            spinner.setVisibility(View.GONE);
                        }

                        @Override
                        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                            spinner.setVisibility(View.GONE); // ?
                        }
                    });

            ((ViewPager) view).addView(imageLayout, 0); // ????ViewPager
            return imageLayout;
        }

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

        @Override
        public void restoreState(Parcelable state, ClassLoader loader) {
        }

        @Override
        public Parcelable saveState() {
            return null;
        }

        @Override
        public void startUpdate(View container) {
        }
    }

    public class MyPageChangeListener implements OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }

        @Override
        public void onPageSelected(int position) {

            Log.d("ImagePager", position + "");
            for (int i = 0; i < 4; i++) {
                if (i == position) {
                    points[i].setEnabled(true);
                } else {
                    points[i].setEnabled(false);
                }
            }
        }

    }
}