1.手势
- java代码
package com.gst.user.myapplication;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import java.util.ArrayList;
public class GestureActivity extends AppCompatActivity implements GestureOverlayView.OnGesturePerformedListener{
private static final String TAG = "GestureActivity";
GestureOverlayView gestureOverlayView;
GestureLibrary gestureLibrary;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gesture);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
gestureOverlayView=(GestureOverlayView)findViewById(R.id.gestureOverlayView);
// gestureLibrary= GestureLibraries.fromRawResource(this, R.raw.gestures);
gestureLibrary=GestureLibraries.fromFile("/sdcard/mygestures");
if (gestureLibrary.load()) {
Toast.makeText(this,"加载文件成功",Toast.LENGTH_SHORT).show();
gestureOverlayView.addOnGesturePerformedListener(this);
}else {
Toast.makeText(this,"加载文件异常",Toast.LENGTH_SHORT).show();
}
}
@Override
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
ArrayList<Prediction> predictions=gestureLibrary.recognize(gesture);
if (predictions.size()>0) {
StringBuffer stringBuffer=new StringBuffer();
int n=0;
for (int i = 0; i < predictions.size(); i++) {
Prediction prediction=predictions.get(i);
if (prediction.score>1) {
stringBuffer.append("score:" + prediction.score + " name:"
+ prediction.name );
n++;
}
Log.d(TAG,"score:" + prediction.score + " name:"
+ prediction.name);
}
stringBuffer.insert(0,n + "个相匹配的手势.\n");
Toast.makeText(this,stringBuffer,Toast.LENGTH_SHORT).show();
}
}
}
- 布局文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:showIn="@layout/activity_gesture" tools:context="com.gst.user.myapplication.GestureActivity"> <android.gesture.GestureOverlayView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/gestureOverlayView" android:gestureStrokeType="multiple" android:layout_centerVertical="true" android:layout_alignParentStart="true"></android.gesture.GestureOverlayView> </RelativeLayout>
2.多点触摸
package com.gst.user.myapplication;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import com.lidroid.xutils.util.LogUtils;
public class MutiTouchActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_muti_touch);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getPointerCount()>=2&&event.getAction()==MotionEvent.ACTION_MOVE) {
int historySize=event.getHistorySize();
if (historySize==0){
return true;
}
float currentY1=event.getY(0);
float historyY1=event.getHistoricalY(0, historySize - 1);
float currentY2=event.getY(1);
float historyY2=event.getHistoricalY(1, historySize - 1);
float distance=Math.abs(currentY1 - currentY2);
float historyDistance=Math.abs(historyY1-historyY2);
if (distance > historyDistance) {
LogUtils.d("放大");
}else if (distance < historyDistance) {
LogUtils.d("缩小");
}else {
LogUtils.d("平行");
}
}
return super.onTouchEvent(event);
}
}
3.语音识别及TTS
- java代码
package com.gst.user.myapplication; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.speech.RecognizerIntent; import android.speech.tts.TextToSpeech; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.Locale; public class VoiceRecognizeActivity extends AppCompatActivity implements TextToSpeech.OnInitListener { private static final int RequestCode=1; private TextToSpeech textToSpeech; TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_voice_recognize); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); textView=(TextView)findViewById(R.id.textView); textToSpeech=new TextToSpeech(this,this); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode==RequestCode&&resultCode== Activity.RESULT_OK){ ArrayList<String> result=data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); if (result.size()>0){ Toast.makeText(VoiceRecognizeActivity.this,result.get(0),Toast.LENGTH_LONG).show(); } } super.onActivityResult(requestCode, resultCode, data); } public void onClickVoiceRecongnize(View view){ Intent intent=new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH); intent.putExtra(RecognizerIntent.EXTRA_PROMPT,"语音输入"); startActivityForResult(intent, RequestCode); } public void onClickTTS(View view){ textToSpeech.speak(textView.getText().toString(),TextToSpeech.QUEUE_FLUSH,null); } @Override public void onInit(int status) { if (status == TextToSpeech.SUCCESS) { int result=textToSpeech.setLanguage(Locale.US); if (result==TextToSpeech.LANG_MISSING_DATA ||result==TextToSpeech.LANG_NOT_SUPPORTED){ Toast.makeText(this, "Language is not available.", Toast.LENGTH_SHORT).show(); } } } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:showIn="@layout/activity_voice_recognize" tools:context="com.gst.user.myapplication.VoiceRecognizeActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/voice_recognize" android:id="@+id/button" android:onClick="onClickVoiceRecongnize" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="朗读" android:onClick="onClickTTS" android:id="@+id/button2" android:layout_below="@+id/button" android:layout_centerHorizontal="true" android:layout_marginTop="49dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="@string/text" android:id="@+id/textView" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> </RelativeLayout>