2011-11-06 123 views
0

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

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

再次感谢! :)

+0

您是否制作了复制粘贴错误?我没有看到任何代码从xml中膨胀自定义视图?如下:'CustomViewOne cvo =(CustomViewOne)findViewById(R.id.customSurfaceViewOne1)' –

+0

对不起,我比较新,而且我不太明白我需要做什么。我应该在我的父java类或我的自定义视图类中做到这一点?我想我可能会忘记这一点,因此无法正常工作。你能否详细说明一下?另外,不要我已经有自定义视图等于? newSeshView = new CustomSurfaceViewOne(this,null); – Jarnuman

+0

那么,在你最后的代码片段中,你说你正在尝试使用xml中的自定义视图。但是,该片段与上面的代码片段相同,并显示了您的视图的程序化实例。如果你正在寻求使用在你的xml文件中声明的视图,你应该从它膨胀你的视图。这意味着在'onCreate()'的某个地方你必须做'setContentView(R.layout.newsession);'然后你可以做CustomViewOne cvo =(CustomViewOne)findViewById(R.id.customSurfaceViewOne1)来获得你的在xml布局文件中声明的自定义视图。 –

回答

0

我想你可能会失踪invallidate()对其触摸阅读代表的调用。

我不确定你的代码到底发生了什么。但是,如果我会创建自己的视图,并像我一样将其添加到自己的布局中。 ,并希望它来改变自己的阅读触摸事件的话,我将在myown视图类是自我可以做这样的事情

@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 

    } 

希望它能帮助:)

+0

对不起,我不太明白你想说什么。我试图添加invallidate();到我的OnTouchEvent结束,但它出现了一个错误“方法invallidate()未定义类型NewSesh” – Jarnuman

+0

拼写错误类型invalidate()。并验证你的视图类是默认视图或其任何子类的子类 – Javanator

+0

当我尝试invalidate()时它仍然给我一个错误;说它没有在我的java类中定义。你是什​​么意思'验证你的视图类是默认视图或其任何子类的子类'? – Jarnuman

0

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

3个文件,你的主要活动,你的surfaceview和你的XML文件。

你的XML文件看起来不错,你有surfaceview它

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

在您的活动,增加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; 
} 

WH myOnTouch()是您的customSurfaceViewOne1类中的一种方法,您可以在其中完成所有ontTouch事件。通知我通过了MotionEvent event

再一次,我不确定这是否是正确的方式来做到这一点,这只是我如何得到它的工作。我做这件事的原因就是我可以在我的表面看到一个admob广告。

相关问题