表面拉丝,unlockCanvasAndPost和抛出:IllegalArgumentException [英] Drawing surface, unlockCanvasAndPost and IllegalArgumentException
问题描述
我有一个绘图应用程序的一个问题。下面我有错误,当我离开绘图活动(例如与返回按钮)。我看了看职位,但似乎没有任何帮助我。请,这将意味着很多,如果你可以看看我的drawingsurface活动,因为我一直在试图使它工作了一段时间!非常感谢:)
错误:
03-25 13:41:36.760:E / AndroidRuntime(6642):致命异常:螺纹423
03-25 13:41:36.760:E / AndroidRuntime(6642):java.lang.IllegalArgumentException异常
03-25 13:41:36.760:E / AndroidRuntime(6642):在android.view.Surface.nativeUnlockCanvasAndPost(本机方法)
03-25 13:41:36.760:E / AndroidRuntime(6642):在android.view.Surface.unlockCanvasAndPost(Surface.java:457)
03-25 13:41:36.760:E / AndroidRuntime(6642):在android.view.SurfaceView $ 4.unlockCanvasAndPost(SurfaceView.java:812)
03-25 13:41:36.760:E / AndroidRuntime(6642):在com.ecp.drawing.DrawingSurface $ DrawThread.run(DrawingSurface.java:80)
DrawingSurface的主要code:
公共无效的run(){
帆布帆布= NULL;
而(_run){
如果(isDrawing ==真){
尝试{
帆布= mSurfaceHolder.lockCanvas(NULL);
如果(mBitmap == NULL){
mBitmap = Bitmap.createBitmap(1,1,Bitmap.Config.ARGB_8888);
}
最后帆布C =新的Canvas(mBitmap); c.drawColor(0,PorterDuff.Mode.CLEAR);
canvas.drawColor(0,PorterDuff.Mode.CLEAR);
commandManager.executeAll(C,previewDoneHandler);
previewPath.draw(C); canvas.drawBitmap(mBitmap,0,0,NULL);
} {最后
mSurfaceHolder.unlockCanvasAndPost(画布);
}
} } }
有已经是一个表面的破坏功能:
公共无效surfaceDestroyed(SurfaceHolder持有人){
// TODO自动生成方法存根
布尔重试= TRUE;
thread.setRunning(假);
而(重试){
尝试{
的Thread.join();
重试= FALSE;
}赶上(InterruptedException的E){
//我们会一次又一次地尝试...
}
}
}
我在4.3和阅读,而其他开发人员如何跳过后也有类似的问题的错误我来到了这一点。
步骤要牢记: lockCanvas 画,然后 unlockCanvas
/ **
*注:图纸线程不循环,它只是运行一次,然后退出
* /
@覆盖
公共无效的run(){
/ *这是不可能发生的,但只是要确定... * /
如果(mSurfaceHolder == NULL || mSurfaceView == NULL){
返回;
} / **
*为了工作可靠的Nexus 7,我们把〜500ms的延迟,绘制螺纹的开始
*(AOSP - 发行58385)
* /
如果(android.os.Build.BRAND.equalsIgnoreCase(谷歌)及和放大器;
android.os.Build.MANUFACTURER.equalsIgnoreCase(华硕)及和放大器;
android.os.Build.MODEL.equalsIgnoreCase(的Nexus 7)){ Log.w(这一点,睡眠500毫秒(设备:华硕的Nexus 7)); 尝试{
视频下载(500);
}赶上(InterruptedException的忽略){
}
} 帆布帆布= NULL; 而(mRunning){
尝试{
面表面= mSurfaceHolder.getSurface(); / *检查表面*可用性/
如果(表面=空&放大器;!&放大器; surface.isValid()){ 画布= mSurfaceHolder.lockCanvas(); 同步(mSurfaceHolder){
如果(帆布!= NULL){ // TODO通话绘图code
}
}
} }赶上(例外五){
Log.e(TAG[绘画主题],E); } {最后
/ **
*为此在最后一个,这样如果有异常上述期间抛出,
*我们不会在表面留下不一致的状态
* /
如果(帆布= NULL&放大器;!&安培;!mSurfaceHolder = NULL){
mSurfaceHolder.unlockCanvasAndPost(画布);
}
}
}
}
我希望这可以帮助别人。 ;)
I have a problem with a drawing application. I have the error below when I leave the drawing activity (with the return button for example). I've looked at the posts but nothing seems to help me. Please, it would mean a lot if you could have a look at my drawingsurface activity, because I've been trying to make it work for a while! Thanks a lot :)
Error :
03-25 13:41:36.760: E/AndroidRuntime(6642): FATAL EXCEPTION: Thread-423
03-25 13:41:36.760: E/AndroidRuntime(6642): java.lang.IllegalArgumentException
03-25 13:41:36.760: E/AndroidRuntime(6642): at android.view.Surface.nativeUnlockCanvasAndPost(Native Method)
03-25 13:41:36.760: E/AndroidRuntime(6642): at android.view.Surface.unlockCanvasAndPost(Surface.java:457)
03-25 13:41:36.760: E/AndroidRuntime(6642): at android.view.SurfaceView$4.unlockCanvasAndPost(SurfaceView.java:812)
03-25 13:41:36.760: E/AndroidRuntime(6642): at com.ecp.drawing.DrawingSurface$DrawThread.run(DrawingSurface.java:80)
Main code of DrawingSurface :
public void run() {
Canvas canvas = null;
while (_run){
if(isDrawing == true){
try{
canvas = mSurfaceHolder.lockCanvas(null);
if(mBitmap == null){
mBitmap = Bitmap.createBitmap (1, 1, Bitmap.Config.ARGB_8888);
}
final Canvas c = new Canvas (mBitmap);
c.drawColor(0, PorterDuff.Mode.CLEAR);
canvas.drawColor(0, PorterDuff.Mode.CLEAR);
commandManager.executeAll(c,previewDoneHandler);
previewPath.draw(c);
canvas.drawBitmap (mBitmap, 0, 0,null);
} finally {
mSurfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
}
There is already a surface destroyed function :
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
boolean retry = true;
thread.setRunning(false);
while (retry) {
try {
thread.join();
retry = false;
} catch (InterruptedException e) {
// we will try it again and again...
}
}
}
I had similar issues in 4.3 and after reading a while how other developers bypassed the bug I came to this.
Steps to keep in mind: lockCanvas, draw and then unlockCanvas.
/**
* Note: The drawing thread doesn't loop, it just runs once and exits
*/
@Override
public void run() {
/* This should never happen but just to be sure... */
if (mSurfaceHolder == null || mSurfaceView == null) {
return;
}
/**
* In order to work reliable on Nexus 7, we place ~500ms delay at the start of drawing thread
* (AOSP - Issue 58385)
*/
if (android.os.Build.BRAND.equalsIgnoreCase("google") &&
android.os.Build.MANUFACTURER.equalsIgnoreCase("asus") &&
android.os.Build.MODEL.equalsIgnoreCase("Nexus 7")) {
Log.w(this, "Sleep 500ms (Device: Asus Nexus 7)");
try {
Thread.sleep(500);
} catch (InterruptedException ignored) {
}
}
Canvas canvas = null;
while (mRunning) {
try {
Surface surface = mSurfaceHolder.getSurface();
/* Check availability of surface */
if (surface != null && surface.isValid()) {
canvas = mSurfaceHolder.lockCanvas();
synchronized (mSurfaceHolder) {
if (canvas != null) {
//TODO call drawing code
}
}
}
} catch (Exception e) {
Log.e(TAG, "[Drawing Thread]", e);
} finally {
/**
* Do this in a finally so that if an exception is thrown during the above,
* we don't leave the Surface in an inconsistent state
*/
if (canvas != null && mSurfaceHolder != null) {
mSurfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
}
I hope this can help someone. ;)
这篇关于表面拉丝,unlockCanvasAndPost和抛出:IllegalArgumentException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!