zoukankan      html  css  js  c++  java
  • ImageView加载长图(适用不需要缩放的情况)

     此案例适用于加载网络长图且图片的宽和高已知的情况。由于ImageView加载图片有一个4096*4096的限制,所以对于巨长图的加载比较麻烦,需要我们自己去手动处理。

    有两种解决方案:第一种就是比较low的方式用WebView,将其设置为自适应屏幕,接下来重点说说第二种方式,手动压缩图片,图片加载框架我用的是Fresco。

    首先贴出xml布局文件:

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="15dp"
                android:text="123" />
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="15dp"
                android:text="123" />
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="15dp"
                android:text="123" />
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="15dp"
                android:text="123" />
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="15dp"
                android:text="123" />
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="15dp"
                android:text="123" />
    
    
            <com.facebook.drawee.view.SimpleDraweeView
                android:id="@+id/pic"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
    
    
        </LinearLayout>
    
    </ScrollView>
    

      接下来是代码:

    public class MainActivity extends AppCompatActivity {
    
        SimpleDraweeView simpleDraweeView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Fresco.initialize(this);
            setContentView(R.layout.activity_main);
    
            //这个图片的原始宽高你想办法拿到
            int width = 1242;
            int height = 9668;
    
            simpleDraweeView = (SimpleDraweeView) findViewById(R.id.pic);
            simpleDraweeView.setAspectRatio(div(width, height, 2));//设置view的大小,fresco是通过宽高比设置大小的
    
            //加载图片的过程
            Uri uri = Uri.parse("http://xxxxxxxxxxxxxxxxxxxxxxxx.jpeg");
            ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                    .setProgressiveRenderingEnabled(true)
                    .setResizeOptions(getResize(width, height))//获取合适的大小
                    .build();
            DraweeController controller = Fresco.newDraweeControllerBuilder()
                    .setImageRequest(request)
                    .setOldController(simpleDraweeView.getController())
                    .build();
            simpleDraweeView.setController(controller);
        }
    
        int MaxSize = 4096;//图片的最大宽高
    
        /**
         * 获取一个合适的大小
         * @param width
         * @param height
         * @return
         */
        private ResizeOptions getResize(int width, int height) {
            int max = Math.max(width, height);
            while (max > MaxSize) {//循环,宽高除2
                width /= 2;
                height /= 2;
                max = Math.max(width, height);
            }
            return new ResizeOptions(width, height);
        }
    
        /**
         * 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指
         * 定精度,以后的数字四舍五入。
         *
         * @param v1    被除数
         * @param v2    除数
         * @param scale 表示表示需要精确到小数点以后几位。
         * @return 两个参数的商
         */
        public static float div(double v1, double v2, int scale) {
            if (scale < 0) {
                throw new IllegalArgumentException(
                        "The scale must be a positive integer or zero");
            }
            BigDecimal b1 = new BigDecimal(Double.toString(v1));
            BigDecimal b2 = new BigDecimal(Double.toString(v2));
            return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).floatValue();
        }
    
    }
    

      

  • 相关阅读:
    Windows 8.1 Visual Studio 2013 OpenGL 配置
    panic 和 recover的区别
    Beego操作数据库
    sql 中 inner join、left join 和 right join的区别
    Go实现网页爬虫
    SqlServer中 不区分大小写 和 全半角的写法
    循环语句
    switch语句
    iota枚举
    关于Go开发工具(LiteIDE)
  • 原文地址:https://www.cnblogs.com/yegong0214/p/7131592.html
Copyright © 2011-2022 走看看