Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

开启缩放后,用Glide加载,图像显示很慢 #80

Open
wmailn opened this issue Sep 12, 2017 · 1 comment
Open

开启缩放后,用Glide加载,图像显示很慢 #80

wmailn opened this issue Sep 12, 2017 · 1 comment

Comments

@wmailn
Copy link

wmailn commented Sep 12, 2017

如题,嵌套在ViewPage里面,用Glide加载图片,代码中动态创建PhotoView。

启用图片缩放功能photoView.enable(); 后,图片要将近3秒才能显示出来(本地图片);

// 禁用图片缩放功能photoView.disenable();后,图像不到1秒就显示出来了;

请问有遇到这个情况吗?

附:代码片段:

package com.simope.eccs.ui.other;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.simope.eccs.R;
import com.simope.eccs.base.BaseActivity;
import com.simope.eccs.widget.PhotoView;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

/**

  • Created by User9527 on 2017/5/10.

  • 图片浏览器页面
    */
    @EActivity(R.layout.activity_image_viewer)
    public class ImageViewerActivity extends BaseActivity {

    @ViewById
    ViewPager mViewPager;

    @ViewById
    TextView mTextView;

    /**

    • 单条图片地址(单条地址和图片地址列表同时不为空时,优先显示多条地址)
      /
      @extra
      String imageUrl;
      /
      *
    • 图片地址列表(单条地址和图片地址列表同时不为空时,优先显示多条地址)
      /
      @extra
      ArrayList imageUrlList;
      /
      *
    • 当前显示的图片位置
      /
      @extra
      int positionImage;
      /
      *
    • PhotoView图片浏览器的列表
      */
      private List mImageList;

    @AfterViews
    public void initData() {
    // 全屏
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager
    .LayoutParams.FLAG_FULLSCREEN);
    // 初始化PhotoView图片浏览器的列表
    mImageList = new ArrayList<>();
    // 单条图片地址
    if (!TextUtils.isEmpty(imageUrl) && positionImage == 0) {
    mImageList.add(getPhotoView(imageUrl));
    }
    // 图片地址列表
    if (null != imageUrlList && !imageUrlList.isEmpty()) {
    mImageList.clear();
    for (String url : imageUrlList) {
    mImageList.add(getPhotoView(url));
    }
    }
    // 设置ViewPager
    if (null != mImageList && !mImageList.isEmpty() && positionImage < mImageList.size()) {
    // 设置适配器
    mViewPager.setAdapter(new ImagePagerAdapter());
    // 监听滑动事件
    mViewPager.addOnPageChangeListener(mOnPageChangeListener);
    // 设置当前显示坐标
    mViewPager.setCurrentItem(positionImage);
    // 设置指示器
    setIndicator();
    }
    }

    /**

    • 封装一个PhotoView图片浏览器
    • @param imageUrl 图片地址
    • @return
      */
      private PhotoView getPhotoView(String imageUrl) {
      // 创建一个PhotoView图片浏览器
      PhotoView pv = new PhotoView(this);
      // 设置开启缩放模式 更多操作参考:https://github.com/bm-x/PhotoView
      pv.enable();
      // 用Glide加载图片
      Glide.with(this).load(imageUrl).fitCenter().into(pv);
      // 设置点击关闭页面
      pv.setOnClickListener(new View.OnClickListener() {
      @OverRide
      public void onClick(View view) {
      finish();
      }
      });
      return pv;
      }

    /**

    • 监听ViewPager滑动事件
      */
      private OnPageChangeListener mOnPageChangeListener = new OnPageChangeListener() {

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

      }

      @OverRide
      public void onPageSelected(int position) {
      // 设置指示器
      setIndicator();
      }

      @OverRide
      public void onPageScrollStateChanged(int state) {

      }
      };

    /**

    • 设置指示器
      */
      private void setIndicator() {
      mTextView.setText((mViewPager.getCurrentItem() + 1) + " / " + mImageList.size());
      }

    /**

    • ViewPager适配器
      */
      class ImagePagerAdapter extends PagerAdapter {

      @OverRide
      public int getCount() {
      return mImageList.size();
      }

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

      @OverRide
      public Object instantiateItem(ViewGroup container, int position) {
      container.addView(mImageList.get(position));
      return mImageList.get(position);
      }

      @OverRide
      public void destroyItem(ViewGroup container, int position, Object object) {
      container.removeView(mImageList.get(position));
      }
      }

}

----------------------------------xml --------------------------------

<android.support.v4.view.ViewPager
    android:id="@+id/mViewPager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

<TextView
    android:id="@+id/mTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|center_horizontal"
    android:text="@string/app_name"
    android:textColor="@android:color/white"/>
@wmailn wmailn changed the title 开启缩放后图像显示很慢 开启缩放后,用Glide加载,图像显示很慢 Sep 12, 2017
@BlackC0
Copy link

BlackC0 commented Mar 7, 2018

猜测是不是请求太多 有没有懒加载

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants