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();
    }

}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM