package com.lenovo.anyclock; import android.annotation.SuppressLint; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.os.Handler; import android.util.Log; import android.view.Display; import android.view.GestureDetector; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.view.WindowManager; import android.widget.TabHost; import com.lenovo.anyclock.alarm.Alarm; import com.lenovo.anyclock.leftview.LeftListView; import com.lenovo.anyclock.setup.Config; import com.lenovo.anyclock.setup.Setup; import com.lenovo.anyclock.sleep.Sleep; import com.lenovo.anyclock.utils.Constants; import com.lenovo.anyclock.utils.Util; import com.umeng.analytics.MobclickAgent; @SuppressLint("NewApi") public class AnyClockActivity extends Activity implements OnClickListener, OnTouchListener { private static final String TAG = "AnyClockActivity"; public static MainSurfaceView mMainView; private static View mListView; private static View mSetupView; private static View mSleepView; private LeftListView mLeftListView; private static Setup mSetup; private static Sleep mSleep; private GestureDetector mGestureDetector; private long mKeyReturnTime; private int mSDKVersion = 0; private final BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); if (Intent.ACTION_SCREEN_OFF.equals(action)) { if (Constants.STATE == Constants.VIEW_ALARM) { if (Alarm.AlreadySnooze >= Alarm.SNOOZE_TIMES) { Log.d(TAG, "snooze count is max, so return"); Alarm.snooze(context, 1); } else { Alarm.AlreadySnooze++; Log.d(TAG, "snooze, times=" + Alarm.AlreadySnooze); Alarm.snooze(context, Alarm.SNOOZE_TIME); } } } } }; @SuppressWarnings("deprecation") @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF); registerReceiver(mBatInfoReceiver, filter); MobclickAgent.onError(this); WindowManager windowManager = getWindowManager(); Display display = windowManager.getDefaultDisplay(); Constants.SCREEN_WIDTH = display.getWidth(); Constants.SCREEN_HEIGTH = display.getHeight(); Constants.WIDGET_RATIO = (float)(Constants.SCREEN_WIDTH * 1.0) / Constants.SCREEN_REF_WIDTH; Constants.SCREEN_WIDTH_RADIO = (float)(Constants.SCREEN_WIDTH * 1.0) / Constants.SCREEN_REF_WIDTH; Constants.SCREEN_HEIGTH_RADIO = (float)(Constants.SCREEN_HEIGTH * 1.0) / Constants.SCREEN_REF_HEIGTH; setContentView(R.layout.main); mMainView = (MainSurfaceView)findViewById(R.id.main_view); mMainView.init(this, Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGTH); Constants.STATE = Constants.VIEW_MAIN; // 初始化闹钟的启动参数 Config mConfig = new Config(); mConfig.init(this); findViewById(R.id.touch_mask).setOnTouchListener(this); mGestureDetector = new GestureDetector(new MyGestureListener(this)); if (getIntent().getAction().equalsIgnoreCase(Alarm.ALARM_ALERT_ACTION)) { Log.d(TAG, "ChangeToAlarmGroup()"); mMainView.changeToAlarmGroup(); } mSDKVersion = Util.getAndoirdSDKVersion(); } @Override public void onDestroy() { if (mBatInfoReceiver != null) { try { unregisterReceiver(mBatInfoReceiver); } catch (Exception e) { Log.d(TAG, "unregisterReceiver mBatInfoReceiver failure :" + e.getCause()); } } super.onDestroy(); } @Override protected void onPause() { mMainView.saveEnvirment(); super.onPause(); MobclickAgent.onPause(this); } @Override protected void onResume() { super.onResume(); mMainView.loadEnvirment(); MobclickAgent.onResume(this); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { Log.d(TAG, "onKeyDown:" + keyCode); boolean ret = true; if (keyCode == KeyEvent.KEYCODE_BACK) { if (Constants.STATE == Constants.VIEW_MAIN) { if ((System.currentTimeMillis() - mKeyReturnTime) < 500) // 500ms以内的按键认为是重复按按键,被抛弃 ret = false; else ret = super.onKeyDown(keyCode, event); } else if (Constants.STATE == Constants.VIEW_SETUP || Constants.STATE == Constants.VIEW_ACHIVEMENT || Constants.STATE == Constants.VIEW_SLEEP) { changeToMainGroup(); } else { mMainView.changeToMainGroup(); } mKeyReturnTime = System.currentTimeMillis(); Constants.STATE = Constants.VIEW_MAIN; return ret; } else return super.onKeyDown(keyCode, event); } @Override public boolean onTouch(View v, MotionEvent event) { if ((isUpdateViewRunning || mScrollDirection != SCROLL_DIREC.SCROLL_IDLE)) { if (((mScrollDirection == SCROLL_DIREC.SCROLL_DOWN || mScrollDirection == SCROLL_DIREC.SCROLL_UP) && Constants.STATE == Constants.VIEW_ACHIVEMENT)) { if (!dispatchTouchEvent(v, event)) { mGestureDetector.onTouchEvent(event); onTouchEvent(event); } return true; } mGestureDetector.onTouchEvent(event); onTouchEvent(event); return true; } if (!dispatchTouchEvent(v, event)) { mGestureDetector.onTouchEvent(event); onTouchEvent(event); } return true; } private boolean dispatchTouchEvent(View v, MotionEvent event) { if (Constants.STATE == Constants.VIEW_ACHIVEMENT) { return mLeftListView.processTouchEvent(event); } else if (Constants.STATE == Constants.VIEW_SETUP) { return mSetup.processTouchEvent(event); } else if (Constants.STATE == Constants.VIEW_SLEEP) { return mSleep.processTouchEvent(event); } else { return mMainView.processTouchEvent(event); } } @Override public void onClick(View view) {} // -1, not judged; 1 direction x; 2 direction y private SCROLL_DIREC mScrollDirection = SCROLL_DIREC.SCROLL_IDLE;; private static final int MIN_DISTANCE = 10; private int PIXEL = 60; Handler mHandler = new Handler(); private int TIME_UNIT = 1; //ms public enum SCROLL_DIREC { SCROLL_IDLE, SCROLL_LEFT, SCROLL_RIGHT, SCROLL_UP, SCROLL_DOWN, SCROLL_HORISON, SCROLL_VERTICAL } boolean isUpdateViewRunning = false; @SuppressLint("NewApi") private void animListView(int x) { int posX = (int)mListView.getX(); int width = mListView.getWidth(); Log.v(TAG, "updateView running, animListView, posX : " + posX); if (posX >= x) { if (posX == 0) { isUpdateViewRunning = false; mScrollDirection = SCROLL_DIREC.SCROLL_IDLE; Constants.STATE = Constants.VIEW_ACHIVEMENT; return; } posX += PIXEL; if (posX > 0) posX = 0; mListView.setX(posX); mHandler.postDelayed(updateView, TIME_UNIT); } else if (posX < x) { if (posX == -width) { isUpdateViewRunning = false; mScrollDirection = SCROLL_DIREC.SCROLL_IDLE; Constants.STATE = Constants.VIEW_MAIN; mListView.setVisibility(View.INVISIBLE); } posX -= PIXEL; if (posX < -width) posX = -width; mListView.setX(posX); mHandler.postDelayed(updateView, TIME_UNIT); } } @SuppressLint("NewApi") private void animSleepView(int y) { int posY = (int)mSleepView.getY(); int height = mSleepView.getHeight(); Log.v(TAG, "updateView running, animSleepView, posY : " + posY); if (posY >= y) { if (posY == 0) { isUpdateViewRunning = false; mScrollDirection = SCROLL_DIREC.SCROLL_IDLE; Constants.STATE = Constants.VIEW_SLEEP; return; } posY += PIXEL; if (posY > 0) posY = 0; mSleepView.setY(posY); mHandler.postDelayed(updateView, TIME_UNIT); } else if (posY < y) { if (posY == -height) { isUpdateViewRunning = false; mScrollDirection = SCROLL_DIREC.SCROLL_IDLE; Constants.STATE = Constants.VIEW_MAIN; mSleepView.setVisibility(View.INVISIBLE); mSleep.reset(); mSleep = null; mSleepView = null; Constants.STATE = Constants.VIEW_MAIN; return; } posY -= PIXEL; if (posY < -height) posY = -height; mSleepView.setY(posY); mHandler.postDelayed(updateView, TIME_UNIT); } } @SuppressLint("NewApi") private void animSetupView(int x) { int posX = (int)mSetupView.getX(); int width = mSetupView.getWidth(); Log.v(TAG, "updateView running, animSetupView, posX : " + posX); if (posX < x) { if (posX == 0) { isUpdateViewRunning = false; mScrollDirection = SCROLL_DIREC.SCROLL_IDLE; Constants.STATE = Constants.VIEW_SETUP; return; } posX -= PIXEL; if (posX < 0) posX = 0; mSetupView.setX(posX); mHandler.postDelayed(updateView, TIME_UNIT); } else if (posX >= x) { if (posX == width) { isUpdateViewRunning = false; mScrollDirection = SCROLL_DIREC.SCROLL_IDLE; Constants.STATE = Constants.VIEW_MAIN; mSetupView.setVisibility(View.INVISIBLE); mSetup.saveConfig(); mSetup = null; mSetupView = null; return; } posX += PIXEL; if (posX > width) posX = width; mSetupView.setX(posX); mHandler.postDelayed(updateView, TIME_UNIT); } } Runnable updateView = new Runnable() { public void run() { if ((mScrollDirection == SCROLL_DIREC.SCROLL_RIGHT && Constants.STATE == Constants.VIEW_MAIN)) { animListView(-mListView.getWidth() + 50); } else if ((mScrollDirection == SCROLL_DIREC.SCROLL_LEFT && Constants.STATE == Constants.VIEW_ACHIVEMENT)) { animListView(-50); } else if ((mScrollDirection == SCROLL_DIREC.SCROLL_LEFT && Constants.STATE == Constants.VIEW_MAIN)) { animSetupView(mSetupView.getWidth() - 50); } else if ((mScrollDirection == SCROLL_DIREC.SCROLL_RIGHT && Constants.STATE == Constants.VIEW_SETUP)) { animSetupView(50); } else if ((mScrollDirection == SCROLL_DIREC.SCROLL_DOWN && Constants.STATE == Constants.VIEW_MAIN)) { animSleepView(-mSleepView.getHeight() + 50); } else if ((mScrollDirection == SCROLL_DIREC.SCROLL_UP && Constants.STATE == Constants.VIEW_SLEEP)) { animSleepView(-50); } // This is important to reset parameters of scroll event else { isUpdateViewRunning = false; mScrollDirection = SCROLL_DIREC.SCROLL_IDLE; } } }; public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_UP: if (mScrollDirection != SCROLL_DIREC.SCROLL_IDLE) { Log.v(TAG, "onTouchEvent, ACTION_UP"); isUpdateViewRunning = true; mHandler.postDelayed(updateView, TIME_UNIT); } // if (((mScrollDirection == SCROLL_DIREC.SCROLL_DOWN || mScrollDirection == SCROLL_DIREC.SCROLL_UP) // && Constants.STATE == Constants.VIEW_ACHIVEMENT)) { // mScrollDirection = SCROLL_DIREC.SCROLL_IDLE; // } // break; case MotionEvent.ACTION_MOVE: break; case MotionEvent.ACTION_CANCEL: break; } return false; } public static void changeToMainGroup() { if (Constants.STATE == Constants.VIEW_ACHIVEMENT) { Util.slideOut(mListView, Util.DIRECTION_LEFT); } else if (Constants.STATE == Constants.VIEW_SETUP) { Util.slideOut(mSetupView, Util.DIRECTION_RIGHT); mSetup.saveConfig(); mSetup = null; mSetupView = null; } else if (Constants.STATE == Constants.VIEW_SLEEP) { Util.slideOut(mSleepView, Util.DIRECTION_UP); mSleep.reset(); mSleep = null; mSleepView = null; } Constants.STATE = Constants.VIEW_MAIN; } private class MyGestureListener extends GestureDetector.SimpleOnGestureListener { private Context mContext; public MyGestureListener(Context context) { mContext = context; } @Override public boolean onDown(MotionEvent arg0) { return true; } private static final int MIN_VERTICAL = 120; private static final int MIN_VELOCITY = 0; public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { if (mSDKVersion < 0) return true; if (Constants.STATE == Constants.VIEW_MAIN) { if (e1.getX() - e2.getX() > MIN_VERTICAL && Math.abs(velocityX) > MIN_VELOCITY) { if (mSetupView == null) { mSetupView = findViewById(R.id.setup_panel); mSetupView.setVisibility(View.INVISIBLE); mSetup = new Setup(mContext, mSetupView); } Util.slideIn(mSetupView, Util.DIRECTION_RIGHT); Constants.STATE = Constants.VIEW_SETUP; } else if (e2.getX() - e1.getX() > MIN_VERTICAL && Math.abs(velocityX) > MIN_VELOCITY) { if (mListView == null) { mListView = findViewById(R.id.tabhost); mListView.setVisibility(View.INVISIBLE); mLeftListView = new LeftListView(mContext, (TabHost)mListView); mLeftListView.initPreference(); } Util.slideIn(mListView, Util.DIRECTION_LEFT); Constants.STATE = Constants.VIEW_ACHIVEMENT; } else if (e2.getY() - e1.getY() > MIN_VERTICAL && Math.abs(velocityY) > MIN_VELOCITY) { if (mSleepView == null) { mSleepView = findViewById(R.id.sleep_panel); mSleepView.setVisibility(View.INVISIBLE); mSleep = new Sleep(mContext, mSleepView); } Util.slideIn(mSleepView, Util.DIRECTION_UP); Constants.STATE = Constants.VIEW_SLEEP; } } else if (Constants.STATE == Constants.VIEW_ACHIVEMENT) { if (e1.getX() - e2.getX() > MIN_VERTICAL && Math.abs(velocityX) > MIN_VELOCITY) { changeToMainGroup(); } } else if (Constants.STATE == Constants.VIEW_SETUP) { if (e2.getX() - e1.getX() > MIN_VERTICAL && Math.abs(velocityX) > MIN_VELOCITY) { changeToMainGroup(); } } else if (Constants.STATE == Constants.VIEW_SLEEP) { if (e1.getY() - e2.getY() > MIN_VERTICAL && Math.abs(velocityY) > MIN_VELOCITY) { changeToMainGroup(); } } return true; } @Override public void onLongPress(MotionEvent arg0) { // TODO Auto-generated method stub Log.v(TAG, "onLongPress 2"); } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float arg2, float arg3) { // method view.setX() not found in lower SDK version, use fling if (mSDKVersion > 0) return true; if (isUpdateViewRunning) return true; // Judge direction if (mScrollDirection == SCROLL_DIREC.SCROLL_IDLE) { if (e2.getX() - e1.getX() > MIN_DISTANCE) { mScrollDirection = SCROLL_DIREC.SCROLL_RIGHT; } else if (e1.getX() - e2.getX() > MIN_DISTANCE) { mScrollDirection = SCROLL_DIREC.SCROLL_LEFT; } else if (e2.getY() - e1.getY() > MIN_DISTANCE) { mScrollDirection = SCROLL_DIREC.SCROLL_DOWN; } else if (e1.getY() - e2.getY() > MIN_DISTANCE) { mScrollDirection = SCROLL_DIREC.SCROLL_UP; } } // Make sure scroll direction is judged if (mScrollDirection == SCROLL_DIREC.SCROLL_IDLE) return true; // Calculate scroll value float scrollX = e2.getX() - e1.getX(); float scrollY = e2.getY() - e1.getY(); // Scroll value must match the direction if (mScrollDirection == SCROLL_DIREC.SCROLL_RIGHT) { scrollX = Util.caliper(0, Constants.SCREEN_WIDTH, scrollX); scrollY = 0; } else if (mScrollDirection == SCROLL_DIREC.SCROLL_LEFT) { scrollX = Util.caliper(-Constants.SCREEN_WIDTH, 0, scrollX); scrollY = 0; } else if (mScrollDirection == SCROLL_DIREC.SCROLL_DOWN) { scrollY = Util.caliper(0, Constants.SCREEN_HEIGTH, scrollY); scrollX = 0; } else if (mScrollDirection == SCROLL_DIREC.SCROLL_UP) { scrollY = Util.caliper(-Constants.SCREEN_HEIGTH, 0, scrollY); scrollX = 0; } // Log.v(TAG, "mScrollDirection : " + mScrollDirection); // Log.v(TAG, "scrollX : " + scrollX + ", scrollY : " + scrollY); // Scroll view if (Constants.STATE == Constants.VIEW_MAIN) { if (mScrollDirection == SCROLL_DIREC.SCROLL_RIGHT) { if (mListView == null) { mListView = findViewById(R.id.tabhost); mListView.setVisibility(View.INVISIBLE); mLeftListView = new LeftListView(mContext, (TabHost)mListView); mLeftListView.initPreference(); } mListView.setVisibility(View.VISIBLE); mListView.setX(-mListView.getWidth() + scrollX); } else if (mScrollDirection == SCROLL_DIREC.SCROLL_LEFT) { if (mSetupView == null) { mSetupView = findViewById(R.id.setup_panel); mSetupView.setVisibility(View.INVISIBLE); mSetup = new Setup(mContext, mSetupView); } mSetupView.setVisibility(View.VISIBLE); mSetupView.setX(mSetupView.getWidth() + (int)scrollX); } else if (mScrollDirection == SCROLL_DIREC.SCROLL_DOWN) { if (mSleepView == null) { mSleepView = findViewById(R.id.sleep_panel); mSleepView.setVisibility(View.INVISIBLE); mSleep = new Sleep(mContext, mSleepView); } mSleepView.setVisibility(View.VISIBLE); mSleepView.setY(-mSleepView.getHeight() + scrollY); } } else if (Constants.STATE == Constants.VIEW_ACHIVEMENT) { if (mScrollDirection == SCROLL_DIREC.SCROLL_LEFT) mListView.setX(scrollX); } else if (Constants.STATE == Constants.VIEW_SETUP) { if (mScrollDirection == SCROLL_DIREC.SCROLL_RIGHT) mSetupView.setX(scrollX); } else if (Constants.STATE == Constants.VIEW_SLEEP) { if (mScrollDirection == SCROLL_DIREC.SCROLL_UP) mSleepView.setY(scrollY); } return true; } @Override public void onShowPress(MotionEvent arg0) { // TODO Auto-generated method stub Log.v(TAG, "onShowPress 2"); } @Override public boolean onSingleTapUp(MotionEvent arg0) { // TODO Auto-generated method stub Log.v(TAG, "onSingleTapUp 2"); return true; } } }
相关推荐
使用onFling方法,实现滑动切换画面的效果
触摸滑动效果,左右滑动,GestureDetector手势监听,ViewPager装左右滑动页面
下面我们就以实现手势识别的onFling动作,在CwjView中我们从View类继承,当然大家可以从TextView等更高层的界面中实现触控,感兴趣的朋友可以了解下哈
在android上实现手势的识别也比较简单,可以实现OnTouchListener和OnGuestureListener接口, 然后在OnTouch函数中注册GestureDetector来判别手势动作, http://wang-peng1.iteye.com/blog/572886 ...
Android-App-MotionGestures Android应用程式可显示动作手势的用法,例如onFling,onLongPress等
实现滑动切换的主要代码都在onFling()方法中,用户按下触摸屏,快速移动后松开,就会触发这个事件。在这段代码示例中,对手指滑动的距离进行了计算,如果滑动距离大于120像素,就做切换动作,否则不做任何切换动作。...
如何实现手机上手动滑动翻页效果呢?呵呵,在这里我们就给你们介绍一下吧。 一般实现这个特效会用到一个控件:ViewFlipper <1>View切换的控件—ViewFlipper 这个控件是什么呢?请注意观看博文Android学习笔记之...
自定义ViewGroup,根据手势滑动界面
通过手势识别器实现界面的转跳,具体内容如下 1、创建 GestureDetector对象 2、创建新类继承SimpleOnGestureListener类(创建 GestureDetecto需要的参数) 3、重写SimpleOnGestureListener中的OnFling()方法。...
Android开发之实现手势滑动的功能 首先得Activity必须实现OnGestureListener接口,该接口提供了关于手势操作的一些方法, onDown方法:onDown是,一旦触摸屏按下,就马上产生onDown事件 public boolean onDown...
大致的实现思路:随着我指尖在屏幕上的游走不断地输出onScroll 的信息,最后onScroll() 和 onFling() 我都用上了, Scroll 的时候随手指的移动切换,当手指松开之后用Fling实现惯性的切换,这样就显得相当的自然了,...
利用viwflipper来将多个activity封装在一个view中,通过实现OnGestureListener接口的onTouchEvent()方法和onFling() 方法,即可通过手持滑动切换activity。
Android的事件:onClick, onScroll,onFling等等,都是由许多个Touch组成的。其中Touch的第一个状态肯定是ACTION_DOWN,表示按下了屏幕。之后,touch将会有后续事件,可能是: ACTION_MOVE //表示为移动手势 ACTION_UP...
本功能使用Fragment+FragmentTabHost实现tab的点击切换屏幕,再加上监听OnGestureListener,实现其onFling方法,完成滑动切屏。简单易懂的一个程序。
使用Gallery和ImageView实现android左右滑动+索引图标效果。 首先自定义Gallery实现一次只能滑动一个页面 代码如下: public class MGalleryView extends Gallery{ public MGalleryView(Context context, ...
3个ListView联动,后期加入onFling
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) { if (e1.getX() - e2.getX() (-FLEEP_DISTANCE)) {//从左向右滑动 currentTabID = tabHost.getCurrentTab() - 1;...
使用ViewFlipper的手势功能完成的图像之间的切换、 package com.action; import android.app.Activity; import android.os.Bundle; import android.view.GestureDetector; import android.view.MotionEvent; import...
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { // If fling happens, index bar shows mScroller.show(); return super.onFling(e1, e2, ...