1. 程式人生 > >實現通過上滑或者下滑來切換前後攝像頭的功能

實現通過上滑或者下滑來切換前後攝像頭的功能

  • 在vendor/mediatek/proprietary/packages/apps/Camera/src/com/android/camera/GestureDispatcher.java中定義一個實現上滑下滑動作的監聽介面
    public interface GestureScrollListener{
        	public boolean onDown(float x,float y, int width, int height);
        	public boolean onScroll(float dx,float dy,float totalX,float totalY);
        	public boolean onFling(MotionEvent e1,MotionEvent e2,float velocityX,float velocityY);
        	public boolean onUp();
        	public boolean onScale(float focusX,float focusY,float scale);
        	public boolean onScaleBegin(float focusX,float focusY);
        }

  • 並在該類中例項化一個GestureScrollListener介面,同時定義一個設定手勢滑動的監聽器方法
    private GestureScrollListener mGestureScrollListener;
        public void setGestureScrollListener(GestureScrollListener listener){
        	mGestureScrollListener=listener;
        }

  • 在該類重寫的方法onScroll中新增
    if(mGestureScrollListener!=null&&mGestureScrollListener.onScroll(dx,dy,totalX,totalY)){
        	return false;
        }

  • 在該類重寫的方法onFling中新增
    if(mGestureScrollListener!=null&&mGestureScrollListener.onFling(e1,e2,velocityX,velocityY)){
        	return false;
        }

  • 在該類重寫的方法onScaleBegin中新增
    if(mGestureScrollListener!=null&&mGestureScrollListener.onScaleBegin(focusX,focusY)){
        	return false;
        }

  • 在該類重寫的方法onScale中新增
    if(mGestureScrollListener!=null&&mGestureScrollListener.onScale(focusX,focusY,scale)){
        	return false;
        }

  • 在該類重寫的方法onDown中新增
    if(mGestureScrollListener!=null&&mGestureScrollListener.onDown(vertex[0],vertex[1],(int)vertex[2],(int)vertex[3])){
        	return;
        }

  • 在該類重寫的方法onUp中新增
    if(mGestureScrollListener!=null&&mGestureScrollListener.onUp()){
        	return;
        }

  • 在vendor/mediatek/proprietary/packages/apps/Camera/src/com/android/camera/manager/PickerManager.java中定義一個繼承了GestureScrollListener的類MyGestureScrollListener,新增內容如下:
    public class MyGestureScrollListener implements GestureScrollListener{
        	private boolean isScroll=false;
        	private boolean isScaled=false;
        	private float mTotalX=0;
        	private float mTotalY=0;
        	private long beginTouchTime=0;
        	private long endTouchTime=0;
        	private void CameraActivity mCameraActivity;
        	public void setCameraActivity(CameraActivity mCameraActivity){
        		this.mCameraActivity=mCameraActivity;
        	}
        	@Override
        	public bollean onScroll(float dx,float dy,float totalX,float totalY){
        		Log.i(TAG,"onScroll("+dx+","+dy+","+totalX+","+totalY+")");
        		if(!isScroll){
        			isScroll=true;
        		}
        		mTotalX=totalX;
        		mTotalY=totalY;
        		return false;
        	}
        	
        	@Override
        	public boolean onFling(MotionEvent e1,MotionEvent e2,float velocityX,float velocityY){
        		Log.i(TAG,"[onFling] ("+velocityX+", "+velocityY+")");
        		Log.d(TAG,"isScroll="+Boolean.toString(isScroll));
        		Log.d(TAG,"isScaled="+Boolean.toString(isScaled));
        		Log.d(TAG,"mCameraActivity.getCurrentMode()="+mCameraActivity.getCurrentMode());
        		if(!isScaled && isScroll && (mCameraActivity.getCurrentMode()!=ModePicker.MODE_VIDEO)){
        			endTouchTime=System.currentTimeMillis();
        			float time=Long.valueOf(endTouchTime-beginTouchTime).floatValue();
        			Log.d(TAG,"time ="+time+" mTotalX"+mTotalX+" mTotalY"+mTotalY+" beginTouchTime="+beginTouchTime);
        		if((Math.ads(mTotalY)>Math.abs(mTotalX))&&(Math.abs(mTotalY)>50)&&((Math.abs(mTotalY)/time)>((float)500/1000))){
        			Log.d(TAG,"Check over");
        			if(mCameraPicker!=null){
        				mCameraPicker.onClick(null);
        			}
        			else{
        				Log.d(TAG,"mCameraPicker=null");
        			}
        		}
        		return false;
        	}
        		
        	@Override
        	public boolean onScaleBegin(float focusX,float focusY){
        		Log.i(TAG,"onScaleBegin("+focusX+","+focusY+")");
        		isScaled=true;
        		return false;
        	}
        	
        	@Override
        	public boolean onScale(float focusX,float focusY,float scale){
        		Log.i(TAG,"onScale("+focusX+", "+focusY+", "+scale);
        		return false;
        	}
        	
        	@Override
        	public boolean onDown(float x,float y,int width,int height){
        		beginTouchTime=System.currentTimeMillis();
        		Log.i(TAG,"onDown()");
        		return false;
        	}
        	
        	@Override
        	public boolean onUp(){
        		Log.i(TAG,"onUp");
        		beginTouchTime=0;
        		endTouchTime=0;
        		isScaled=false;
        		isScroll=false;
        		return false;
        	}
        }
        

  • 在vendor/mediatek/proprietary/packages/apps/Camera/src/com/android/camera/CameraActivity.java中新增GestureScrollListener物件和設定手勢滑動監聽器的方法
    private GestureScrollListener mGestureScrollListener;
        public void setGestureScrollListener(GestureScrollListener listener){
        	mGestureScrollListener=listener;
        }

  • 呼叫GestureDispatcher的設定監聽器的方法
    mGestureDispatcher.setGestureScrollListener(mGestureScrollListener);

  • 在vendor/mediatek/proprietary/packages/apps/Camera/src/com/android/camera/bridge/CameraAppUiImpl.java中定義一個MyGestureScrollListener物件,並將活動傳入該監聽器物件,同時給活動傳入該監聽器
    MyGestureScrollListener mGestureScrollListener=mPickerManage.new MyGestureScrollLisener();
        mGestureScrollListener.setCameraActivity(mCameraActivity);
        mCameraActivity.setGestureScrollListener(mGestureScrollListener);