经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android内嵌PDF预览
来源:cnblogs  作者:_York  时间:2018/9/28 16:48:51  对本文有异议

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

  1. dependencies {
  2. implementation 'com.github.barteksc:android-pdf-viewer:3.1.0-beta.1'
  3. }

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

  

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".PdfActivity">
  8.  
  9. <com.github.barteksc.pdfviewer.PDFView
  10. android:id="@+id/pdfView"
  11. android:layout_width="match_parent"
  12. android:layout_height="match_parent" />
  13.  
  14. </android.support.constraint.ConstraintLayout>

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

  PDFView加载本地文件代码

  1. /**
  2. * 查看PDF
  3. */
  4. private void SeePdf(File dest) {
  5. try {
  6. pdfView.setVisibility(View.VISIBLE);

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

  下载PDF使用OKhttp:

  1. /**
  2. * 开始下载PDF
  3. */
  4. private void DownloadPdf() {
  5. cacheUrl = getCacheDir().getAbsolutePath();//应用缓存路径
  6. pdfName = mPdfUrl.substring(mPdfUrl.lastIndexOf("/") + 1);//文件名称
  7. final File dest = new File(cacheUrl, pdfName);
  8. if (dest.exists()) {
  9. SeePdf(dest);
  10. } else {
  11. Request request = new Request.Builder().url(mPdfUrl).build();
  12. new OkHttpClient().newCall(request).enqueue(new Callback() {
  13. @Override
  14. public void onFailure(Call call, IOException e) {
  15. // 下载失败
  16. }
  17. @Override
  18. public void onResponse(Call call, Response response) throws IOException {
  19. Sink sink = null;
  20. BufferedSink bufferedSink = null;
  21. try {
  22. if (!dest.exists()) {
  23. boolean newFile = dest.createNewFile();
  24. }
  25. sink = Okio.sink(dest);
  26. bufferedSink = Okio.buffer(sink);
  27. bufferedSink.writeAll(response.body().source());
  28. bufferedSink.close();
  29. if (handler == null) {
  30. handler = new Handler(Looper.getMainLooper());
  31. }
  32. handler.post(new Runnable() {
  33. @Override
  34. public void run() {
  35. SeePdf(dest);
  36. }
  37. });
  38. } catch (Exception e) {
  39. e.printStackTrace();
  40. } finally {
  41. if (bufferedSink != null) {
  42. bufferedSink.close();
  43. }
  44. }
  45. }
  46. });
  47. }
  48. }

  自动翻页的实现:

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

    

  1. pdfView.fromFile(dest).onRender(new OnRenderListener() {
  2. @Override
  3. public void onInitiallyRendered(int nbPages) {
  4. if (pdf_trun_time != null) {
  5. if (handler == null) {
  6. handler = new Handler();
  7. }
  8. handler.postDelayed(goNextPageRunnable, pdf_trun_time);
  9. }
  10. }
  11. })
  12. .load();
  13. private Runnable goNextPageRunnable = new Runnable() {
  14. @Override
  15. public void run() {
  16. if (pdf_trun_time != null) {
  17. handler.postDelayed(this, pdf_trun_time);//设置循环时间,此处是5秒
  18. GoNextPage();
  19. }
  20. }
  21. };
  22. private void GoNextPage() {
  23. int totalPage = pdfView.getPageCount();
  24. int curPage = pdfView.getCurrentPage();
  25. int nextPage = 0;
  26. if (curPage < totalPage - 1) {
  27. nextPage = curPage + 1;
  28. } else {
  29. nextPage = 0;
  30. }
  31. pdfView.jumpTo(nextPage, true);
  32. }

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号