Android 手势小试牛刀

手势识别,是通过在具有触摸屏的手机上划出不同的手势来实现人机交互。比起以前的单一使用按键来操控,手势识别的方式能让用户获得更好的操作体验。android sdk中已经创建了一个gesture builder的方法(该方法能实现手势的添加,今天说到的程序就是在此基础上改进而来),这样就使得基于创建手势的gesture开发变得方便。

进入程序后的初始页面

 

添加一个手势之后的情况 :

 

 

 gesture_list.xml中定义两个button

<LinearLayout
        
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"           

            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中
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);//重写父类内容
        setContentView(R.layout.gestures_list);//将gesture_list.xml对应的布局载入
        mAdapter = new GesturesAdapter(this);//新建配适器对象
        setListAdapter(mAdapter);//将配适器中所封装的内容用list形式表示出来

adapter的作用就是将要在列表内显示的数据和列表本身结合起来。列表本身只完成显示的作用,其实他就是继承自VIEWGROUP 类。但是他又有一个独特的函数就是setAdapter()就是完成了view和adapter的结合。adapter如同其本身含义,其实就是一个适配器,他可以对要显示的数据进行统一的封装,主要是将数据变成view提供给list。

GestureBuilderActivity中通过intent实现到另外两个activity的跳转
public void reloadGestures(View v) {
        Intent intent = new Intent(this,        GestureShowActivity.class);
        startActivity(intent);
    }
    
public void addGesture(View v) {
        Intent intent = new Intent(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是像素的意思         
         <LinearLayout android: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中获取手机中已安装的程序并点选之后取得程序的基本信息

 private ArrayList<PInfo> getInstalledApps(boolean getSysPackages) {     

        ArrayList<PInfo> res = new ArrayList<PInfo>();             
        List
<PackageInfo> packs = getPackageManager().getInstalledPackages();     
        
for(int i=0;i<packs.size();i++) {     
            PackageInfo p 
= packs.get(i);     
            
if ((!getSysPackages) && (p.versionName == null)) {     
                
continue ;     //使用arraylist方式通过getPackageManager().getInstalledPackages()来获得已经安装的程序
            }     
            PInfo newInfo 
= 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中通过监听器来获取手势

GestureOverlayView overlay 
= (GestureOverlayView) findViewById(R.id.gestures_overlay);   //GestureOverlayView能在所在界面的最外层划出手势
        overlay.addOnGestureListener(new GesturesProcessor());//设置监听器,能够记录划出手势的点和经过的路径,并保存

 

GestureShowActivity中识别手势并打开应用程序

public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {  
        ArrayList predictions 
= mLibrary.recognize(gesture);          //  使用了recognize的方法
  
        
// We want at least one prediction  
        if (predictions.size() > 0) {                    //必须得有划动
            Prediction prediction = (Prediction) predictions.get(0);  
            
// We want at least some confidence in the result  
            if (prediction.score > 1.0) {                //有一定的相似程度
                
// Open the application  
                String packageName = prediction.name;
                
try{
                Intent mIntent 
= getPackageManager() .getLaunchIntentForPackage(packageName) ;
                startActivity(mIntent);                   
// 通过getPackageManagerd中getLaunchIntentForPacksge的方法打开相对应的应用程序
                }
                
catch(Exception e){
                Toast.makeText (
this,"查无此程序",Toast.LENGTH_SHORT).show();
                }          
//不存在的话抛出异常

            } 

注:本文参加 

第二届 Google 暑期大学生博客分享大赛 - 2011 Android 成长篇 ”

 

posted @ 2011-08-12 02:30  Just for Fun  阅读(398)  评论(0编辑  收藏  举报