我正在努力寻找任何教程来帮助我将 SurfaceView 放入一个盒子中。指向正确方向的指针会很棒 - 不需要通过它寻找一个手。

我希望能够在屏幕顶部分配一个区域,例如做按钮等,然后用一个表面 View 填充其余部分。但是,当我尝试修改用于全屏 SurfaceView 的代码以引用我的 xml 时,一切都有些错误。

我的主要 Activity 代码如下(我删除了很多我认为不相关的部分)。

package com.example.mylistviewidea;

import android.os.Bundle;
etc...

public class MainActivity extends Activity implements OnTouchListener {

MyListView myListView;

@Override
protected 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_alignParentLeft="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

在此先感谢您的帮助!

干杯,

麦克风

最佳答案

您应该有一个自定义 View 并在您的 xml 中使用它。

首先创建这个 View

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_alignParentLeft="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" />

并更改主要 Activity
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();

    }
}

请注意,您也有锁定 Canvas 的错误,但这应该可以正常工作。

关于android - 盒子中的 SurfaceView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14771858/

10-12 06:13