Android(安卓)手势小试牛刀
16lz
2021-01-26
手势识别,是通过在具有触摸屏的手机上划出不同的手势来实现人机交互。比起以前的单一使用按键来操控,手势识别的方式能让用户获得更好的操作体验。android sdk中已经创建了一个gesture builder的方法(该方法能实现手势的添加,今天说到的程序就是在此基础上改进而来),这样就使得基于创建手势的gesture开发变得方便。
style =“@android:style/ButtonBar”//定义buttonbar的组件类型
android:layout_width =“match_parent”//宽度与父类一致
android:layout_height =“wrap_content”//高度能包住内容即可
android:orientation =“horizontal”>//走向为水平
<Button
android:id =“@+id/addButton“//新建addbutton的id,方便调用
android:onClick ="addGesture"
android:layout_width =“0dip“//dip是一种单位,是怎么样的长度不是很清楚
android:layout_height ="wrap_content"
android:layout_weight ="1"
</LinearLayout >
android:enabled="false"
android:text=“@string/button_add”/>//Button上书写的内容
< Button
android:id ="@+id/reloadButton"
android:onClick ="reloadGestures"
android:layout_width ="0dip"
android:layout_height ="wrap_content"
android:layout_weight ="1"
android:enabled ="false"
< ListView
android:id ="@android:id/list"
android:layout_width ="match_parent"
android:layout_height ="0dip"
android:layout_weight ="1.0" />
< TextView
android:id ="@android:id/empty"
android:layout_width ="match_parent"
android:layout_height ="0dip"
android:layout_weight ="1.0"
android:gravity =“center“ //本元素所有子元素的重力方向
android:text ="@string/gestures_loading"
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);//重写父类内容
setContentView(R.layout.gestures_list);//将gesture_list.xml对应的布局载入
mAdapter=newGesturesAdapter(this);//新建配适器对象
setListAdapter(mAdapter);//将配适器中所封装的内容用list形式表示出来
publicvoidreloadGestures(Viewv){
Intentintent=newIntent(this,GestureShowActivity.class);
startActivity(intent);
}
publicvoidaddGesture(Viewv){
Intentintent=newIntent(this,CreateGestureActivity.class);
startActivityForResult(intent,REQUEST_NEW_GESTURE);
}
< LinearLayout
xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:orientation ="horizontal" >
< ImageView android:id =“@+id/appicon” //显示应用程序的图标
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:layout_margin =“5px”/> //px是像素的意思
<LinearLayoutandroid:orientation ="vertical"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content" >
< TextView android:id ="@+id/appname"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:textColor ="#FFFFFFFF"
android:textSize ="22px" />
< TextView android:id ="@+id/pname"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:textColor ="#FFFFFFFF"
android:textSize ="13px" />
</ LinearLayout >
List < PackageInfo > packs = getPackageManager().getInstalledPackages();
for ( int i = 0 ;i < packs.size();i ++ ){
PackageInfop = packs.get(i);
if (( ! getSysPackages) && (p.versionName == null )){
continue ; // 使用arraylist方式通过getPackageManager().getInstalledPackages()来获得已经安装的程序
}
PInfonewInfo = new PInfo();
newInfo.appname = p.applicationInfo.loadLabel(getPackageManager()).toString();
newInfo.pname = p.packageName;
newInfo.appicon = p.applicationInfo.loadIcon(getPackageManager());
res.add(newInfo); // 获得程序名,包名,应用程序的图标
}
return res;
} CreateGestureActivity中通过监听器来获取手势
GestureOverlayViewoverlay = (GestureOverlayView)findViewById(R.id.gestures_overlay); // GestureOverlayView能在所在界面的最外层划出手势
overlay.addOnGestureListener( new GesturesProcessor()); // 设置监听器,能够记录划出手势的点和经过的路径,并保存 GestureShowActivity中识别手势并打开应用程序
public void onGesturePerformed(GestureOverlayViewoverlay,Gesturegesture){
ArrayListpredictions = mLibrary.recognize(gesture); // 使用了recognize的方法
// Wewantatleastoneprediction
if (predictions.size() > 0 ){ // 必须得有划动
Predictionprediction = (Prediction)predictions.get( 0 );
// Wewantatleastsomeconfidenceintheresult
if (prediction.score > 1.0 ){ // 有一定的相似程度
// Opentheapplication
StringpackageName = prediction.name;
try {
IntentmIntent = getPackageManager().getLaunchIntentForPackage(packageName);
startActivity(mIntent); // 通过getPackageManagerd中getLaunchIntentForPacksge的方法打开相对应的应用程序
}
catch (Exceptione){
Toast.makeText( this , " 查无此程序 " ,Toast.LENGTH_SHORT).show();
} // 不存在的话抛出异常
进入程序后的初始页面
添加一个手势之后的情况:
gesture_list.xml中定义两个button
< LinearLayoutstyle =“@android:style/ButtonBar”//定义buttonbar的组件类型
android:layout_width =“match_parent”//宽度与父类一致
android:layout_height =“wrap_content”//高度能包住内容即可
android:orientation =“horizontal”>//走向为水平
<Button
android:id =“@+id/addButton“//新建addbutton的id,方便调用
android:onClick ="addGesture"
android:layout_width =“0dip“//dip是一种单位,是怎么样的长度不是很清楚
android:layout_height ="wrap_content"
android:layout_weight ="1"
</LinearLayout >
android:enabled="false"
android:text=“@string/button_add”/>//Button上书写的内容
< Button
android:id ="@+id/reloadButton"
android:onClick ="reloadGestures"
android:layout_width ="0dip"
android:layout_height ="wrap_content"
android:layout_weight ="1"
android:enabled ="false"
android:text="@string/button_reload"/>
gesture_list.xml中创建list的页面布局< ListView
android:id ="@android:id/list"
android:layout_width ="match_parent"
android:layout_height ="0dip"
android:layout_weight ="1.0" />
< TextView
android:id ="@android:id/empty"
android:layout_width ="match_parent"
android:layout_height ="0dip"
android:layout_weight ="1.0"
android:gravity =“center“ //本元素所有子元素的重力方向
android:text ="@string/gestures_loading"
android:textAppearance=“?android:attr/textAppearanceMedium”/>//appearance为属性中定义的textAppearanceMedium
GestureBuilderActivity中使用配适器将手势的内容与对应显示在list中protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);//重写父类内容
setContentView(R.layout.gestures_list);//将gesture_list.xml对应的布局载入
mAdapter=newGesturesAdapter(this);//新建配适器对象
setListAdapter(mAdapter);//将配适器中所封装的内容用list形式表示出来
adapter的作用就是将要在列表内显示的数据和列表本身结合起来。列表本身只完成显示的作用,其实他就是继承自VIEWGROUP类。但是他又有一个独特的函数就是setAdapter()就是完成了view和adapter的结合。adapter如同其本身含义,其实就是一个适配器,他可以对要显示的数据进行统一的封装,主要是将数据变成view提供给list。
GestureBuilderActivity中通过intent实现到另外两个activity的跳转publicvoidreloadGestures(Viewv){
Intentintent=newIntent(this,GestureShowActivity.class);
startActivity(intent);
}
publicvoidaddGesture(Viewv){
Intentintent=newIntent(this,CreateGestureActivity.class);
startActivityForResult(intent,REQUEST_NEW_GESTURE);
}
注:后面关于页面之间的跳转都将用到intent
App_select.xml中的布局< LinearLayout
xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:orientation ="horizontal" >
< ImageView android:id =“@+id/appicon” //显示应用程序的图标
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:layout_margin =“5px”/> //px是像素的意思
<LinearLayoutandroid:orientation ="vertical"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content" >
< TextView android:id ="@+id/appname"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:textColor ="#FFFFFFFF"
android:textSize ="22px" />
< TextView android:id ="@+id/pname"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:textColor ="#FFFFFFFF"
android:textSize ="13px" />
</ LinearLayout >
</LinearLayout>
ApplicationSelecte中获取手机中已安装的程序并点选之后取得程序的基本信息
privateArrayList<PInfo>getInstalledApps(booleangetSysPackages){
ArrayList < PInfo > res = new ArrayList < PInfo > ();List < PackageInfo > packs = getPackageManager().getInstalledPackages();
for ( int i = 0 ;i < packs.size();i ++ ){
PackageInfop = packs.get(i);
if (( ! getSysPackages) && (p.versionName == null )){
continue ; // 使用arraylist方式通过getPackageManager().getInstalledPackages()来获得已经安装的程序
}
PInfonewInfo = new PInfo();
newInfo.appname = p.applicationInfo.loadLabel(getPackageManager()).toString();
newInfo.pname = p.packageName;
newInfo.appicon = p.applicationInfo.loadIcon(getPackageManager());
res.add(newInfo); // 获得程序名,包名,应用程序的图标
}
return res;
} CreateGestureActivity中通过监听器来获取手势
GestureOverlayViewoverlay = (GestureOverlayView)findViewById(R.id.gestures_overlay); // GestureOverlayView能在所在界面的最外层划出手势
overlay.addOnGestureListener( new GesturesProcessor()); // 设置监听器,能够记录划出手势的点和经过的路径,并保存 GestureShowActivity中识别手势并打开应用程序
public void onGesturePerformed(GestureOverlayViewoverlay,Gesturegesture){
ArrayListpredictions = mLibrary.recognize(gesture); // 使用了recognize的方法
// Wewantatleastoneprediction
if (predictions.size() > 0 ){ // 必须得有划动
Predictionprediction = (Prediction)predictions.get( 0 );
// Wewantatleastsomeconfidenceintheresult
if (prediction.score > 1.0 ){ // 有一定的相似程度
// Opentheapplication
StringpackageName = prediction.name;
try {
IntentmIntent = getPackageManager().getLaunchIntentForPackage(packageName);
startActivity(mIntent); // 通过getPackageManagerd中getLaunchIntentForPacksge的方法打开相对应的应用程序
}
catch (Exceptione){
Toast.makeText( this , " 查无此程序 " ,Toast.LENGTH_SHORT).show();
} // 不存在的话抛出异常
}
注:本文参加
“第二届 Google 暑期大学生博客分享大赛 - 2011 Android 成长篇”更多相关文章
- Android新浪星座运势程序开发
- Android程序开发入门——进程生命周期
- Android(安卓)API Demo程序框架
- Android-support-v4 v7 v8 v13 v17 的区别和特性说明
- [转]修改Android的自带程序“屏幕锁”
- Android(安卓)FrameWork框架和它在android的四层架构起到的作用
- 试读《Android软件安全与逆向分析》
- android修改默认桌面程序
- 跟Google学习Android开发-起始篇-保存数据(1)