你不知道一些神奇Android Api
這將是一個關于Android Api的系列文章,AntSoft的Android 團隊一直緊隨技術前沿,在 Budapest University of Technology and Economics 培訓Android技術已經有8年多的時間。公司里有個傳統就是每周進行技術分享,這里將介紹一些Android平臺上有意思的API。
當前Android已經有了非常多可用的依賴庫(Library),但其實Android platform的一些API有些鮮為人知,但非常有用的方法和類,去研究一下這些API是非常有意思的。
我們知道Android API依賴的Java SE API也非常龐大,根據統計,Java SE 8有217個package,4240個方法,而java SE 7有209個package,4024個方法。
source : Java 8 Pocket Guide book by Robert Liguori, Patricia Liguor
demo App中給出的每個API的使用都是在不同的Activity中,從App首頁可以進入到不同的API demoActivity。
拼寫檢查
Android從level 14開始有一個檢查拼寫的API,可以通過 TextServicesManager 使用,從level16開始已經可以甚至可以檢查一個完整的句子了。
使用方法非常簡單,通過 TextServicesManager 可以創建 SpellCheckerSession :
TextServicesManager tsm = (TextServicesManager) getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE);
SpellCheckerSession spellCheckerSession = tsm.newSpellCheckerSession(null, null, this, true
可以通過實現SpellCheckerSessionListener接口得到檢查結果:
onGetSuggestions(SentenceSuggestionsInfo[] sentenceSuggestionsInfos)
onGetSentenceSuggestions(SentenceSuggestionsInfo[] sentenceSuggestionsInfos));
SentenceSuggestionsInfo 數據中保存了正確的文字、偏移量以及所有相關的信息。
文字識別
這是 Google Play Services Vision API 中提供的功能,可以通過gradle dependency非常簡單的引入到project中,需要注意的是不要引入整個 Play Services ,因為 Play Services 非常大,而我們需要的只是其中的一小部分, https://developers.google.com/android/guides/setup 中可以找到相關的幫助。
Vision API 中包含的服務有:
-
人臉識別
-
條形碼掃描
-
文字識別
使用 Text Recognizer API 非常簡單:
首先,在build.gradle中引入依賴:
compile 'com.google.android.gms:play-services-vision:10.0.1'
然后創建TextRecognizer對象:
TextRecognizer textRecognizer = new TextRecognizer.Builder(context).build();
之后實現 Detector.Processor 接口接口監聽結果,得到的結果是 TextBlock 數組。
public class OcrDetectorProcessor implements Detector.Processor<TextBlock> {
@Override
public void receiveDetections(Detector.Detections<TextBlock> detections) {
...
SparseArray<TextBlock> items = detections.getDetectedItems();
...
}
@Override
public void release() {
}
}
合理地使用 TextRecognizer ,一般要自定義包含 SurfaceView 的View用于在屏幕顯示結果。demo地址 OCRActivity , ocr 中有一些幫助類。
TimingLogger
TimingLogger 可以很容易地計算兩個log信息之間的時間差,如下所示:
D/TAG_MYJOB: MyJob: begin
D/TAG_MYJOB: MyJob: 2002 ms, Phase 1 ready
D/TAG_MYJOB: MyJob: 2002 ms, Phase 2 ready
D/TAG_MYJOB: MyJob: 2001 ms, Phase 3 ready
D/TAG_MYJOB: MyJob: end, 6005 ms
使用 TimingLogger :
TimingLogger timings = new TimingLogger("TAG_MYJOB", "MyJob");
然后通過 addSplit(...) 方法創建一個 log entry:
timings.addSplit("Phase 1 ready");
當使用 dumpToLog() 后,log信息就會打印出來:
timings.dumpToLog();
注意要使用TimingLogger, 要設置adb命令是Tag可用:
setprop log.tag.TAG_MYJOB VERBOSE
截屏
在某些情況下,截屏非常有用。也有一些第三方庫如 Falcon 實現這個功能,從level 21開始 MediaProjection 可以實時獲取屏幕內容和系統聲音信息流。
qi,有時使用標準的Android API通過 getWindow() 非常簡單地把屏幕內容保存為 Bitmap :
View viewRoot = getWindow().getDecorView().getRootView();
viewRoot.setDrawingCacheEnabled(true);
Bitmap screenShotAsBitmap = Bitmap.createBitmap(viewRoot.getDrawingCache());
viewRoot.setDrawingCacheEnabled(false);
// use screenShotAsBitmap as you need
PDF創建
從level 19開始Android支持本地內容生成PDF文件。
首先創建一個PageInfo new PdfDocument.PageInfo.Builder(w,h,pageNum).create() ; ,然后使用PDFDocument中的 startPage([pageInfo]) 就可以創建一個PDF文件了。
以下的代碼創建了一個demo.pdf文件:
public void createPdfFromCurrentScreen() {
new Thread() {
public void run() {
// Get the directory for the app's private pictures directory.
final File file = new File(
Environment.getExternalStorageDirectory(), "demo.pdf");
if (file.exists ()) {
file.delete ();
}
FileOutputStream out = null;
try {
out = new FileOutputStream(file);
PdfDocument document = new PdfDocument();
Point windowSize = new Point();
getWindowManager().getDefaultDisplay().getSize(windowSize);
PdfDocument.PageInfo pageInfo =
new PdfDocument.PageInfo.Builder(
windowSize.x, windowSize.y, 1).create();
PdfDocument.Page page = document.startPage(pageInfo);
View content = getWindow().getDecorView();
content.draw(page.getCanvas());
document.finishPage(page);
document.writeTo(out);
document.close();
out.flush();
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(PDFCreateActivity.this, "File created: "+file.getAbsolutePath(), Toast.LENGTH_LONG).show();
}
});
} catch (Exception e) {
Log.d("TAG_PDF", "File was not created: "+e.getMessage());
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}.start();
}
來自:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2017/0118/7051.html