blog from:http://blog.csdn.net/liuxian13183, reprint annotated source! All Rights Reserved!
Camera Focus: Principle, use vertical seekbar, according to user drag to get distance, and then set to camera in the form of parameter.
Implement Onseekbarchangelistener
/*
* (Non-javadoc)
*
* @see
* Android.widget.seekbar.onseekbarchangelistener#onprogresschanged (Android
*. widgets. 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);
}
}
The implementation of Onseekbarchangelistener is 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 the thumb can only be set in the construction method, the other place is invalid, and the Seekbar handstand mainly uses the canvas's rotate method, and the scroll mainly depends on the hand touch height to set the progress. Camera_thumb is a small picture I'm looking for, and Color1 is a picture of a pixel.
Finally, how to add this control to the screen, this example uses the WindowManager operation
< Span style= "White-space:pre" > 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);
}
Hover Menu
Private Windowmanager.layoutparams wmparams = null;
private void Initfloatview () {
Set Layoutparams (global variables) related parameters
Wmparams = new Windowmanager.layoutparams ();
Wmparams.type = Layoutparams.type_phone; Set Window type
Wmparams.format = pixelformat.rgba_8888; Format picture, effect is transparent background
Set Window flag
Wmparams.flags = Layoutparams.flag_not_touch_modal
| layoutparams.flag_not_focusable;
Set X, y initial values as the origin in the upper-left corner of the screen
wmparams.x = 100;
Wmparams.y = 0;
Setting the hover window length-width data
Wmparams.width = Cwwindowmanager.getscreenwidth ()/20;
Wmparams.height = Cwwindowmanager.getscreenheight ()-170;
;
Adjust the hover window
wmparams.gravity = Gravity.left | gravity.center_vertical;
}
This will complete the initial requirements of the project.
In addition, if you need to switch between tabs, you can use the method of sending notifications to add and clear Seekbar
public void Removeseekbar () {
TODO auto-generated Method Stub
if (Cameraseekbar = = null) {
Return
}
Wmanager.removeview (Cameraseekbar);
Cameraseekbar = null;
}