• 显示大图Activity(支持手势放大)


    xml:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/black">
    
        <android.support.v4.view.ViewPager
            android:id="@+id/picViewPager"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
    </RelativeLayout>
    

      

    java类:

    public class ActShowBigPicture extends BaseActivity implements ZoomImageView.onImageTouchedListener {
    /** 图片url集合 intent key */
    public static final String INTENT_PICURLS = "picUrls";
    /** 显示下标 intent key */
    public static final String INTENT_SHOW_POS = "showPos";

    private List<String> picUrls; // 图片URL集合
    private int showPos; // 显示下标
    private int oldPosition = 0;

    private ViewPager picViewPager;

    private List<View> zoomImageViewContainers;
    private List<ZoomImageView> zoomImageViews;
    private DisplayImageOptions mPicOptions;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.act_show_big_picture);

    mBar.setVisibility(View.GONE);

    dismissLoadingView();

    // 获取传递过来的Intent对象
    Intent intent = getIntent();
    picUrls = intent.getStringArrayListExtra(INTENT_PICURLS);
    showPos = intent.getIntExtra(INTENT_SHOW_POS, 0);

    if (null == picUrls || picUrls.size() <= 0) {
    finish();
    return ;
    }

    mPicOptions = new DisplayImageOptions.Builder()
    .showImageOnLoading(R.drawable.def_banner_img)
    .showImageOnFail(R.drawable.def_banner_img)
    .showImageForEmptyUri(R.drawable.def_banner_img)
    .cacheOnDisk(true)
    .cacheInMemory(false)
    .displayer(new FadeInBitmapDisplayer(200, true, true, false)) // 显示动画
    .build();

    // 初始化View
    picViewPager = (ViewPager)findViewById(R.id.picViewPager);

    // 设置事件
    // findViewById(R.id.layout_back).setOnClickListener(this);

    zoomImageViews = new ArrayList<>();
    zoomImageViewContainers = new ArrayList<>();
    for (int i=0; i<picUrls.size(); i++) {
    View zoomImageViewContainer = getLayoutInflater().inflate(R.layout.item_big_picture, null);
    ZoomImageView zommImageView = (ZoomImageView)zoomImageViewContainer.findViewById(R.id.zommImageView);
    zommImageView.setOnImageTouchedListener(this);
    zommImageView.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
    ZoomImageView curZoomImageView = (ZoomImageView)v;
    Bitmap tempImageBitmap = curZoomImageView.getImageBitmap();
    if (null == tempImageBitmap) {
    return false;
    }

    int curW = tempImageBitmap.getWidth();
    int curH = tempImageBitmap.getWidth();

    Matrix m = curZoomImageView.getImageViewMatrix();
    RectF rect = new RectF(0, 0, curW, curH);
    m.mapRect(rect);
    // 图片超出屏幕范围后移动
    // Log.i("wg", "rect.right= " + rect.right + " _ rect.left = " + rect.left);
    if (!(rect.right > curZoomImageView.getWidth() + 0.1 && rect.left < -0.1)) {
    picViewPager.requestDisallowInterceptTouchEvent(false);
    } else {
    picViewPager.requestDisallowInterceptTouchEvent(true);
    }

    return false;
    }
    });

    zoomImageViewContainers.add(zoomImageViewContainer);
    zoomImageViews.add(zommImageView);
    }

    // 设置图片间的间距
    // final float scale = getResources().getDisplayMetrics().density;
    // int pagerMarginPixels = (int) (PAGER_MARGIN_DP * scale + 0.5f);
    // picViewPager.setPageMargin(pagerMarginPixels);
    // picViewPager.setPageMarginDrawable(new ColorDrawable(Color.BLACK));
    // 设置适配器
    picViewPager.setAdapter(new PagerAdapter() {
    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
    ((ViewPager)arg0).removeView(zoomImageViewContainers.get(arg1));
    }

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

    @Override
    public Object instantiateItem(View arg0, int arg1) {
    View v = zoomImageViewContainers.get(arg1);
    ((ViewPager) arg0).addView(v, 0);
    return v;
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
    return arg0 == (arg1);
    }

    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {
    }

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

    @Override
    public void startUpdate(View arg0) {
    }
    });
    picViewPager.setOnPageChangeListener(new SimpleOnPageChangeListener() {
    @Override
    public void onPageSelected(int position) {
    // 恢复原图最小化
    ZoomImageView oldImageViewTouch = zoomImageViews.get(oldPosition);
    if (null != oldImageViewTouch && null != oldImageViewTouch.getImageBitmap() && !oldImageViewTouch.getImageBitmap().isRecycled()) {
    oldImageViewTouch.zoomTo(oldImageViewTouch.zoomDefault());
    }

    ZoomImageView curImageViewTouch = zoomImageViews.get(position);
    Bitmap curImageBitmap = curImageViewTouch.getImageBitmap();
    // 当图片Bitmap为空或者已经被回收的时候才去加载
    if (null == curImageBitmap || curImageBitmap.isRecycled()) {
    loadImageViewBitmap(picUrls.get(position), curImageViewTouch, null);
    }

    oldPosition = position;
    }
    });

    picViewPager.setCurrentItem(showPos, false);
    if (showPos == 0) {
    // 显示第一张图片
    // 显示Loading
    ImageLoader.getInstance().displayImage(picUrls.get(0), zoomImageViews.get(0), mPicOptions, new SimpleImageLoadingListener(){
    @Override
    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
    super.onLoadingComplete(imageUri, view, loadedImage);

    // ((FrameLayout)zoomImageViews.get(0).getParent()).findViewById(R.id.pagerLoad).setVisibility(View.GONE);
    }

    @Override
    public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
    super.onLoadingFailed(imageUri, view, failReason);

    // 隐藏Loading
    // ((FrameLayout)zoomImageViews.get(0).getParent()).findViewById(R.id.pagerLoad).setVisibility(View.GONE);

    // 显示loading错误View
    // ((FrameLayout)zoomImageViews.get(0).getParent()).findViewById(R.id.loaddataErrRL).setVisibility(View.VISIBLE);
    }
    });
    }
    }

    private void loadImageViewBitmap(final String picUrl, final ImageView iv, final View parentView) {
    if (null != parentView) {
    // 显示Loading
    // parentView.findViewById(R.id.pagerLoad).setVisibility(View.VISIBLE);
    }

    ImageLoader.getInstance().displayImage(picUrl, iv, mPicOptions, new SimpleImageLoadingListener() {
    @Override
    public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
    super.onLoadingFailed(imageUri, view, failReason);

    // 获取父View
    // final FrameLayout parentView = (FrameLayout)iv.getParent();

    // 隐藏Loading
    // parentView.findViewById(R.id.pagerLoad).setVisibility(View.GONE);

    // 显示loading错误View
    // parentView.findViewById(R.id.loaddataErrRL).setVisibility(View.VISIBLE);
    }

    @Override
    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
    super.onLoadingComplete(imageUri, view, loadedImage);

    // 获取父View
    // final FrameLayout parentView = (FrameLayout)iv.getParent();

    if (null == loadedImage) {
    // 显示loading错误View
    // parentView.findViewById(R.id.loaddataErrRL).setVisibility(View.VISIBLE);
    }

    // 隐藏Loading
    // parentView.findViewById(R.id.pagerLoad).setVisibility(View.GONE);

    }
    });
    }

    @Override
    public void onImageTouched() {
    finish();
    }

    public static void showPic(Context context, String path) {
    if (TextUtils.isEmpty(path)) return ;

    ArrayList<String> picurls = new ArrayList<>();
    picurls.add(path);
    showPic(context, picurls);
    }

    public static void showPic(Context context, ArrayList<String> picurls) {
    showPic(context, picurls, 0);
    }

    public static void showPic(Context context, ArrayList<String> picurls, int postion) {
    if (null == picurls || picurls.size() == 0) return ;

    Intent intent = new Intent(context, ActShowBigPicture.class);
    intent.putStringArrayListExtra(ActShowBigPicture.INTENT_PICURLS, picurls);
    if (postion != 0) {
    intent.putExtra(INTENT_SHOW_POS, postion);
    }
    context.startActivity(intent);
    }


    @Override
    protected boolean isNotSwipe() {
    return true;
    }
    }

      

    ZoomImageView类参考前一篇随笔
  • 相关阅读:
    [转]Angular4首页加载慢优化之路
    [转]angular 禁止缓存
    微服务之分布式跟踪系统(springboot+pinpoint)
    分布式追踪系统设计与实现
    QLExpress 规则引擎使用介绍
    几个常见规则引擎的简单介绍和演示
    大数据对账
    美团配送资金安全治理之对账体系建设
    美的支付-对账系统实现
    redis实现对账(集合比较)功能
  • 原文地址:https://www.cnblogs.com/zyandroid/p/5013212.html
Copyright © 2020-2023  润新知