org.uis.luu.PicActivity.java Source code

Java tutorial

Introduction

Here is the source code for org.uis.luu.PicActivity.java

Source

/*
 * Copyright (c) 2015 uis
 *
 * 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 org.uis.luu;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.uis.library.common.util.FileFactory;
import org.uis.library.common.util.LogUtil;
import org.uis.library.common.util.MD5;
import org.uis.library.l;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author uis
 * @version 1.0.0
 * @date 2015/12/18
 * @time 17:30
 */
public class PicActivity extends Activity {
    int postion = 0;
    ArrayList<String> imgList;
    ViewPager viewPager;
    TextView num;
    List<ImageView> mList = new ArrayList<ImageView>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pic);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        num = (TextView) findViewById(R.id.num);
        postion = getIntent().getIntExtra("position", 0);
        imgList = getIntent().getStringArrayListExtra("item");
        num.setText("p=" + postion);

        viewPager.setAdapter(new PicAdapter());
        viewPager.setCurrentItem(postion, false);
        //viewPager.setDrawingCacheEnabled(false);
        //viewPager.setOffscreenPageLimit(3);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                num.setText("p=" + position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private ImageView createPager(String url) {
        LinearLayout.LayoutParams dotParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        ImageView imageView = new ImageView(this);
        imageView.setLayoutParams(dotParams);
        imageView.setTag(url);
        imageView.setImageResource(R.mipmap.ic_launcher);

        //imageView.setImageBitmap(null);
        return imageView;
    }

    private class PicAdapter extends android.support.v4.view.PagerAdapter {
        @Override
        public int getCount() {
            if (mList == null)
                return 0;
            return imgList.size();
        }

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

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {

            //mList.get(position).setImageBitmap(null);
            //container.removeView(mList.get(position));
            if (object instanceof ImageView) {
                container.removeView((ImageView) object);
                object = null;
                LogUtil.d("pic", "destroyItem" + position);
            }
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            LogUtil.d("pic", "instantiate" + position);
            ImageView imageView = createPager(imgList.get(position));
            ((ViewPager) container).addView(imageView);
            //mList.get(position).setDrawingCacheEnabled(false);
            l.image().load(imageView, imgList.get(position), "Luu/images/");
            return imageView;
        }
    }
}