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 onScroll各个手势效果实现
猜你喜欢
转载自xiaxingwork.iteye.com/blog/1834865
今日推荐
周排行