SurfaceView in the box

I am trying to find any tutorial that can help me put SurfaceView in the field. A pointer in the right direction would be awesome - without looking at it with your hand.

I would like to be able to distribute an area at the top of the screen, for example, to do buttons, etc., and then fill the rest. However, when I try to change the code I used for the full screen surfaceView to link to my xml, things go a little wrong.

My code for my main activity looks like this (I've removed a lot of bits that I don't think are relevant).

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

        }
    }
}
}

      

My 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>

      

The error I am getting:

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

      

Thanks in advance for your help!

Greetings,

Mike

+3


source to share


2 answers


You must have a custom view and use it in your xml.

First create this 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);

        }
    }
}

      

Change your xml to

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

      



and change the main action

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

    }
}

      

Note that you also have a bug with the lock canvas, but that should work fine.

+5


source


This can be a little trivial. But try to clean the project and then rebuild. OR Declare MyListView in a separate java class file and reference it in your xml layout file,



0


source







All Articles