• 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);
        }
  • 相关阅读:
    Javascript arguments.callee和caller的区别
    HTTP报错401和403详解及解决办法
    win10设置vscode的终端为管理员权限
    Vue2.0的三种常用传值方式、父传子、子传父、非父子组件传值
    Vue生命周期中mounted和created的区别
    Git可视化教程——Git Gui的使用
    url的三个js编码函数escape(),encodeURI(),encodeURIComponent()简介【转】
    在window系统上安装redis服务-Invalid argument during startup: Failed to open the .conf
    事务基础知识-->Spring事务管理
    线程本地变更,即ThreadLocal-->Spring事务管理
  • 原文地址:https://www.cnblogs.com/tangchun/p/9686474.html
Copyright © 2020-2023  润新知