如何触摸监听器添加到表面的看法? [英] How to add a touch listener to a surface view?
问题描述
我是新来的Android,所以请原谅如果是问过!
我与一些相机code打(在网上找到),我想显示/隐藏屏幕上的一些按钮。当用户触摸屏幕时,我希望它捕捉图像。
我的设置:
1。
主要活动:
公共类CameraDemo延伸活动{
/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main_inuse); preVIEW =新的preVIEW(本); ((的FrameLayout)findViewById(R.id. preVIEW))addView(preVIEW)。......
//将code,抓住当按钮为pressed图像的其余部分。
//按钮被main.xml中定义的按钮的ID ButtonClicked
}
2。
在preVIEW类看起来是这样的:
类preVIEW延伸SurfaceView实现SurfaceHolder.Callback { SurfaceHolder mHolder;
公共摄像头摄像头; preVIEW(上下文的背景下){
超级(上下文); //安装SurfaceHolder.Callback所以我们得到通知时,
//创建下垫面和销毁。
mHolder = getHolder();
mHolder.addCallback(本);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
我的问题是,
-
我如何添加触摸功能,使用户可以触摸preVIEW(说的第二个,或者只是快速触摸)和有事? (说的图像保存)
-
和一个按钮会出现在表面上说下一步按钮,例如?
@覆盖
公共布尔onTouchEvent(MotionEvent事件){
返回super.onTouchEvent(事件);
}
在您的preVIEW类中,MotionEvent对象将告诉你这是什么样的触摸(和位置等等等等),让你做任何你想做的事情。
I am new to Android, so please excuse if it is asked before!
I am playing with some camera code (found online) and I want to show/hide some buttons on screen. When the user touches the screen, I want it to capture the image.
My setup:
1. Main Activity:
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. The preview class looks like this:
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);
}
My question is,
How can I add touch functionality so that the user can touch the preview (say for a second, or just touch quickly) and something happens ? (Say image is saved)
And a button will appear on the surface say a "Next" button, for example?
@Override
public boolean onTouchEvent(MotionEvent event) {
return super.onTouchEvent(event);
}
in your preview class , the MotionEvent object will tell you what kind of touch it is (and position etc etc) and let you do whatever you want to do.
这篇关于如何触摸监听器添加到表面的看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!