简单的图片浏览器和手势识别的功能实现
1.首先上布局
--------1.main.xml
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridphoto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="5dp"
android:horizontalSpacing="5dp"
android:columnWidth="90dp"
android:gravity="center">
</GridView>
----------2.photo.xml
<?xml version="1.0" encoding="utf-8"?>
<android.gesture.GestureOverlayView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:id="@+id/mygesture">"
<ImageSwitcher
android:id="@+id/switcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
/>
</android.gesture.GestureOverlayView>
2.接着咱就上源码
--------1.GridViewActivity.java
package com.wang;
import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
public class GridViewActivity extends Activity {
//存放图片资源
private int images[]={R.drawable.p1,R.drawable.p2,R.drawable.p3,R.drawable.p4,R.drawable.p5,
R.drawable.p6,R.drawable.p7,R.drawable.p8,R.drawable.p9,R.drawable.p10,
R.drawable.p11,R.drawable.p12,R.drawable.p13,R.drawable.p14,R.drawable.p15,
R.drawable.p16,R.drawable.p17,R.drawable.p18,R.drawable.p19,R.drawable.p20,
R.drawable.p21};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//加载子啊lyout中添加GridView的控件
GridView photos=(GridView)findViewById(R.id.gridphoto);
photos.setAdapter(new photosAdapter(this));
photos.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id) {
// 跳转Activity 将图片的Id传给他
Intent intent=new Intent(GridViewActivity.this, photos.class);
intent.putExtra("position", position);
startActivity(intent);
}
});
}
// 继承BaseAdapter 添加图片的参数
public class photosAdapter extends BaseAdapter{
private Context context;
public photosAdapter (Context c)
{
context=c;
}
public int getCount() {
// TODO Auto-generated method stub
return images.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView==null) {
// 上下左右的边框的宽度
imageView=new ImageView(context);
imageView.setPadding(4, 4, 4, 4);
//图片的显示方法(拉伸,居中,适应。等)
imageView.setLayoutParams(new GridView.LayoutParams(85,100));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
} else {
imageView=(ImageView)convertView;
}
imageView.setImageResource(images[position]);
return imageView;
}
}
}
-----2.photos.java
package com.wang;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.gesture.GestureOverlayView.OnGestureListener;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.os.Bundle;
import android.provider.MediaStore.Images;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewSwitcher.ViewFactory;
public class photos extends Activity implements ViewFactory, OnGesturePerformedListener{
private ImageSwitcher myImageSwitcher;
//记录当前的索引值
private int currentpostion;
/*
* 图片的数量,实际应用中我们可以再GridView中,
* 通过Adapter中的getCount()来获得,然后通过intent 传过来
* */
private static final int MAXSIZE=20;
GestureLibrary myGestureLibrary;
private int images[]={R.drawable.p1,R.drawable.p2,R.drawable.p3,R.drawable.p4,R.drawable.p5,
R.drawable.p6,R.drawable.p7,R.drawable.p8,R.drawable.p9,R.drawable.p10,
R.drawable.p11,R.drawable.p12,R.drawable.p13,R.drawable.p14,R.drawable.p15,
R.drawable.p16,R.drawable.p17,R.drawable.p18,R.drawable.p19,R.drawable.p20,
R.drawable.p21};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 取消标题的显示
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.photo);
myImageSwitcher=(ImageSwitcher)findViewById(R.id.switcher);
//调用setFactory()方法,创建俩个图层,为ImageSwitcher过渡时使用
myImageSwitcher.setFactory((ViewFactory) this);
// 设置文件出现的时候的动画效果
myImageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left));
// 设置文件消失时候的动画效果
myImageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right));
//获取layout 中是手势图层
GestureOverlayView gestureOverlayView=(GestureOverlayView)findViewById(R.id.mygesture);
//设置图层监听
gestureOverlayView.addOnGesturePerformedListener(this);
//加载以创建的文件的手势
myGestureLibrary =GestureLibraries.fromRawResource(this,R.raw.gestures);
// 检测手势是否正确
if (!myGestureLibrary.load()) {
Toast.makeText(photos.this, "读取文件手势出错,程序可能无法正常运行", Toast.LENGTH_LONG).show();
}
// 获取携带的意图
Intent intent =getIntent();
int index=intent.getIntExtra("position", 0);
currentpostion=index;
//通过传递过来的值,设定当前所需要显示的图片
myImageSwitcher.setImageResource(images[index]);
}
// 大图显示
public View makeView(){
ImageView i=new ImageView(this);
i.setBackgroundColor(0xFF000000);
// 设置图片显示的格式
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
//设置全局显示
i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
return i;
}
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
// 将用户已经建好的手势与用户的手势进行对比,后存放在list中
ArrayList<Prediction> predictions=myGestureLibrary.recognize(gesture);
if (predictions.size()>0) {
Prediction prediction=(Prediction) predictions.get(0);
//判断是否相似
if (prediction.score>1.0) {
if ("R".equals(prediction.name)) {
//判断是否是最后一张图片,用于提示用户
if (currentpostion==MAXSIZE) {
Toast.makeText(photos.this, "已经显示到最后一张了", Toast.LENGTH_LONG).show();
}else {
++currentpostion;
myImageSwitcher.setImageResource(images[currentpostion]);
}
}
//在第一张的时候,同样需要判断,如果是第一张,提示一下
if ("Leftrun".equals(prediction.name)) {
//判断是否是第一张图片,用于提示用户
if (currentpostion==0) {
Toast.makeText(photos.this, "已经显示到第一张了", Toast.LENGTH_LONG).show();
}else {
--currentpostion;
myImageSwitcher.setImageResource(images[currentpostion]);
}
}
}
}
}
}
3.亲、别忘了加Activity的权限哦!
<activity android:name=".photos"
android:label="@string/photos" > </activity>
</application>
4.最重要的手势识别,GestureBuilder 手势的定义
关于解决GestureBuilder 的方法如下 1.6以上的版本都自带有GestureBuilder ,我用的是2.3.3的版本位置如下
我的是在H:\android\android-sdk_r10-windows\android-sdk-windows\samples\android-13
不过找到后并不可以用,必须先建立一个android的工程 然后把GestureBuilder 复制到所建的工程中,运行一下会有错,解决方法如下,cmd进入dos下进入自己的platform-tools 我的是H:\android\android-sdk_r10-windows\android-sdk-windows\platform-tools 输入命令adb uninstall com.android.gesture.builder 进行卸载,然后运行一下就可以了
5.在屏幕上画出自定义的手势 如下:
6.运行结果如下: