【发布时间】:2015-06-19 17:09:56
【问题描述】:
我创建了一个自定义视图类并为其附加了一个线程。然后,我使用相应活动的 xml 布局文件添加了视图,其中还包含另一个视图,例如工具栏和文本视图。但是添加自定义视图时不显示视图;只是具有默认颜色的背景。没有错误。线程工作正常,我可以从调试日志中看到每一帧的时间差。但是,我看不出为什么没有显示任何视图。感谢您的帮助...
xml 布局:
<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>
自定义视图类:
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 by 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 the game loop thread
thread = new MyCanvasThread(getHolder(),this);
// make the GamePanel focusable so it 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) {
}
}
而我对应的线程类是:
package com.mert.canvastutorial;
import android.graphics.Canvas;
import android.util.Log;
import android.view.SurfaceHolder;
/**
* Created by 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++;
}
}
} finally {
if (c!= null) {
surfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
}
我将自定义视图从 xml 布局文件添加到活动中:
【问题讨论】:
标签: android android-layout android-canvas surfaceview