使用 OnTouchListener 将自定义视图添加到 XML 布局

发布于 2024-12-14 10:12:22 字数 2040 浏览 3 评论 0 原文

我一直在开发一个应用程序,其中用户点击屏幕时画布上会出现一个球(位图)。背景是一个xml布局setContentView(R.layout.newsession)。画布是黑色的画布。当我设置 java 父类 setContentView(customView) 时,程序工作正常,但是当我将自定义表面视图添加到 XML 布局和 setContentView(R.layout.newsession) 时,屏幕仅显示画布,而 OnTouch 事件则不显示不工作。我做错了什么吗?我已经为此工作了将近一周,我真的需要帮助。我将在下面发布 XML 布局和自定义 SurfaceView 的代码。提前致谢!

XML 布局 (newsession)

    <FrameLayout 
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
     >

    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/newSessionPage" 
    >   

    <ImageView 
      android:layout_width="231dp" 
      android:id="@+id/ivStrikeGrid" 
      android:layout_gravity="center" 
      android:layout_height="270dp"
      android:layout_marginTop="18dp" 
      android:src="@drawable/strike_grid"
      android:layout_marginBottom="10dp"
    />

    <appsys.studios.CustomSurfaceViewOne 
     android:id="@+id/customSurfaceViewOne1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"></appsys.studios.CustomSurfaceViewOne
    >

  </FrameLayout>

自定义 SurfaceView

   package appsys.studios;
   public class CustomSurfaceViewOne extends SurfaceView implements Runnable{
       public CustomSurfaceViewOne(Context context, AttributeSet attr) {
          super(context, attr);
          ourHolder = getHolder();
     }
      // Other stuff
  }

它工作正常,如下所示:

    newSeshView = new CustomSurfaceViewOne(this, null);
    setContentView(newSeshView);

但是当我尝试从 XML 布局使用它时,没有任何反应,如下所示:

    newSeshView = new CustomSurfaceViewOne(this, null);
    setContentView(R.layout.newsession);

再次感谢! :)

I've been working on an app where a ball (bitmap) appears on the canvas at the point where the user taps on the screen. The background is an xml layout setContentView(R.layout.newsession). The canvas is a black painted canvas. When i set my java parent class setContentView(customView), the program works fine but when I add the custom surface view to my XML layout and setContentView(R.layout.newsession), the screen just shows the canvas, and the OnTouch event doesn't work. Am I doing something wrong? I've been working on this for almost a week now and I really need help. I will post my code below for the XML layout and custom surfaceView. Thanks in advance!

XML layout (newsession)

    <FrameLayout 
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
     >

    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/newSessionPage" 
    >   

    <ImageView 
      android:layout_width="231dp" 
      android:id="@+id/ivStrikeGrid" 
      android:layout_gravity="center" 
      android:layout_height="270dp"
      android:layout_marginTop="18dp" 
      android:src="@drawable/strike_grid"
      android:layout_marginBottom="10dp"
    />

    <appsys.studios.CustomSurfaceViewOne 
     android:id="@+id/customSurfaceViewOne1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"></appsys.studios.CustomSurfaceViewOne
    >

  </FrameLayout>

Custom SurfaceView

   package appsys.studios;
   public class CustomSurfaceViewOne extends SurfaceView implements Runnable{
       public CustomSurfaceViewOne(Context context, AttributeSet attr) {
          super(context, attr);
          ourHolder = getHolder();
     }
      // Other stuff
  }

It works fine like this:

    newSeshView = new CustomSurfaceViewOne(this, null);
    setContentView(newSeshView);

But nothing happens when I try to use it from the XML layout, like this:

    newSeshView = new CustomSurfaceViewOne(this, null);
    setContentView(R.layout.newsession);

Thanks again! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦回梦里 2024-12-21 10:12:22

我认为您可能错过了对其触摸读取委托的视图的 invallidate() 调用。

我不确定您的代码中到底发生了什么。但是,如果我像您一样创建自己的视图并将其添加到我自己的布局中。
并希望它在读取触摸事件时改变自身,那么我会做类似的事情
在我自己的视图类中

   @override
    // i dont remem exact signature of this method. google it or see docs
    // motive is to read touch event of view and doing appropriate changes
    // and redrawing the view again
   public void onTouchEvent(MotionEvent me)
   {
      doCalculation(); // change points where to draw the ball next time. Read me
      invalidate();  // tell the view re draw it self

   }

希望它有帮助:)

I think you might be missing invallidate() call of the view on its touch reading delegates.

I am not sure exactly what is happening in your code. But if i would be creating my own view and adding it in my own layout as you did.
And want it to change itself on reading touch events then i would be doing something like
in the myown view class it self

   @override
    // i dont remem exact signature of this method. google it or see docs
    // motive is to read touch event of view and doing appropriate changes
    // and redrawing the view again
   public void onTouchEvent(MotionEvent me)
   {
      doCalculation(); // change points where to draw the ball next time. Read me
      invalidate();  // tell the view re draw it self

   }

Hope it helps :)

林空鹿饮溪 2024-12-21 10:12:22

我遇到了同样的问题,在寻找答案时发现了你的问题。我解决了它,但我不确定这是否是正确的方法。

3 个文件,您的主要活动、表面视图和 XML 文件。

您的 XML 文件看起来不错,其中包含 SurfaceView

<appsys.studios.CustomSurfaceViewOne 
 android:id="@+id/customSurfaceViewOne1" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content"></appsys.studios.CustomSurfaceViewOne
>

在您的 Activity 中,添加 implements OnTouchListener,使用 setContentView(R.layout.newsession); ,然后仍然在您的 onCreate() 添加此行 CustomViewOne cvo = (CustomViewOne)findViewById(R.id.customSurfaceViewOne1) 并将侦听器设置为cvo.setOnTouchListener(this);

然后添加 onTouch

@Override
public boolean onTouch(View v, MotionEvent event) {
    customSurfaceViewOne1.myOnTouch(event);
    return false;
}

,其中 myOnTouch() 是您的 customSurfaceViewOne1 类中的一个方法,您将在其中执行所有 ontTouch 事件。请注意,我传入了 MotionEvent 事件

再说一次,我不确定这是否是正确的方法,这只是我让它工作的方法。我这样做的原因只是为了我可以在我的 SurfaceView 上方放置一个 Admob 广告,哈哈。

I ran into the same issue, and found your question while looking for the answer. I solved it, but I'm not sure it is the proper way.

3 files, your main activity, your surfaceview, and your XML file.

Your XML file looks ok, you have the surfaceview in it

<appsys.studios.CustomSurfaceViewOne 
 android:id="@+id/customSurfaceViewOne1" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content"></appsys.studios.CustomSurfaceViewOne
>

In your activity, add implements OnTouchListener, use setContentView(R.layout.newsession); and after that, still in your onCreate() add this line CustomViewOne cvo = (CustomViewOne)findViewById(R.id.customSurfaceViewOne1) and set the listener to it by cvo.setOnTouchListener(this);

then add your onTouch

@Override
public boolean onTouch(View v, MotionEvent event) {
    customSurfaceViewOne1.myOnTouch(event);
    return false;
}

where myOnTouch() is a method in your customSurfaceViewOne1 class where you will do all your ontTouch events. Notice I passed in the MotionEvent event.

Again, I'm not sure if this is the proper way to do it, it's just how I got it to work. The reason I did it was just so I could have an admob ad above my surfaceview lol.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文