点击查看大图Activity

1.使用方式

Intent intent = new Intent(FriendCircleActivity.this, ImageGralleryPagerActivity.class);
//0,索引,第几张图片 intent.putExtra(ImageGralleryPagerActivity.EXTRA_IMAGE_INDEX,
0); ArrayList<String> imgs = new ArrayList<String>(); imgs.add(“图片URL”); intent.putStringArrayListExtra("imgs", imgs); startActivity(intent);

2.展示大图Activity

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.SpannableString;
import android.text.style.RelativeSizeSpan;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import com.jlm.happyselling.R;
import com.jlm.happyselling.base.ECSuperActivity;
import com.jlm.happyselling.helper.IMessageSqlManager;
import com.jlm.happyselling.model.ViewImageInfo;
import com.jlm.happyselling.util.ECUtils;
import com.jlm.happyselling.util.FileAccessor;
import com.jlm.happyselling.util.LogUtil;
import com.jlm.happyselling.util.ToastUtil;
import com.jlm.happyselling.widget.HackyViewPager;

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

public class ImageGralleryPagerActivity extends ECSuperActivity implements View.OnClickListener {

    private static final String TAG = "ImageGralleryPagerActivity";

    private static final String STATE_POSITION = "STATE_POSITION";
    public static final String EXTRA_IMAGE_INDEX = "image_index";
    public static final String EXTRA_IMAGE_URLS = "image_urls";
    public static final String EXTRA_IMAGE_URLS_ID = "image_urls_id";
    public static boolean isFireMsg = false;

    /**
     *
     */
    private boolean mFullscreen = true;
    private HackyViewPager mPager;
    private int pagerPosition;
    private TextView indicator;
    private List<ViewImageInfo> urls;
    private List<String> imgs;

    @Override
    protected int getLayoutId() {
        return R.layout.image_grallery_container;
    }

    @Override
    protected boolean isEnableSwipe() {
        // TODO Auto-generated method stub
        return false;
    }

    public static String sgId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getTopBarView().setTopBarToStatus(1, R.drawable.topbar_back_bt, -1, "1 / 1", this);

        if (!getIntent().hasExtra("imgs")) {
            ImageGalleryFragment.i = 0;
            pagerPosition = getIntent().getIntExtra(EXTRA_IMAGE_INDEX, 0);
            urls = getIntent().getParcelableArrayListExtra(EXTRA_IMAGE_URLS);
            sgId = getIntent().getStringExtra(EXTRA_IMAGE_URLS_ID);
            onChatImgLayout();
        } else {
            ImageGalleryFragment2.i = 0;
            pagerPosition = getIntent().getIntExtra(EXTRA_IMAGE_INDEX, 0);
            imgs = getIntent().getStringArrayListExtra("imgs");
            onImgLayout();
        }

        // 更新下标
        mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrollStateChanged(int arg0) {
            }

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

            @Override
            public void onPageSelected(int arg0) {
                initIndicatorIndex(arg0);
            }

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

        mPager.setCurrentItem(pagerPosition);
        initIndicatorIndex(pagerPosition);
        setTitleFooterVisible(true);
    }

    private void onImgLayout() {
        if (imgs == null || imgs.isEmpty()) {
            finish();
            return;
        }
        if (pagerPosition > imgs.size()) {
            pagerPosition = 0;
        }
        setActionBarTitle(pagerPosition + "/" + (imgs != null ? imgs.size() : 0));
        mPager = (HackyViewPager) findViewById(R.id.pager);
        final ImagePagerAdapter1 adapter1 = new ImagePagerAdapter1(getSupportFragmentManager(),imgs);
        mPager.setAdapter(adapter1);
        indicator = (TextView) findViewById(R.id.indicator);
    }

    private void onChatImgLayout() {
        if (urls == null || urls.isEmpty()) {
            finish();
            return;
        }
        if (pagerPosition > urls.size()) {
            pagerPosition = 0;
        }

        setActionBarTitle(pagerPosition + "/" + (urls != null ? urls.size() : 0));
        mPager = (HackyViewPager) findViewById(R.id.pager);
        final ImagePagerAdapter mAdapter = new ImagePagerAdapter(getSupportFragmentManager(), urls);
        mPager.setAdapter(mAdapter);
        indicator = (TextView) findViewById(R.id.indicator);
        findViewById(R.id.imagebrower_iv_save).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mAdapter != null) {
                    ViewImageInfo viewImageInfo = urls.get(mPager.getCurrentItem());
                    if (viewImageInfo == null || !viewImageInfo.isDownload()) {
                        ToastUtil.show(R.string.save_img_waite_download);
                        return;
                    }
                    try {
                        File file = new File(FileAccessor.getImagePathName(), viewImageInfo.getPicurl());
                        if (viewImageInfo != null && viewImageInfo.isGif()) {
                            ECUtils.saveImage(file.getAbsolutePath(), ".gif");
                            return;
                        }
                        ECUtils.saveImage(file.getAbsolutePath());
                    } catch (Exception e) {
                        LogUtil.e(TAG, "onContextItemSelected error ");
                    }

                }
            }
        });
        //待改 lu
        if (mAdapter != null) {
            ViewImageInfo viewImageInfoItem = urls.get(mPager.getCurrentItem());
            if (viewImageInfoItem != null && IMessageSqlManager.isFireMsg(viewImageInfoItem.getMsgLocalId())) {
                findViewById(R.id.imagebrower_iv_save).setVisibility(View.GONE);
            } else {
                findViewById(R.id.imagebrower_iv_save).setVisibility(View.GONE);

            }

        }
    }

    private void initIndicatorIndex(int arg0) {
        CharSequence text = getString(R.string.viewpager_indicator, arg0 + 1, mPager.getAdapter().getCount());
        SpannableString spannableString = new SpannableString(text);
        spannableString.setSpan(new RelativeSizeSpan(1.5F), 0, text.toString().indexOf("/"), SpannableString.SPAN_PRIORITY);
        indicator.setText(spannableString);
    }

    @Override
    public void onClick(View v) {
        mHandlerCallbck.sendEmptyMessageDelayed(1, 350L);
        switch (v.getId()) {
            case R.id.btn_left:
                hideSoftKeyboard();
                finish();
                break;

            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (urls != null) {
            urls.clear();
            urls = null;
        }
        if (mHandlerCallbck != null) {
            mHandlerCallbck.removeCallbacksAndMessages(null);
        }
        mPager = null;
        isFireMsg = false;
        ImageGalleryFragment.i = 0;
        System.gc();

    }

    private final Handler mHandlerCallbck = new Handler() {

        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
            mFullscreen = !mFullscreen;
            setTitleFooterVisible(mFullscreen);
        }

    };

    @Override
    public void onBaseContentViewAttach(View contentView) {
        View activityLayoutView = getActivityLayoutView();
        ((ViewGroup) activityLayoutView.getParent()).removeView(activityLayoutView);
        ((ViewGroup) getWindow().getDecorView()).addView(activityLayoutView, 1);

    }


    /**
     * @param request
     */
    private void requestStatusbars(boolean request) {
        if (request) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
            return;
        }
        LogUtil.d(getClass().getSimpleName(), "request custom title");
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    /**
     * Full screen, hidden actionBar
     *
     * @param visible
     */
    void setTitleFooterVisible(boolean visible) {
        if (visible) {
            requestStatusbars(false);
            showTitleView();
            return;
        }

        requestStatusbars(true);
        hideTitleView();
    }

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

    private class ImagePagerAdapter extends FragmentStatePagerAdapter {

        public List<ViewImageInfo> fileList;

        public ImagePagerAdapter(FragmentManager fm, List<ViewImageInfo> fileList) {
            super(fm);
            this.fileList = fileList;
        }

        @Override
        public int getCount() {
            return fileList == null ? 0 : fileList.size();
        }

        @Override
        public Fragment getItem(int position) {
            ViewImageInfo url = fileList.get(position);
            return ImageGalleryFragment.newInstance(url);
        }

    }

    private class ImagePagerAdapter1 extends FragmentStatePagerAdapter {

        public List<String> fileList;

        public ImagePagerAdapter1(FragmentManager fm, List<String> fileList) {
            super(fm);
            this.fileList = fileList;
        }

        @Override
        public int getCount() {
            return fileList == null ? 0 : fileList.size();
        }

        @Override
        public Fragment getItem(int position) {
            String url = fileList.get(position);
            return ImageGalleryFragment2.newInstance(url);
        }

    }

    @Override
    public boolean isEnableRightSlideGesture() {
        return false;
    }
}

 3.布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:background="#ff171b19">

    <com.jlm.happyselling.widget.HackyViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/black" />

    <RelativeLayout
        android:id="@+id/imagebrower_layout_pagelayout"
        android:background="#5e171b19"
        android:paddingLeft="15.0dip"
        android:paddingRight="15.0dip"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:minHeight="80.0dip"
        android:layout_alignParentBottom="true">

        <ImageView
            android:id="@+id/imagebrower_iv_save"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="visible"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true" />

        <TextView
            android:textSize="18.0sp"
            android:textColor="#ffcdcdcd"
            android:id="@+id/indicator"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true" />
    </RelativeLayout>

</RelativeLayout>

 

posted @ 2017-07-31 18:42  点滴之水  阅读(604)  评论(0编辑  收藏  举报