Android 中pdfView的使用

很多時候我們需要加載pdf,今天就來介紹一下pdfView這個控件,對於使用也很簡單。

效果圖:
這裏寫圖片描述

github地址:

https://github.com/barteksc/AndroidPdfViewer

添加依賴:

compile ‘com.github.barteksc:android-pdf-viewer:2.7.0-beta.1’

xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.administrator.pdfviewdemo.MainActivity">

    <com.github.barteksc.pdfviewer.PDFView
        android:id="@+id/pdfView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>


    <LinearLayout
        android:layout_width="match_parent"
        android:gravity="center"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/pageTv1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:textSize="15sp" />

        <TextView
            android:id="@+id/pageTv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:textSize="15sp" />

    </LinearLayout>

</LinearLayout>

MainActivity中:


public class MainActivity extends AppCompatActivity {

    private PDFView pdfView;
    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private TextView pageTv, pageTv1;
    private int p;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //獲取動態權限
        getPermission();

        pdfView = (PDFView) findViewById(R.id.pdfView);
        pageTv = (TextView) findViewById(R.id.pageTv);
        pageTv1 = (TextView) findViewById(R.id.pageTv1);

        final int myPage = (int) SPUtils.get(MainActivity.this, "page", 0);
        //選擇pdf
        pdfView.fromAsset("android.pdf")
//                .pages(0, 2, 3, 4, 5); // 把0 , 2 , 3 , 4 , 5 過濾掉
                //是否允許翻頁,默認是允許翻頁
                .enableSwipe(true)
                //pdf文檔翻頁是否是垂直翻頁,默認是左右滑動翻頁
                .swipeHorizontal(true)
                //
                .enableDoubletap(false)
                //設置默認顯示第0頁
                .defaultPage(myPage)
                //允許在當前頁面上繪製一些內容,通常在屏幕中間可見。
//                .onDraw(onDrawListener)
//                // 允許在每一頁上單獨繪製一個頁面。只調用可見頁面
//                .onDrawAll(onDrawListener)
                //設置加載監聽
                .onLoad(new OnLoadCompleteListener() {
                    @Override
                    public void loadComplete(int nbPages) {
                        pageTv.setText(nbPages + "");
                        pageTv1.setText(myPage +  "/");
                    }
                })
                //設置翻頁監聽
                .onPageChange(new OnPageChangeListener() {

                    @Override
                    public void onPageChanged(int page, int pageCount) {
                        p = page;
                        pageTv1.setText(page + "/");
                    }
                })
                //設置頁面滑動監聽
//                .onPageScroll(onPageScrollListener)
//                .onError(onErrorListener)
                // 首次提交文檔後調用。
//                .onRender(onRenderListener)
                // 渲染風格(就像註釋,顏色或表單)
                .enableAnnotationRendering(false)
                .password(null)
                .scrollHandle(null)
                // 改善低分辨率屏幕上的渲染
                .enableAntialiasing(true)
                // 頁面間的間距。定義間距顏色,設置背景視圖
                .spacing(0)
                .load();
    }

    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };


    private void getPermission() {
        int hasWriteContactsPermission = ContextCompat.checkSelfPermission(MainActivity.this,
                Manifest.permission.READ_EXTERNAL_STORAGE);
        if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
            if (!ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
                    Manifest.permission.READ_EXTERNAL_STORAGE)) {
                ActivityCompat.requestPermissions(MainActivity.this,
                        PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
            }

            ActivityCompat.requestPermissions(MainActivity.this,
                    PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
        }

        while ((ContextCompat.checkSelfPermission(MainActivity.this,
                Manifest.permission.READ_EXTERNAL_STORAGE)) != PackageManager.PERMISSION_GRANTED) {
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //當activity銷燬的時候,保存當前的頁數,下次打開的時候,直接翻到這個頁
        SPUtils.put(MainActivity.this, "page", p);
    }
}

這裏我們用sp保存了打開的pdf的當前頁數,當下次再進入的時候,直接顯示就是這個頁。

本人菜鳥一個,有什麼不對的地方希望大家指出評論,大神勿噴,希望大家一起學習進步!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章