android - Custom view with a drawing thread prevents all views from displaying -
i have created custom view class , attached thread it. then, added view using xml layout file of corresponding activity has views in such toolbar , textview. no view displayed when adding custom view; background default color. there no errors. thread working fine , can see time difference each frame debug log. however, cannot see why view not being displayed. help...
the xml layout:
<linearlayout 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" android:orientation="vertical" android:focusable="false"> <android.support.v7.widget.toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toolbar" android:minheight="?attr/actionbarsize" android:background="?attr/colorprimary" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/themeoverlay.appcompat.dark.actionbar" android:popuptheme="@style/themeoverlay.appcompat.light"> </android.support.v7.widget.toolbar> <linearlayout 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" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" tools:context=".homeactivity" android:orientation="vertical" android:id="@+id/background" android:focusable="false" android:animatelayoutchanges="true"> <com.puppetlabs.canvastutorial.mycanvasview android:layout_width="100dp" android:layout_height="100dp" android:id="@+id/mycanvasview"/> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:text="large text" android:id="@+id/textview" /> </linearlayout> </linearlayout>
the custom view class:
package com.mert.canvastutorial; import android.content.context; import android.graphics.canvas; import android.graphics.color; import android.util.attributeset; import android.util.log; import android.view.surfaceholder; import android.view.surfaceview; /** * created pc on 19.6.2015. */ public class mycanvasview extends surfaceview implements surfaceholder.callback{ private mycanvasthread thread; private void init() { getholder().addcallback(this); log.d("surface_view", "initialized."); // create game loop thread thread = new mycanvasthread(getholder(),this); // make gamepanel focusable can handle events setfocusable(true); } public mycanvasview(context context) { super(context); init(); } public mycanvasview(context context, attributeset attrs) { super(context, attrs); init(); } public mycanvasview(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); init(); } public void update(){ } public void render(canvas c){ c.drawcolor(color.red); } @override protected void ondraw(canvas canvas) { //super.ondraw(canvas); //canvas.drawcolor(color.parsecolor("#00dd00")); } @override public void surfacecreated(surfaceholder holder) { thread.setrunning(true); thread.start(); } @override public void surfacechanged(surfaceholder holder, int format, int width, int height) { boolean retry = true; while (retry) { try { thread.join(); retry = false; } catch (interruptedexception e) {} } } @override public void surfacedestroyed(surfaceholder holder) { } }
and corresponding thread class is:
package com.mert.canvastutorial; import android.graphics.canvas; import android.util.log; import android.view.surfaceholder; /** * created pc on 19.6.2015. */ public class mycanvasthread extends thread { private final static int max_fps = 30; private final static int max_frame_skips = 5; private final static int frame_period = 1000/max_fps; private surfaceholder surfaceholder; private mycanvasview mycanvasview; private boolean _run = false; public mycanvasthread (surfaceholder surfaceholder, mycanvasview mycanvasview) { this.mycanvasview = mycanvasview; this.surfaceholder = surfaceholder; log.d("thread","initialized."); } public void setrunning (boolean _run) { this._run = _run; } @override public void run() { //super.run(); canvas c; long begintime; long timediff; int sleeptime; int framesskipped; sleeptime = 0; while (_run) { c = null; try { c = surfaceholder.lockcanvas(null); synchronized (surfaceholder) { begintime = system.currenttimemillis(); framesskipped = 0; mycanvasview.update(); mycanvasview.render(c); timediff = system.currenttimemillis() - begintime; log.d("time",string.valueof(timediff)); sleeptime = (int) (frame_period - timediff); if (sleeptime>0) { try{ thread.sleep(sleeptime); } catch (interruptedexception e) {} } while (sleeptime < 0 && framesskipped < max_frame_skips) { this.mycanvasview.update(); sleeptime += frame_period; framesskipped++; } } } { if (c!= null) { surfaceholder.unlockcanvasandpost(c); } } } } }
i added custom view activity xml layout file:
a "custom view" different subclassing surfaceview. surfaceview has 2 parts, surface , view. if you're going draw on view part, want use custom view, , there's no need have surfaceview @ all. (see creating custom views more approach.)
since you're trying draw on surface of surfaceview, don't want override ondraw()
, , in fact don't need subclass surfaceview @ all. best practice favor composition on inheritance.
the view part of surfaceview intended transparent rectangle layout code uses leave "hole" in view layer. surfaceview surface on separate layer that, default, sits behind view layer. if set background view or draw on it, obscure surface, , won't able see anything.
you can trivially determine whether case moving surfaceview on top, using setzorderontop()
in oncreate()
(the call has no effect after surface has been created). if surface content appears when this, problem have filled in view part of surfaceview.
the "multi-surface test" activity in grafika demonstrates animated canvas rendering on multiple overlapping surfaceviews.
fwiw, recommend looking @ surfaceview lifecycle , game loop sections of graphics architecture doc.
Comments
Post a Comment