我正在实现一个自定义相机,我使用 onPreviewFrame() 进行实时效果。主要效果(fx1)是一个可运行的并且似乎没问题,在需要时会丢帧。
当 fx1 关闭时,第二个更简单的 fx (fx2) 运行。
当 fx2 在主线程上运行时没问题(它在 2 毫秒内运行),但有时我看到 ANR,所以我决定将它移到第二个可运行的,但现在导致应用程序冻结。
class FX2Runnable implements Runnable {
private byte[] data;
FX2Runnable(byte[] _data) {
data = _data;
}
public void run() {
hview.FX2(data);
hview.post(new Runnable() {
public void run() {
hview.postInvalidate();
//.invalidate(); caused ANR!!
}
});
FLAG_FX2_PROCESSING = false;
}
}
我是这样称呼它的
private PreviewCallback previewCallback=new PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera cam) {
if(FX==1) {
// FX1
if(FLAG_FX1_PROCESSING) {
//skip
} else {
FLAG_FX1_PROCESSING = true;
FX1Thread = new Thread(new FX1Runnable());
FX1Thread.start();
}
} else {
// FX2
if(FLAG_FX2_PROCESSING) {
//skip
} else {
FLAG_FX2_PROCESSING = true;
FX2Thread = new Thread(new FX2Runnable(data));
FX2Thread.start();
}
/* this was the old code and was working fine
but i needed to solve some spare ANR
//hview.FX2(data);
//hview.invalidate();
*/
}
}
}
我究竟做错了什么!?
顺便说一句,我似乎无法在模拟器上冻结应用程序,而只能在我的设备上冻结。
编辑
尝试使用 runOnUiThread(Runnable) 而不是 hview.post(Runnable),它似乎有效,但我只需要等待几分钟才能再次看到冻结的应用程序!它稍后会发生,但它会再次发生!
这是一个日志猫
08-26 17:32:13.398: I/ActivityManager(250): Displayed it.jcsoft.abbracadabbra/.EnhancedCameraPreviewActivity: +3s576ms
08-26 17:32:13.414: W/IInputConnectionWrapper(459): showStatusIcon on inactive InputConnection
08-26 17:32:13.441: I/GPS(9149): new network location: Location[mProvider=network,mTime=1377531132668,mLatitude=44.1459721,mLongitude=12.4545174,mHasAltitude=false,mAltitude=0.0,mHasSpeed=false,mSpeed=0.0,mHasBearing=false,mBearing=0.0,mHasAccuracy=true,mAccuracy=43.887,mExtras=Bundle[mParcelledData.dataSize=212]]
08-26 17:32:15.441: D/dalvikvm(250): WAIT_FOR_CONCURRENT_GC blocked 0ms
08-26 17:32:15.867: D/dalvikvm(250): GC_EXPLICIT freed 1300K, 23% free 16030K/20615K, paused 3ms+41ms, total 428ms
08-26 17:32:37.707: D/dalvikvm(250): GC_CONCURRENT freed 1878K, 22% free 16105K/20615K, paused 4ms+110ms, total 888ms
08-26 17:32:37.855: D/dalvikvm(9149): null clazz in OP_INSTANCE_OF, single-stepping
08-26 17:32:49.375: D/dalvikvm(834): GC_CONCURRENT freed 512K, 9% free 9179K/9991K, paused 336ms+218ms, total 4454ms
08-26 17:32:49.375: D/dalvikvm(834): WAIT_FOR_CONCURRENT_GC blocked 1740ms
08-26 17:32:58.605: D/dalvikvm(250): GC_CONCURRENT freed 1934K, 22% free 16098K/20615K, paused 5ms+32ms, total 284ms
08-26 17:33:00.070: D/SizeAdaptiveLayout(335): com.android.internal.widget.SizeAdaptiveLayout@422dcb90child view android.widget.FrameLayout@422edb18 measured out of bounds at 95px clamped to 96px
08-26 17:33:00.093: D/SizeAdaptiveLayout(335): com.android.internal.widget.SizeAdaptiveLayout@4231b860child view android.widget.FrameLayout@4231d180 measured out of bounds at 95px clamped to 96px
08-26 17:33:10.257: D/dalvikvm(9149): GC_CONCURRENT freed 455K, 4% free 14709K/15303K, paused 3ms+4ms, total 78ms
08-26 17:33:20.082: D/dalvikvm(250): GC_CONCURRENT freed 1933K, 22% free 16091K/20615K, paused 5ms+30ms, total 282ms
08-26 17:33:23.379: I/InputDispatcher(250): Application is not responding: Window{425f23b0 it.jcsoft.abbracadabbra/it.jcsoft.abbracadabbra.EnhancedCameraPreviewActivity paused=false}. It has been 5002.6ms since event, 5002.5ms since wait started. Reason: Waiting because the touched window has not finished processing the input events that were previously delivered to it.
08-26 17:33:23.379: I/WindowManager(250): Input event dispatching timed out sending to it.jcsoft.abbracadabbra/it.jcsoft.abbracadabbra.EnhancedCameraPreviewActivity
08-26 17:33:28.437: I/InputDispatcher(250): Dropped event because it is stale.
请帮忙!