android - SurfaceView in a Box -
i'm struggling find tutorial help me set surfaceview in box. pointer in right direction amazing - not looking hand hold through it.
i'd able apportion area @ top of screen illustration buttons etc , have surfaceview filling rest. however, when seek modify code i've used total screen surfaceview reference xml, goes bit wrong.
my code main activity follows (i've stripped out lot of bits think aren't 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; // 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 is:
<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'm getting is:
02-08 11:44:17.885: e/androidruntime(10523): java.lang.runtimeexception: unable start activity componentinfo{com.example.mylistviewidea/com.example.mylistviewidea.mainactivity}: java.lang.classcastexception: android.view.surfaceview cannot cast com.example.mylistviewidea.mylistview
thanks in advance help!
cheers,
mike
you should have custom view , utilize in xml.
first create view
class mylistview extends surfaceview implements runnable { surfaceholder myholder; thread mythread = null; boolean isrunning = true; // 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 xml this
<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 alter main 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(); } }
note have bug lock canvas, should work fine.
android surfaceview
No comments:
Post a Comment