今天按項目要求找了一個android的PDF控件衩婚,各種操作效果都非常好谭溉,在這里和大家分享一下月劈。
用法很簡單:
1.在build.gradle里面添加依賴?compile 'com.github.barteksc:android-pdf-viewer:2.8.0'
2.然后就可以直接在layout里面使用了
3.在Activity里面引用資源僧界,就可以打開pdf文件了迅矛,而且這個控件有超級多的方法棒坏,支持很多功能
? ? ? ?pdfView= findViewById(R.id.pdfview);
? ? ? String url="http://www8.cao.go.jp/okinawa/8/2012/0409-1-1.pdf";
? ? ? String name=url.split("/")[url.split("/").length-1];
? ? ? String pdfPath =Environment.getExternalStorageDirectory().getAbsolutePath() ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? +"/Android/data/com.demo/downloadPPT/"+name; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
? ? ? File file =newFile(pdfPath);
? ? ? ?if(file.exists()){ //如果文件存在直接打開??
? ? ? ? ? ? Uri uri = Uri.fromFile(file);
? ? ? ? ? ? ?pdfView.fromUri(uri)? ? ? ? ??
? ? ? ? ? ? ? ? ?.defaultPage(1)? ? ? ? ? ?
? ? ? ? ? ? ? ? .enableSwipe(true)? ? ? ??
? ? ? ? ? ? ? ? ?.load();
? ? ?}else{? ? ?
? ? ? ? ? ? ? showZpDialog("加載中...",2);
? ? ? ? ? ? ? downLoadFile(url,pdfPath);//下載PDF文件
? ? }
private void ?downLoadFile(String url, finalString pdfPath) { ? ? ? ? ? ? ? ? ? ? ?FileDownloader.getImpl().create(url).setWifiRequired(true).
setPath(pdfPath).setListener(newFileDownloadListener() {
@Override
protected void pending(BaseDownloadTask task, intsoFarBytes, inttotalBytes) {? ? ? ? }
@Override
protected void ?progress(BaseDownloadTask task, intsoFarBytes, inttotalBytes) {? ? ? ? }
@Override
protected void blockComplete(BaseDownloadTask task) {? ? ? ? }
@Override
protected void completed(BaseDownloadTask task) {? ? ? ? ??
? ? ? ?hideZpDialog();//隱藏加載框
? ? ? ? ?Toast.makeText(LookPPTActivity.this,"下載完成!",Toast.LENGTH_SHORT).show();
? ? ? ? ?File file =newFile(pdfPath);
? ? ? ? ?Uri uri = Uri.fromFile(file);
? ? ? ? ? ? ?pdfView.fromUri(uri)? ? ? ? ? ? ? ? ? ?
?.defaultPage(1)? ? ? ? ? ? ? ? ? ?
?.enableSwipe(true)? ? ? ? ? ? ? ? ? ??
.load();
}
@Override
protected void paused(BaseDownloadTask task, intsoFarBytes, inttotalBytes) {? ? ? ? }
@Override
protected void error(BaseDownloadTask task,Throwable e) {? ? ? ? ? ?
? ? ? ? hideZpDialog();
}
@Override?
protected void warn(BaseDownloadTask task) {}??
? }).start();
}