Android高级部分第四天。
今天学习了应用程序的真正入口,是application,同时,我们可以自己定义一个MyApplication,并继承Application,并在清单中将application的name设置为我们所设置的application。这个application是全局的东西,我们在任意一个Activity中都可以调用application上面的方法、属性。
同时,也学习了手势的使用,如何使用手势,步骤如下:
1. 使用GestureBuilder录制手势,并从SD卡拷贝gestures文件到res/raw/目录
2. 加载手势库
GestureLibrary library =
GestureLibraries.fromRawResource(this, R.raw.gestures);
library.load(); // 加载手势到内存
3. 在布局中增加一个android.gesture.GestureOverlayView
4. 监听用户输入的手势
GestureOverlayView overlayView = (GestureOverlayView) findViewById(R.id.gestureOverlayView1);
overlayView.addOnGesturePerformedListener(new OnGesturePerformedListener()
{
@Override
public void onGesturePerformed(GestureOverlayView overlay,
Gesture gesture)
{
ArrayList result = library.recognize(gesture);
Prediction prediction = result.get(0);
if (prediction.score >= 5.0)
{
if ("toast".equals(prediction.name))
{
ToastUtils.showCustomToast(MainActivity.this, "Toast手势");
}
else if ("new".equals(prediction.name))
{
}
else if ("exit".equals(prediction.name))
{
finish();
}
}
else
{
ToastUtils.showCustomToast(MainActivity.this, "无效手势");
}
}
});
还有传感器Sensor的使用。使用方法步骤如下:
1) 获取传感器管理器和某个传感器对象
在onCreate()方法中
mSensorMgr = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSensor = mSensorMgr.getDefaultSensor(Sensor.TYPE_ORIENTATION);
2) 在onResume()方法中注册传感器事件
mSensorMgr.registerListener(mListener, mSensor,
SensorManager.SENSOR_DELAY_UI);
其中SensorManager.SENSOR_DELAY是传感器数据采集频率
应用程序使用SensorManager.SENSOR_DELAY_UI,游戏使用DELAY_GAME
3) 在Sensor事件中获得传感器的数据
private final SensorEventListener mListener = new SensorEventListener()
{
public void onSensorChanged(SensorEvent event)
{
if (Config.LOGD)
Log.d(TAG, "sensorChanged (" + event.values[0] + ", "
+ event.values[1] + ", " + event.values[2] + ")");
mValues = event.values;
if (mView != null)
{
mView.invalidate();
}
}
并学习了NDK的使用,如何调用c/C++的这些底层的东西。
JNI的步骤:
1) 在JAVA中声明native方法
2) 在DOS中执行javah命令,生成.h头文件
3) C++工程师实现.h文件中的方法,并编译生成库文件
在windows中:dll,dynamic linked library
在Linux中生成的库:.so文件
4) 在JAVA代码中加载库文件
static
{
System.loadLibrary("libname");
}