我是Android的新手,所以请原谅!
我正在玩一些相机代码(可在网上找到),并且想在屏幕上显示/隐藏一些按钮.当用户触摸屏幕时,我希望它捕获图像.
我的设置:
1.
主要活动:
public class CameraDemo extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_inuse);
preview = new Preview(this);
((FrameLayout) findViewById(R.id.preview)).addView(preview);
... ...
// rest of the code that captures the image when a button is pressed.
// the button is defined in main.xml with button id ButtonClicked
}
2.
预览类如下所示:
class Preview extends SurfaceView implements SurfaceHolder.Callback {
SurfaceHolder mHolder;
public Camera camera;
Preview(Context context) {
super(context);
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
我的问题是
>如何添加触摸功能,以便用户可以触摸预览(比如说一秒钟,或者只是快速触摸),然后会发生什么? (说的图像已保存)
>例如,一个按钮会出现在表面上,例如“下一步”按钮?
解决方法:
@Override
public boolean onTouchEvent(MotionEvent event) {
return super.onTouchEvent(event);
}
在您的预览类中,MotionEvent对象将告诉您它的触摸方式(以及位置等),并让您执行您想做的任何事情.