Android中提供了判断手势的接口,所有我们可以根据提供的API来实现各种各样的手势功能来提高手机应用的用户体验。
下面是我写的一段小Demo:
GestureActivity.java
public class GestureActivity extends Activity {
private GestureDetector gestureDetector;
private Screen screen;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gestureDetector = new GestureDetector(this,onGestureListener);
//得到屏幕的大小
screen = GestureUtils.getScreenPix(this);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
GestureDetector.OnGestureListener onGestureListener = new GestureDetector.SimpleOnGestureListener(){
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
float x = e2.getX() - e1.getX();
float y = e2.getY() - e1.getY();
//限制必须得划过屏幕的1/3才能算划过
float x_limit = screen.widthPixels / 3;
float y_limit = screen.heightPixels / 3;
float x_abs = Math.abs(x);
float y_abs = Math.abs(y);
if(x_abs >= y_abs){
//gesture left or right
if(x > x_limit || x < -x_limit){
if(x>0){
//right
show("right");
}else if(x<0){
//left
show("left");
}
}
}else{
//gesture down or up
if(y > y_limit || y < -y_limit){
if(y>0){
//down
show("down");
}else if(y<0){
//up
show("up");
}
}
}
return true;
}
};
private void show(String value){
Toast.makeText(this, value, Toast.LENGTH_SHORT).show();
}
}
GestureUtils.java
public class GestureUtils {
//获取屏幕的大小
public static Screen getScreenPix(Context context) {
DisplayMetrics dm = new DisplayMetrics();
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getMetrics(dm);
return new Screen(dm.widthPixels,dm.heightPixels);
}
public static class Screen{
public int widthPixels;
public int heightPixels;
public Screen(){
}
public Screen(int widthPixels,int heightPixels){
this.widthPixels=widthPixels;
this.heightPixels=heightPixels;
}
@Override
public String toString() {
return "("+widthPixels+","+heightPixels+")";
}
}
}

John F. Kennedy-“Ask not what your twitter can do for you, ask what you can do for your twitter.”
肯尼迪总统-“不要问你的微博能为你做些什么,问一下你自己能为微博做些什么?”
(原文:”Ask not what your country can do for you; ask what you can do for your country.“不要问你国家能为你做些什么而问你能为你国家做些什么。)

William Shakespeare -“There is nothing either good or bad but twittering makes it so.”
莎士比亚-“事物本身无所谓好坏之分,只是看你如何围脖它了。”
(原文:”There is nothing either good or bad, but thinking makes it so “事物本身无所谓好坏之分,只是人们的主观认知使然。)

Confucious -“Men’s natures are alike; it is their twitters that separate them.”
孔子-“性相近也,微博相远也”
(原文:”Men’s natures are alike, it is their habits that carry them farapart.“性相近也,习相远也.——文言文注解:人生下来的时候都是好的,只是由于成长过程中,后天的学习环境不一样,性情也就有了好与坏的差别。)

Alexander Pope -“To err is human, to twitter is divine.”
“会犯错是人,但会织围脖就是圣贤了。”——亚历山大波普(1688年5月21日- 1744年5月30日)是18世纪英国诗人。
(原文:”To Err Is Human, To Forgive Divine”会犯错是人,但会原谅别人过错就是圣贤了。)

Bill Gates -“140 characters ought to be enough for anybody.”
比尔盖茨-“140字应该对谁都足够啦。”
(原文:”640K ought to be enough for anybody.” 640kb的速度应该对谁都足够啦。)

Benjamin Franklin -“Mankind naturally and generally loved to be retweeted.”
本杰明·富兰克林-”人类天生就是喜欢被围脖转发。”
(原文:”Mankind naturally and generally love to be flattered.”人类天生就是喜欢被奉承。)

Franklin Delano Roosevelt -“The only thing to twitter about is twittering itself.”
罗斯福美国总统-“只有一个东西应该被围脖,那就是‘织围脖’本身。”
(原文:”Only Thing We Have to Fear Is Fear Itself”只有一个东西应该被恐惧,那就是恐惧本身。)

Dr. Victor Hugo Lindlahr -“You are what you tweet”
“从你的微博可以看出你是一个怎样的人。”——维克多雨果林德拉尔博士是美国健康食品和减肥的先驱。
(原文:”You Are What You Eat”从你吃的食物可以看出你是一个怎样的人?)

Mark Twain -“Twitter is mankind’s greatest blessing.”
马克吐温哦——“微博是人类得到最好的恩赐。”
(英文原文:”Humor is mankind’s greatest blessing.“幽默是人类得到最好的恩赐。)

Vincent Van Gogh -“I twitter my painting, and then I paint my twitter.”
文森特梵高——“我围脖我的画,然后我画我的微博。”
(原文:”I dream of painting and then I paint my dream.”我梦见我的画,然后我画我的梦。)

Rene Descartes -“I tweet, therefore I am”
哲学家笛卡尔-“我围脖,故我在。”
(原文:I Think, Therefore I Am 我思故我在。)

Martin Luther King, Jr.—”I have a dream that my four littlechildren will one day live in a nation where theywill not be judged bythe color of their skin, but by the content of their twitter.”
马丁路德金——“我梦想有一天,我的四个孩子将在一个不是以他们的肤色,而是以他们的微博内容来评判他们的国度里生活。”

Barack Obama–”Twitter,we need it!”
总统奥巴马——“我们需要微博。”
(原文:” Change,we need it!”我们需要改革!)
(原文:”I have a dream that my four little children will one day livein a nation where theywill not be judged by the color of their skin,but by the content of their character.”我梦想有一天,我的四个孩子将在一个不是以他们的肤色,而是以他们的品格优劣来评判他们的国度里生活。)

后记:奥巴马和马丁路德金这一条为eyeera原创(当然我们对马丁路德金这个伟人非常的敬畏!),其他参考资料来自http://www.gadgetcage.com。本文中的中文和英文原文都是由eyeera.com整理编译,哈哈,欢迎转载,麻烦您注明一下出处,呵呵,谢谢!【爱心】