Android 中pdfView的使用
来源:互联网 发布:虚拟视频软件电脑 编辑:程序博客网 时间:2024/06/06 18:15
很多时候我们需要加载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的当前页数,当下次再进入的时候,直接显示就是这个页。
本人菜鸟一个,有什么不对的地方希望大家指出评论,大神勿喷,希望大家一起学习进步!
阅读全文
1 0
- Android 中pdfView的使用
- Android PDFView
- android 对pdf文件的下载、缓存、显示,包含android-pdfview框架使用教程
- Android PDF开发:android-pdfview
- Android PDF开发:android-pdfview
- Android PDF开发:android-pdfview
- Android 查看pdf文档——PDFView
- Android 查看pdf文档——PDFView
- Android中ListActivity的使用
- Android 中LayoutInflater的使用
- Android中ExpandableListView的使用
- Android中Handler的使用
- android中ProgressBar的使用
- Android中ListView的使用
- android中ImageButton的使用
- Android中Spinner的使用
- Android中ContextMenu的使用
- Android中Preference的使用
- [Java注意点]
- 抽象类与抽象方法
- JavaScript难点——变量提升和函数提升(三)
- 高德地图定位
- IJKPlayer如何支持https
- Android 中pdfView的使用
- 简单页面+java后台+数据库,实现从页面对数据库的增删改查
- 简单验证码的识别(matlab实现)
- Android WiFi从app到driver详解
- 拓扑排序简介+hdu4857逃生(逆向建图+拓扑排序)
- 十条不错的编程观点
- GitHub学习-----在github上传已有项目---(二)
- HTTP1.0,HTTP1.1,HTTP2.0,SPDY,HTTPS你应该知道的一些事
- Nuget 安装dll 版本