码迷,mamicode.com
首页 > 移动开发 > 详细

Android中级第九讲--相机调焦

时间:2014-05-15 19:44:33      阅读:431      评论:0      收藏:0      [点我收藏+]

标签:相机   调焦   竖直seekbar   

              

                    博客出自:http://blog.csdn.net/liuxian13183,转载注明出处! All Rights Reserved ! 


相机调焦:原理,使用竖直seekbar,根据用户拖拉来获得距离,然后以Parameter的形式设置到Camera。

实现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如下

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;
}


}

设置背景和thumb只能在构造方法里设置,其他地方设置无效,另外seekbar倒立主要采用Canvas的rotate方法;而滚动主要靠获得手触高度,来设置进度。camera_thumb是我找的一张小图片,而color1是一个像素的图片。

bubuko.com,布布扣bubuko.com,布布扣

最后如何把这个控件添加到屏幕上,本例采用WindowManager操作

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);
}

// 悬浮菜单
private WindowManager.LayoutParams wmParams = null;


private void initFloatView() {
// 设置LayoutParams(全局变量)相关参数
wmParams = new WindowManager.LayoutParams();
wmParams.type = LayoutParams.TYPE_PHONE; // 设置window type
wmParams.format = PixelFormat.RGBA_8888; // 设置图片格式,效果为背景透明
// 设置Window flag
wmParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL
| LayoutParams.FLAG_NOT_FOCUSABLE;
// 以屏幕左上角为原点,设置x、y初始值
wmParams.x = 100;
wmParams.y = 0;
// 设置悬浮窗口长宽数据
wmParams.width = CWWindowManager.getScreenWidth() / 20;
wmParams.height = CWWindowManager.getScreenHeight() - 170;
;
// 调整悬浮窗口
wmParams.gravity = Gravity.LEFT | Gravity.CENTER_VERTICAL;
}

这样就完成项目的初步要求。

另外如果需要tab间的切换,你可以使用发通知的方式来添加和清除SeekBar

public void removeSeekBar() {
// TODO Auto-generated method stub
if (cameraSeekBar == null) {
return;
}
wManager.removeView(cameraSeekBar);
cameraSeekBar = null;
}


Android中级第九讲--相机调焦,布布扣,bubuko.com

Android中级第九讲--相机调焦

标签:相机   调焦   竖直seekbar   

原文地址:http://blog.csdn.net/reboot123/article/details/25898071

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!