
我很难找到任何教程来帮助我将SurfaceVIEw放在一个盒子里.正确方向的指针将是惊人的 – 不寻找通过它的手握.
我希望能够在屏幕顶部分配一个区域,例如按钮等,然后填充其余的表面视图.但是,当我尝试修改我用于全屏幕SurfaceVIEw的代码来引用我的xml时,这一切都有点不对劲.
我的主要活动的代码如下(我已经删除了很多我认为不相关的位).
package com.example.myListvIEwIDea;import androID.os.Bundle;etc...public class MainActivity extends Activity implements OntouchListener {MyListVIEw myListVIEw;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); myListVIEw = (MyListVIEw) findVIEwByID(R.ID.surfaceVIEw1);}class MyListVIEw extends SurfaceVIEw implements Runnable { SurfaceHolder myHolder; Thread myThread = null; boolean isRunning = false; // My Variables long dT; int myRed; public MyListVIEw(Context context) { super(context); myHolder = getHolder(); } @OverrIDe public voID run() { // Todo auto-generated method stub while (isRunning) { if (!myHolder.getSurface().isValID()) continue; Canvas canvas = myHolder.lockCanvas(); canvas.drawARGB(128, 0, 0, 0); dT = SystemClock.uptimeMillis(); myRed = (int) ((int) 128*(1+Math.sin((double) dT/1000))); } }}}我的XML是:
<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"xmlns:tools="http://schemas.androID.com/tools"androID:layout_wIDth="match_parent"androID:layout_height="match_parent"tools:context=".MainActivity" ><SurfaceVIEw androID:ID="@+ID/surfaceVIEw1" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:layout_alignParent@R_419_6823@="true" /><button androID:ID="@+ID/button1" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignParentRight="true" androID:layout_alignParenttop="true" androID:text="@string/add_entrIEs" /><button androID:ID="@+ID/button2" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignParentRight="true" androID:layout_below="@+ID/button1" androID:text="@string/remove_entrIEs" /></relativeLayout>我得到的错误是:
02-08 11:44:17.885: E/AndroIDRuntime(10523): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myListvIEwIDea/com.example.myListvIEwIDea.MainActivity}: java.lang.classCastException: androID.vIEw.SurfaceVIEw cannot be cast to com.example.myListvIEwIDea.MyListVIEw在此先感谢您的帮助!
干杯,
麦克风
解决方法:
您应该有一个自定义视图并在xml中使用它.
首先创建此视图
class MyListVIEw extends SurfaceVIEw implements Runnable { SurfaceHolder myHolder; Thread myThread = null; boolean isRunning = true; // My Variables long dT; int myRed; public MyListVIEw(Context context) { super(context); init(); } public MyListVIEw(Context context, AttributeSet attrs) { super(context, attrs); init(); } public MyListVIEw(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private voID init() { myHolder = getHolder(); myHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } @OverrIDe public voID run() { // Todo auto-generated method stub while (isRunning) { if (!myHolder.getSurface().isValID()) continue; Canvas canvas = myHolder.lockCanvas(); canvas.drawARGB(128, 128, 0, 0); dT = SystemClock.uptimeMillis(); myRed = (int) ((int) 128 * (1 + Math.sin((double) dT / 1000))); myHolder.unlockCanvasAndPost(canvas); } }}将您的xml更改为此
<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"xmlns:tools="http://schemas.androID.com/tools"androID:layout_wIDth="match_parent"androID:layout_height="match_parent"tools:context=".MainActivity" ><com.example.testant.MyListVIEw androID:ID="@+ID/surfaceVIEw1" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:layout_alignParent@R_419_6823@="true" /><button androID:ID="@+ID/button1" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignParentRight="true" androID:layout_alignParenttop="true" androID:text="add_entrIEs" /><button androID:ID="@+ID/button2" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignParentRight="true" androID:layout_below="@+ID/button1" androID:text="remove_entrIEs" />并改变主要活动
public class MainActivity extends Activity { MyListVIEw myListVIEw; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); myListVIEw = (MyListVIEw) findVIEwByID(R.ID.surfaceVIEw1); new Thread(myListVIEw).start(); }}请注意,您还有一个锁定画布的错误,但这应该可以正常工作.
总结以上是内存溢出为你收集整理的android – 一个盒子里的SurfaceView全部内容,希望文章能够帮你解决android – 一个盒子里的SurfaceView所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)