Cordova plugins explain
Cordova插件为我们提供了一个本地化的API, 供javascript调用, 能使我们很方便的使用javascript使用NativeApp的一些特性. 这里主要是记录一些比较常用切比较调用比较简单插件, 比较复杂的会另起篇幅进行说明.
各种loading(菊花)
pbernasconi/cordova-progressIndicator
详细文档: http://paolobernasconi.com/cordova-progressIndicator/
Install:1
2
3cordova plugin add org.pbernasconi.progressindicator
# or
phonegap local plugin add org.pbernasconi.progressindicator
Use:1
2
3ProgressIndicator.showSimple(true);
ProgressIndicator.showSimpleWithLabel(false, 'Loading...');
ProgressIndicator.showSimpleWithLabelDetail(false, 'Loading...', 'Refreshing list');
crosswalk引擎
webview引擎, 主要给安卓4.0~4.3提供一个统一的webview运行环境
Install1
cordova plugin add cordova-plugin-crosswalk-webview
Use:1
2
3
4
5cordova build android
// outputs
/path/to/hello/platforms/android/build/outputs/apk/android-x86-debug.apk
/path/to/hello/platforms/android/build/outputs/apk/android-armv7-debug.apk
控制台显示console.log
apache/cordova-plugin-console
没有安装此插件是无法输出javascript控制台日志的, 仅用于调试使用, 一般发布都会删除此插件
Install:1
cordova plugin add cordova-plugin-console
Use:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25document.addEventListener("deviceready", function () {
console.log("console.log works well");
console.error
console.exception
console.warn
console.info
console.debug
console.assert
console.dir
console.dirxml
console.time
console.timeEnd
console.table
// Partially supported Methods
console.group
console.groupCollapsed
// Not supported Methods
console.clear
console.trace
console.groupEnd
console.timeStamp
console.profile
console.profileEnd
console.count
}, false);
图片多选
Install:1
2
3phonegap plugin add https://github.com/wymsee/cordova-imagePicker.git
# or
cordova plugin add https://github.com/wymsee/cordova-imagePicker.git
Use:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15window.imagePicker.getPictures(
function(results) {
// success
for (var i = 0; i < results.length; i++) {
console.log('Image URI: ' + results[i]);
}
}, function (error) {
// error
console.log('Error: ' + error);
}, {
// options
maximumImagesCount: 10,
width: 800
}
);
微信分享
base64编码
hazemhagrass/phonegap-base64
warning: 依赖apache/cordova-plugin-device
Install:1
2
3
4
5phonegap local plugin add org.apache.cordova.device
phonegap local plugin add https://github.com/hazemhagrass/phonegap-base64.git
# or
cordova plugin add org.apache.cordova.device
cordova plugin add https://github.com/hazemhagrass/phonegap-base64.git
Use:1
2
3
4
5document.addEventListener('deviceready', function () {
window.plugins.Base64.encodeFile(filePath, function(base64){
console.log('file base64 encoding: ' + base64);
});
});
二维码扫描
照相机
Install:1
cordova plugin add cordova-plugin-camera
Use:1
2
3
4
5
6
7
8
9
10
11
12
13document.addEventListener('deviceready', function () {
navigator.camera.getPicture(function (imageURI) {
// success
document.getElementById('myImage').src = imageURI;
}, function (message) {
// error
alert('Failed because: ' + message);
}, {
// options
quality: 50,
destinationType: Camera.DestinationType.FILE_URI
});
}, false);
修改图片尺寸
protonet/cordova-plugin-image-resizer
Install:1
cordova plugin add https://github.com/protonet/cordova-plugin-image-resizer.git
Use:1
2
3
4
5
6
7
8
9
10
11
12
13
14window.ImageResizer.resize({
uri: imageUrl,
folderName: "Protonet Messenger",
quality: 90,
width: 120,
height: 120
},
function (image) {
document.getElementById('myImage').src = image;
},
function () {
// failed: grumpy cat likes this function
}
);