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; } } }
相关推荐
而`SimpleOnGestureListener`是`GestureDetector`的简单实现,包含了一些基本的手势监听方法,如`onDown()`, `onSingleTapUp()`, `onScroll()`, `onFling()`等。 1. **onDown(MotionEvent e)**: 这是手势的起点,当...
开发者需要覆写onDown、onFling、onScroll等方法,以识别和处理滑动手势。 2. **图片缓存**:为了实现图片的循环显示,LoopImageView需要维护一个图片缓存,通常使用LruCache或者其他高效的缓存策略,确保图片的...
- `SimpleOnGestureListener`是`GestureDetector`的一个简单实现,包含了一些基本手势的回调方法,比如`onDown()`, `onFling()`, `onScroll()`等,开发者可能会覆写这些方法来响应特定的手势操作。 2. **自定义...
其中,`MyGestureListener`是你实现的OnGestureListener子类,包含对各个手势方法的具体实现。通过这种方式,你可以根据应用需求定制各种手势行为。 总之,Android中的手势滑动实现主要依赖于GestureDetector和...
手势处理在Android应用开发中扮演着重要的角色,它极大地提升了用户与应用程序的交互体验。...同时,注意在实际操作中正确处理各个手势方法,以及确保组件能够接受长按事件,这是实现手势识别的关键。
它不仅实现了传统的点击切换,还引入了拖动和滑动的交互方式,使用户可以通过手势轻松地在各个标签之间切换,提高了用户的操作效率。 首先,我们来理解"TabSwitcher"的基本结构。这个控件可能包含一个容器,用于...
开发者可以监听`onDown()`, `onFling()`, `onScroll()`等方法来识别和处理滑动、轻拍等手势,从而实现日历页面的左右滑动切换月份。 2. **自定义View**:为了实现特定的日历布局和交互,开发者可能创建了一个自定义...
然后,我们实现了`OnGestureListener`接口中的各个方法,针对不同的手势进行相应的操作。例如,`onScroll`方法可以用来处理水平滑动,根据`distanceX`的值来决定是否切换图片。 为了使图片能够被切换,我们还需要在...
通过重写`onDown()`, `onScroll()`, 和`onFling()`等方法,我们可以捕获滑动开始、滑动过程和快速滑动结束的事件。同时,需要计算滑动的距离和速度,以便判断是向上还是向下滑动。 2. **视图动画**:Android提供了...
`onDown`方法用于初始化滑动手势,`onScroll`用于处理滑动过程,而`onFling`则处理快速滑动的情况。通过比较滑动的起始和结束位置,我们可以判断滑动的方向,进而执行不同的操作。 为了使滑动过程平滑,我们需要在...
这个例子将详细展示如何实现滑动删除的各个步骤,包括自定义ViewGroup、手势检测和动画处理。通过学习和实践这个示例,你将能够掌握在Android中实现滑动删除Activity的核心技术。 总的来说,实现仿知乎滑动删除...
我们可以监听`onDown()`, `onScroll()`, 和 `onFling()`等手势事件,从而实现平滑的页面切换。此外,`PageTransformer`接口可以用来定制滑动时的页面变换效果,比如淡入淡出、缩放等特效。 为了增加用户体验,我们...
1. **手势检测**:在Android中,我们可以创建一个`GestureDetector`实例,重写其`onDown()`, `onFling()`, `onScroll()`等方法,以便在用户进行滑动操作时触发相应的回调。`onFling()`方法尤其关键,它会在用户快速...
`GestureDetector`允许我们监听和处理各种触摸事件,如`onDown()`, `onScroll()`, `onFling()`等,其中`onFling()`方法用于检测快速滑动的动作,非常适合实现侧滑效果。 2. **视图切换**:当滑动手势被识别后,需要...
通过创建`GestureDetector`实例,重写其回调方法如`onDown()`, `onFling()`, `onScroll()`等,我们可以轻松地检测和响应滑动事件。 6. **SlidingDrawer**: `SlidingDrawer`(已废弃,但仍然有用)是Android早期...
`SimpleOnGestureListener`是`GestureDetector`的一个简单实现,提供了常用手势的回调方法,如`onDown()`(手指按下)、`onScroll()`(滚动)、`onFling()`(快速滑动)。在这个示例中,可能会重写`onFling()`来...
`OnGestureListener`接口提供了`onDown()`, `onFling()`, `onScroll()`等方法,用于处理不同的滑动事件。 6. **动画效果**: 为了让菜单滑动更炫酷,可以使用`ObjectAnimator`或`PropertyAnimator`添加过渡动画。...
这通常需要使用` GestureDetector` 或 `SimpleOnGestureListener` 类,监听`onDown()`, `onFling()`, 和 `onScroll()` 等事件。 5. **布局文件**:在res目录下,可能包含XML布局文件,用于定义每个页面的视图结构。...
`onDown()`, `onScroll()`, `onFling()`等方法可以捕获并处理滑动事件。`SwipeRefreshLayout`是另一个常用的组件,用于在列表上方实现下拉刷新的手势。 3. **滑动切换页面**:在多个Fragment或Activity之间进行侧滑...
`onSingleTapUp(MotionEvent event)`则处理单击手势,`onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY)`用于检测快速滑动动作,而`onScroll(MotionEvent e1, MotionEvent e2, ...