安卓:surfaceView不能恢复从暂停活动() [英] Android: surfaceView can't resume activity from pause()
本文介绍了安卓:surfaceView不能恢复从暂停活动()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我测试surfaceView,基本上仅仅指刚更改背景颜色的应用程序。
该应用程序没有问题开始,但是当我暂停活动和恢复了,应用程序被挡在了暂停()方法循环。
这是我的code:
导入了java.util.Random;进口android.os.Bundle;
进口android.app.Activity;
进口android.content.Context;
进口android.graphics.Canvas;
进口android.util.Log;
进口android.view.Menu;
进口android.view.SurfaceHolder;
进口android.view.SurfaceView;公共类SurfaceViewTest延伸活动{
FastRenderView的RenderView; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_surface_view_test);
的RenderView =新FastRenderView(本);
的setContentView(的RenderView);
} 保护无效onResume(){
super.onResume();
renderView.resume();
} @覆盖
保护无效的onPause(){
super.onPause();
renderView.pause();
} 公共类FastRenderView扩展SurfaceView实现Runnable {
螺纹renderThread = NULL;
SurfaceHolder持有人;
挥发性布尔运行= FALSE; 随机兰特=新的随机(); //随机数的创造者---- 公共FastRenderView(上下文的背景下){
超级(上下文);
支架= getHolder(); //<< - 检查本声明
}
公共无效简历(){
Log.d(ZR,在恢复);
运行= TRUE;
renderThread =新主题(本);
renderThread.start(); //<<< - AVVIA IL螺纹
}
@覆盖
公共无效的run(){
Log.d(ZR,运行);
而(运行){
尝试{
视频下载(500);
}赶上(InterruptedException的E){
// TODO自动生成catch块
e.printStackTrace();
}
如果(!holder.getSurface()。isValid()的)
继续;
帆布帆布= holder.lockCanvas();
canvas.drawRGB(rand.nextInt(255),rand.nextInt(255),rand.nextInt(255));
holder.unlockCanvasAndPost(画布);
}
}
公共无效暂停(){
运行= FALSE;
Log.d(ZR,在暂停);
而(真){//<<<<<<<<<<<<<< CHECKKKKKK
尝试{
renderThread.join();
Log.d(ZR,在暂停结束);
}赶上(InterruptedException的E){
e.printStackTrace();
}
}
}
}
}
解决方案
的,而(真)
循环无它突破将永不停止方法暂停()
。我希望像一回东西后,成功地加入了渲染线程(等到它结束)
使用的
的while循环中 run()中的
应该没问题。挥发性
声明运行
I am testing surfaceView, basically the app jsut changes the color of the background.
The app starts with no issue but when I "pause" the activity and "resume" it, the app is blocked in the "pause()" method loop.
this is my code:
import java.util.Random;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.util.Log;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class SurfaceViewTest extends Activity {
FastRenderView renderView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_surface_view_test);
renderView= new FastRenderView(this);
setContentView(renderView);
}
protected void onResume() {
super.onResume();
renderView.resume();
}
@Override
protected void onPause() {
super.onPause();
renderView.pause();
}
public class FastRenderView extends SurfaceView implements Runnable {
Thread renderThread=null;
SurfaceHolder holder;
volatile boolean running=false;
Random rand= new Random(); //random number creator----
public FastRenderView(Context context) {
super(context);
holder=getHolder(); //<<-check the this statement
}
public void resume() {
Log.d("ZR", "in resume");
running=true;
renderThread=new Thread(this);
renderThread.start(); //<<<--AVVIA IL THREAD
}
@Override
public void run() {
Log.d("ZR", "in running");
while(running){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(!holder.getSurface().isValid())
continue;
Canvas canvas = holder.lockCanvas();
canvas.drawRGB(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255));
holder.unlockCanvasAndPost(canvas);
}
}
public void pause() {
running = false;
Log.d("ZR", "in pause");
while(true){ //<<<<<<<<<<<<<<CHECKKKKKK
try {
renderThread.join();
Log.d("ZR", "in pause end");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
解决方案
The while(true)
loop without a break in it will never stop in method pause()
. I would expect something like a return after succesfully joined the rendering thread (waited till it ended)
With your volatile
declaration of running
the while-loop in run()
should be ok.
这篇关于安卓:surfaceView不能恢复从暂停活动()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文