Android PDF開發:android-pdfview
阿新 • • 發佈:2021-12-10
Android PDF開發:android-pdfview
同步滾動:Android PDF開發:android-pdfview
android-pdfview使用比較簡單,關鍵的地方是PDFView,將PDFView作為像Android的ImageView或者TextView一樣寫進xml佈局檔案:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.joanzapata.pdfview.PDFView
android:id="@+id/pdfView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
然後在Java上層程式碼直接載入pdf檔案資源裝載進去即可:
package zhangphil.pdfview;
import com.joanzapata.pdfview.PDFView;
import com.joanzapata.pdfview.listener.OnPageChangeListener;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PDFView pdfView = (PDFView) findViewById(R.id.pdfView);
// 在我這個測試例子中,事先準備一個叫做sample.pdf的pdf大檔案放到assets目錄下。
// 從assets檔案目錄下讀取名為 sample.pdf的檔案,預設把該pdf定位到第一頁。
pdfView.fromAsset("sample.pdf").defaultPage(1).onPageChange(new OnPageChangeListener() {
@Override
public void onPageChanged(int page, int pageCount) {
// 當用戶在翻頁時候將回調。
Toast.makeText(getApplicationContext(), page + " / " + pageCount, Toast.LENGTH_SHORT).show();
}
}).load();
}
}