Steptember 2016-07-07 07:19 采纳率: 0%
浏览 1193

安卓ViewPager卡顿问题

ViewPager加载了五张大图,每张图都是2M,左右翻页会很卡很卡,该怎么解决

  • 写回答

1条回答 默认 最新

  • cjh13267196337 2016-07-07 07:50
    关注

    是本地图片吗?可以先展示缩略图,再展示原始图。加载都在子线程中进行。

    读取缩略图的方法:

      /**
         * 通过限制加载图片的宽度和高度,限制不加载超大图片
         *
         * @param dst    图片文件
         * @param width  宽度
         * @param height 高度
         */
        public static Bitmap getBitmapFromFile(File dst, int width, int height) {
            if (null != dst && dst.exists()) {
                BitmapFactory.Options opts = null;
                if (width > 0 && height > 0) {
                    opts = new BitmapFactory.Options();
                    //设置inJustDecodeBounds为true后,decodeFile并不分配空间,此时计算原始图片的长度和宽度
                    opts.inJustDecodeBounds = true;
                    BitmapFactory.decodeFile(dst.getPath(), opts);
                    // 计算图片缩放比例
                    final int minSideLength = Math.min(width, height);
                    opts.inSampleSize = computeSampleSize(opts, minSideLength, width * height);
                    //这里一定要将其设置回false,因为之前我们将其设置成了true
                    opts.inJustDecodeBounds = false;
                    opts.inInputShareable = true;
                    opts.inPurgeable = true;
                }
                try {
                    return BitmapFactory.decodeFile(dst.getPath(), opts);
                } catch (OutOfMemoryError e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
    
        private static int computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
            int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels);
    
            int roundedSize;
            if (initialSize <= 8) {
                roundedSize = 1;
                while (roundedSize < initialSize) {
                    roundedSize <<= 1;
                }
            } else {
                roundedSize = (initialSize + 7) / 8 * 8;
            }
    
            return roundedSize;
        }
    
        private static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
            double w = options.outWidth;
            double h = options.outHeight;
    
            int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math
                    .sqrt(w * h / maxNumOfPixels));
            int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(Math
                    .floor(w / minSideLength), Math.floor(h / minSideLength));
    
            if (upperBound < lowerBound) {
                // return the larger one when there is no overlapping zone.
                return lowerBound;
            }
    
            if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
                return 1;
            } else if (minSideLength == -1) {
                return lowerBound;
            } else {
                return upperBound;
            }
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配