Android内嵌PDF预览

一、在对应模块的build.gradle文件中加入依赖

dependencies {

    implementation 'com.github.barteksc:android-pdf-viewer:3.1.0-beta.1'

}

二、Activity布局Xml文件中,加入com.github.barteksc.pdfviewer.PDFView控件

  

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".PdfActivity">

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

</android.support.constraint.ConstraintLayout>

三、PDFView只能预览本地文件 如果是网络PDF还需要下载

  PDFView加载本地文件代码

/**
     * 查看PDF
     */
    private void SeePdf(File dest) {
        try {
            pdfView.setVisibility(View.VISIBLE);

pdfView.useBestQuality(false);
            Constants.Cache.CACHE_SIZE=40;
            pdfView.fromFile(dest).load(); 
} catch (Exception e) { e.printStackTrace(); } }

  下载PDF使用OKhttp:

/**
     * 开始下载PDF
     */
    private void DownloadPdf() {
        cacheUrl = getCacheDir().getAbsolutePath();//应用缓存路径
        pdfName = mPdfUrl.substring(mPdfUrl.lastIndexOf("/") + 1);//文件名称
        final File dest = new File(cacheUrl, pdfName);
        if (dest.exists()) {
            SeePdf(dest);
        } else {
            Request request = new Request.Builder().url(mPdfUrl).build();
            new OkHttpClient().newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    // 下载失败
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    Sink sink = null;
                    BufferedSink bufferedSink = null;
                    try {
                        if (!dest.exists()) {
                            boolean newFile = dest.createNewFile();
                        }
                        sink = Okio.sink(dest);
                        bufferedSink = Okio.buffer(sink);
                        bufferedSink.writeAll(response.body().source());
                        bufferedSink.close();
                        if (handler == null) {
                            handler = new Handler(Looper.getMainLooper());
                        }
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                SeePdf(dest);
                            }
                        });
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        if (bufferedSink != null) {
                            bufferedSink.close();
                        }

                    }
                }
            });
        }
    }

  自动翻页的实现:

   1、在PDFView的OnRenderListener实现翻页,handler.postDelayed来定时执行翻页方法

    

 pdfView.fromFile(dest).onRender(new OnRenderListener() {
                @Override
                public void onInitiallyRendered(int nbPages) {
                    if (pdf_trun_time != null) {
                        if (handler == null) {
                            handler = new Handler();
                        }
                        handler.postDelayed(goNextPageRunnable, pdf_trun_time);
                    }
                }
            })
                    .load();


 private Runnable goNextPageRunnable = new Runnable() {
        @Override
        public void run() {
            if (pdf_trun_time != null) {
                handler.postDelayed(this, pdf_trun_time);//设置循环时间,此处是5秒
                GoNextPage();
            }
        }
    };



 private void GoNextPage() {
        int totalPage = pdfView.getPageCount();
        int curPage = pdfView.getCurrentPage();
        int nextPage = 0;
        if (curPage < totalPage - 1) {
            nextPage = curPage + 1;
        } else {
            nextPage = 0;
        }

        pdfView.jumpTo(nextPage, true);
    }

 

posted @ 2018-09-21 14:45  _York  阅读(2485)  评论(0编辑  收藏  举报