Camera focusing: in principle, the vertical seekbar is used to obtain the distance based on the user's drag, and then set it to Camera in the form of Parameter.
OnSeekBarChangeListener
/*
* (Non-Javadoc)
*
* @ See
* Android. widget. SeekBar. OnSeekBarChangeListener # onProgressChanged (android
*. Widget. SeekBar, int, boolean)
*/
@ Override
Public void onProgressChanged (SeekBar arg0, int arg1, boolean arg2 ){
// TODO Auto-generated method stub
If (myCamera = null | arg2 ){
Arg0.setProgress (0 );
Return;
}
Try {
Parameters p = myCamera. getParameters ();
Int maxPa = p. getMaxZoom ();
Int maxCa = arg0.getMax ();
P. setZoom (maxPa * arg1/maxCa );
MyCamera. setParameters (p );
} Catch (Exception e ){
// TODO: handle exception
Arg0.setProgress (0 );
}
}
OnSeekBarChangeListener is implemented as follows:
Public class CameraSeekBar extends SeekBar {
/**
*
*/
/**
* @ Param context
*/
Public CameraSeekBar (Context context ){
Super (context );
// TODO Auto-generated constructor stub
SetThumb (getResources (). getDrawable (R. drawable. camera_thumb ));
SetThumbOffset (6 );
SetProgressDrawable (getResources (). getDrawable (R. drawable. color1 ));
SetIndeterminateDrawable (getResources (). getDrawable (R. drawable. color1 ));
}
Public CameraSeekBar (Context context, AttributeSet attrs, int defStyle ){
Super (context, attrs, defStyle );
}
Protected void onSizeChanged (int w, int h, int oldw, int oldh ){
Super. onSizeChanged (h, w, oldh, oldw );
}
@ Override
Protected synchronized void onMeasure (int widthMeasureSpec,
Int heightMeasureSpec ){
Super. onMeasure (heightMeasureSpec, widthMeasureSpec );
SetMeasuredDimension (getMeasuredHeight (), getMeasuredWidth ());
}
Protected void onDraw (Canvas c ){
C. rotate (-90 );
C. translate (-getHeight (), 0 );
C. drawColor (0x00000000 );
Super. onDraw (c );
}
@ Override
Public boolean onTouchEvent (MotionEvent event ){
If (! IsEnabled ()){
Return false;
}
Switch (event. getAction ()){
Case MotionEvent. ACTION_DOWN:
// Case MotionEvent. ACTION_MOVE:
Case MotionEvent. ACTION_UP:
Int I = 0;
I = getMax ()-(int) (getMax () * event. getY ()/getHeight ());
SetProgress (I );
OnSizeChanged (getWidth (), getHeight (), 0, 0 );
Break;
Case MotionEvent. ACTION_CANCEL:
Break;
}
Return true;
}
}
Setting the background and thumb can only be set in the constructor. The settings in other places are invalid. In addition, seekbar handstands mainly use the rotate method of Canvas, while rolling mainly depends on obtaining the touch height to set the progress. Camera_thumb is a small image I am looking for, while color1 is a pixel image.
Finally, add the control to the screen. In this example, the WindowManager operation is used.
Public void addSeekBar (){
// TODO Auto-generated method stub
If (cameraSeekBar! = Null ){
Return;
}
WManager = (WindowManager) context
. GetSystemService (Context. WINDOW_SERVICE );
CameraSeekBar = new CameraSeekBar (context );
CameraSeekBar. setBackgroundColor (0x00000000 );
If (wmParams = null ){
InitFloatView ();
}
WManager. addView (cameraSeekBar, wmParams );
CameraSeekBar. setOnSeekBarChangeListener (this );
}
// Floating menu
Private WindowManager. LayoutParams wmParams = null;
Private void initFloatView (){
// Set parameters related to LayoutParams
WmParams = new WindowManager. LayoutParams ();
WmParams. type = LayoutParams. TYPE_PHONE; // set the window type.
WmParams. format = PixelFormat. RGBA_8888; // set the image format. The effect is transparent to the background.
// Set Window flag
WmParams. flags = LayoutParams. FLAG_NOT_TOUCH_MODAL
| LayoutParams. FLAG_NOT_FOCUSABLE;
// Set the starting value of x and y in the upper left corner of the screen
WmParams. x = 100;
WmParams. y = 0;
// Set the long and wide data of the floating window
WmParams. width = CWWindowManager. getScreenWidth ()/20;
WmParams. height = CWWindowManager. getScreenHeight ()-170;
;
// Adjust the floating window
WmParams. gravity = Gravity. LEFT | Gravity. CENTER_VERTICAL;
}
This completes the preliminary requirements of the project.
In addition, if you need to switch between tabs, you can use the notification method to add and clear SeekBar
Public void removeSeekBar (){
// TODO Auto-generated method stub
If (cameraSeekBar = null ){
Return;
}
WManager. removeView (cameraSeekBar );
CameraSeekBar = null;
}