是否可以在Android视图中显示巨大的pdf文件?
我只需要能够显示它,使用捏合和缩放以及最近在ImageViews中实现的其他常规功能。
答案 0 :(得分:7)
有三个开源库,它们结合起来,可以做你想要的。所有都获得Apache V2(或Beer-Ware许可证)的许可!
请注意,V21或以上是必需的。但是,如果需要,您可以使用if语句和Intent作为后备。
如何实现这一点的示例在解码器库的示例中给出。
repositories {
mavenCentral()
jcenter()
//decoder library, will soon be on jcenter
maven { url 'https://dl.bintray.com/n42/maven'}
}
dependencies {
compile 'com.davemorrissey.labs:subsampling-scale-image-view:3.5.0'
compile 'com.github.castorflex.verticalviewpager:library:19.0.1'
compile 'de.number42:subsampling-pdf-decoder:0.1.0@aar'
}
public class PDFPagerAdapter extends PagerAdapter {
/**
* context for the view
*/
private Context context;
/**
* pdf file to show
*/
private File file;
/**
* file descriptor of the PDF
*/
private ParcelFileDescriptor mFileDescriptor;
/**
* this scale sets the size of the {@link PdfRenderer.Page} in the {@link
* PDFRegionDecoder}.
* Since it rescales the picture, it also sets the possible zoom level.
*/
private float scale;
/**
* this renderer is only used to count the pages
*/
private PdfRenderer renderer;
/**
* @param file the pdf file
*/
public PDFPagerAdapter(Context context, File file) {
super();
this.context = context;
this.file = file;
this.scale = 8;
try {
mFileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
renderer = new PdfRenderer(mFileDescriptor);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* Instantiate an item. Therefor a {@link SubsamplingScaleImageView} with special decoders is
* initialized and rendered.
*
* @param container isn't used here
* @param position the current pdf page position
*/
public Object instantiateItem(ViewGroup container, int position) {
SubsamplingScaleImageView imageView = new SubsamplingScaleImageView(context);
// the smaller this number, the smaller the chance to get an "outOfMemoryException"
// still, values lower than 100 really do affect the quality of the pdf picture
int minimumTileDpi = 120;
imageView.setMinimumTileDpi(minimumTileDpi);
//sets the PDFDecoder for the imageView
imageView.setBitmapDecoderFactory(() -> new PDFDecoder(position, file, scale));
//sets the PDFRegionDecoder for the imageView
imageView.setRegionDecoderFactory(() -> new PDFRegionDecoder(position, file, scale));
ImageSource source = ImageSource.uri(file.getAbsolutePath());
imageView.setImage(source);
container.addView(imageView);
return imageView;
}
/**
* gets the pdf site count
*
* @return pdf site count
*/
public int getCount() {
return renderer.getPageCount();
}
@Override public void destroyItem(ViewGroup container, int position, Object view) {
container.removeView((View) view);
}
@Override public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
VerticalViewPager pager = (VerticalViewPager) findViewById(R.id.pager);
File pdfFile = ....
PDFPagerAdapter pagerAdapter = new PDFPagerAdapter(this,pdfFile);
pager.setAdapter(pagerAdapter);
答案 1 :(得分:1)
您可以使用webview,这很容易实现。这样做
@SuppressLint("NewApi")
private void startWebView(String url) {
// Create new webview Client to show progress dialog
// When opening a url or click on link
// Javascript inabled on webview
web.getSettings().setJavaScriptEnabled(true);
web.getSettings().setBuiltInZoomControls(true);
web.getSettings().setDisplayZoomControls(false);
web.getSettings().setLoadWithOverviewMode(true);
web.getSettings().setUseWideViewPort(true);
web.getSettings().setDomStorageEnabled(true);
web.setWebChromeClient(new WebChromeClient());
// web.getSettings().setPluginState(WebSettings.PluginState.ON);
web.setWebViewClient(new WebViewClient() {
ProgressDialog progressDialog;
// If you will not use this method url links are opeen in new brower
// not in webview
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@SuppressWarnings("deprecation")
@Override
public void onReceivedError(WebView view, int errorCode, String description, final String failingUrl) {
web.loadUrl("about:blank");
}
// Show loader on url load
public void onLoadResource(WebView view, String url) {
/* if (progressDialog == null) {
// in standard case YourActivity.this
if (getActivity() != null) {
progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("Loading...");
progressDialog.show();
}
}*/
}
public void onPageFinished(WebView view, String url) {
try {
setProgressBar(false);
} catch (Exception e) {
}
}
});
// Load url in webview
web.loadUrl(url);
}