自定义了一个画板
底层libc报空指针,求教定位或解决办法
在线等
01-16 10:56:11.764 15941-15941/com.cs.sls.smarterlecturestudents E/SketchView: *2* drawBitmap: ---->drawBitmap tempBitmap after
01-16 10:56:11.852 304-304/? A/DEBUG: *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
01-16 10:56:11.852 304-304/? A/DEBUG: Build fingerprint: 'Lenovo/YT3-X50F/YT3-X50F:6.0.1/MMB29M/YT3-X50F_USR_S113_170323_Q1241_PRC:user/release-keys'
01-16 10:56:11.852 304-304/? A/DEBUG: Revision: '0'
01-16 10:56:11.852 304-304/? A/DEBUG: ABI: 'arm'
01-16 10:56:11.852 304-304/? A/DEBUG: pid: 15941, tid: 15949, name: FinalizerDaemon >>> com.cs.sls.smarterlecturestudents <<<
01-16 10:56:11.852 304-304/? A/DEBUG: signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0xb8891d68
01-16 10:56:11.889 304-304/? A/DEBUG: r0 b74360c8 r1 b418b58c r2 b8891d68 r3 ffffffff
01-16 10:56:11.889 304-304/? A/DEBUG: r4 b8891d68 r5 6f5e8b24 r6 13051860 r7 b8891d68
01-16 10:56:11.890 304-304/? A/DEBUG: r8 ffffffff r9 b73ee2d0 sl 12fb8400 fp 13051860
01-16 10:56:11.890 304-304/? A/DEBUG: ip b6ede9d1 sp b418b578 lr 720e24c7 pc b6ede9d4 cpsr 60070030
01-16 10:56:11.892 304-304/? A/DEBUG: backtrace:
01-16 10:56:11.893 304-304/? A/DEBUG: #00 pc 000969d4 /system/lib/libandroid_runtime.so
01-16 10:56:11.893 304-304/? A/DEBUG: #01 pc 021984c5 /system/framework/arm/boot.oat (offset 0x1eff000)
01-16 10:56:12.791 816-16229/? W/ActivityManager: Force finishing activity com.cs.sls.smarterlecturestudents/.ui.activity.WhiteBoardActivity
核心onDraw方法如下:
private Bitmap tempBitmap;//临时绘制的bitmap
private Canvas tempCanvas;
private Bitmap tempHoldBitmap;//保存已固化的笔画bitmap
private Canvas tempHoldCanvas;
public void drawRecord(Canvas canvas) {
//同步绘制
if (curSketchData != null) {
//新建一个临时画布,以便橡皮擦生效
Log.e(TAG, "drawRecord: ---->createBitmap tempBitmap");
if (tempBitmap == null) {
tempBitmap = Bitmap.createBitmap(getWidth() / drawDensity, getHeight() / drawDensity, Bitmap.Config.ARGB_4444);
tempCanvas = new Canvas(tempBitmap);
}
//新建一个临时画布,以便保存过多的画笔
Log.e(TAG, "drawRecord: ---->createBitmap tempHoldBitmap");
if (tempHoldBitmap == null) {
tempHoldBitmap = Bitmap.createBitmap(getWidth() / drawDensity, getHeight() / drawDensity, Bitmap.Config.ARGB_4444);
tempHoldCanvas = new Canvas(tempHoldBitmap);
}
//把十个操作以前的笔画全都画进固化层
while (curSketchData.strokeRecordList.size() > 10) {
StrokeRecord record = curSketchData.strokeRecordList.get(0);
strokePaint.setColor(record.color);
strokePaint.setStrokeWidth(record.strokeWidth);
Paint paint = new Paint(strokePaint);
switch (record.type){
case STROKE_TYPE_ERASER:
strokePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));//关键代码
Path eraserPath = new Path();
strokePaint.setColor(Color.WHITE);
strokePaint.setStrokeWidth(eraserSize);
paint = new Paint(strokePaint);
eraserPath.moveTo(record.downX, record.downY);
//历史路径
for (StrokeDrawEarserEntity listPathHi : record.listPathHis) {
eraserPath.quadTo(listPathHi.preX, listPathHi.preY, (listPathHi.curX + listPathHi.preX) / 2, (listPathHi.curY + listPathHi.preY) / 2);
}
tempHoldCanvas.drawPath(eraserPath,paint);
Log.e(TAG, "drawRecord: ---->tempHoldCanvas eraserPath");
break;
case STROKE_TYPE_DRAW:
Path drawPath = new Path();
drawPath.moveTo(record.downX, record.downY);
for (StrokeDrawEarserEntity listPathHi : record.listPathHis) {
drawPath.quadTo(listPathHi.preX, listPathHi.preY, (listPathHi.curX + listPathHi.preX) / 2, (listPathHi.curY + listPathHi.preY) / 2);
}
tempHoldCanvas.drawPath(drawPath, paint);
Log.e(TAG, "drawRecord: ---->tempHoldCanvas drawPath");
break;
case STROKE_TYPE_LINE:
Path linePath = new Path();
linePath.moveTo(record.downX, record.downY);
linePath.lineTo(record.upX, record.upY);
tempHoldCanvas.drawPath(linePath, paint);
Log.e(TAG, "drawRecord: ---->tempHoldCanvas linePath");
break;
case STROKE_TYPE_CIRCLE:
tempHoldCanvas.drawOval(record.rect, paint);
Log.e(TAG, "drawRecord: ---->tempHoldCanvas drawOval");
break;
case STROKE_TYPE_RECTANGLE:
tempHoldCanvas.drawRect(record.rect, paint);
Log.e(TAG, "drawRecord: ---->tempHoldCanvas drawRect");
break;
case STROKE_TYPE_TEXT:
if (record.text != null) {
TextPaint tp = new TextPaint();
tp.setTextSize(record.textSize);
tp.setColor(record.color);
StaticLayout layout = new StaticLayout(record.text, tp, record.textWidth, Layout.Alignment.ALIGN_NORMAL, 1.0F, 0.0F, true);
tempCanvas.translate(record.textOffX, record.textOffY);
layout.draw(tempCanvas);
tempCanvas.translate(-record.textOffX, -record.textOffY);
}
Log.e(TAG, "drawRecord: ---->tempHoldCanvas STROKE_TYPE_TEXT");
break;
}
curSketchData.strokeRecordList.remove(0);
}
clearCanvas(tempCanvas);//清空画布
tempCanvas.drawColor(Color.TRANSPARENT);
Log.e(TAG, "drawBitmap: ---->drawBitmap tempHoldBitmap before");
tempCanvas.drawBitmap(tempHoldBitmap, new Rect(0, 0, tempHoldBitmap.getWidth(), tempHoldBitmap.getHeight()), new Rect(0, 0, tempCanvas.getWidth(), tempCanvas.getHeight()), null);
Log.e(TAG, "drawBitmap: ---->drawBitmap tempHoldBitmap after");
for (StrokeRecord record : curSketchData.strokeRecordList) {
strokePaint.setXfermode(null);//关键代码
strokePaint.setColor(record.color);
strokePaint.setStrokeWidth(record.strokeWidth);
Paint paint = new Paint(strokePaint);
switch (record.type){
case STROKE_TYPE_ERASER:
strokePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));//关键代码
Path eraserPath = new Path();
strokePaint.setColor(Color.WHITE);
strokePaint.setStrokeWidth(eraserSize);
paint = new Paint(strokePaint);
eraserPath.moveTo(record.downX, record.downY);
//历史路径
for (StrokeDrawEarserEntity listPathHi : record.listPathHis) {
eraserPath.quadTo(listPathHi.preX, listPathHi.preY, (listPathHi.curX + listPathHi.preX) / 2, (listPathHi.curY + listPathHi.preY) / 2);
}
tempHoldCanvas.drawPath(eraserPath,paint);
tempCanvas.drawPath(eraserPath, paint);
Log.e(TAG, "*2* drawPath: ---->drawPath eraserPath");
break;
case STROKE_TYPE_DRAW:
Path drawPath = new Path();
drawPath.moveTo(record.downX, record.downY);
for (StrokeDrawEarserEntity listPathHi : record.listPathHis) {
drawPath.quadTo(listPathHi.preX, listPathHi.preY, (listPathHi.curX + listPathHi.preX) / 2, (listPathHi.curY + listPathHi.preY) / 2);
}
tempCanvas.drawPath(drawPath, paint);
Log.e(TAG, "*2* drawPath: ---->drawPath drawPath");
break;
case STROKE_TYPE_LINE:
Path linePath = new Path();
linePath.moveTo(record.downX, record.downY);
linePath.lineTo(record.upX, record.upY);
tempCanvas.drawPath(linePath, paint);
Log.e(TAG, "*2* drawPath: ---->drawPath linePath");
break;
case STROKE_TYPE_CIRCLE:
tempCanvas.drawOval(record.rect, paint);
Log.e(TAG, "*2* drawPath: ---->drawPath STROKE_TYPE_CIRCLE");
break;
case STROKE_TYPE_RECTANGLE:
tempCanvas.drawRect(record.rect, paint);
Log.e(TAG, "*2* drawPath: ---->drawPath STROKE_TYPE_RECTANGLE");
break;
case STROKE_TYPE_TEXT:
if (record.text != null) {
TextPaint tp = new TextPaint();
tp.setTextSize(record.textSize);
tp.setColor(record.color);
StaticLayout layout = new StaticLayout(record.text, tp, record.textWidth, Layout.Alignment.ALIGN_NORMAL, 1.0F, 0.0F, true);
tempCanvas.translate(record.textOffX, record.textOffY);
layout.draw(tempCanvas);
tempCanvas.translate(-record.textOffX, -record.textOffY);
}
Log.e(TAG, "*2* drawPath: ---->drawPath STROKE_TYPE_TEXT");
break;
}
}
Log.e(TAG, "*2* drawBitmap: ---->drawBitmap tempBitmap before");
canvas.drawBitmap(tempBitmap, new Rect(0, 0, tempCanvas.getWidth(), tempCanvas.getHeight()), new Rect(0, 0, canvas.getWidth(), canvas.getHeight()), null);
Log.e(TAG, "*2* drawBitmap: ---->drawBitmap tempBitmap after");
}
}
调用界面代码
public class WhiteBoardActivity extends BaseActivity {
//start 画板相关
@BindView(R.id.sketch_view)
SketchView mSketchView;
@BindView(R.id.whiteboad_root_content)
View rootView;
private List<SketchData> sketchDataList = new ArrayList<>();
private static final int SKETCHVIEW_RECORDDATA_TAG = 0x9001;
private static final int CLEAR_SKETCHVIEW_RECORDDATA_TAG = 0x9002;
private Handler sketchViewhandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
String sketchDataJSON = (String) msg.obj;
switch (msg.what){
case SKETCHVIEW_RECORDDATA_TAG:
SketchData curSketchData = GsonUtil.GsonToBean(sketchDataJSON,SketchData.class);
if (curSketchData!=null){
if (curSketchData.strokeRecordList.size() == 0 && curSketchData.strokeRedoList.size() == 0){
mSketchView.erase();
return;
}
mSketchView.setSketchData(curSketchData);
mSketchView.invalidate();
}
break;
case CLEAR_SKETCHVIEW_RECORDDATA_TAG:
Log.d(TAG,"收到关闭画板指令--->"+sketchDataJSON);
SketchData clearSketchData = GsonUtil.GsonToBean(sketchDataJSON,SketchData.class);
if (clearSketchData!=null){
mSketchView.erase();
rootView.setVisibility(View.GONE);
}
activity.finish();
break;
}
}
};
//画板相关 end
private WhiteBoardReceiver whiteBoardReceiver;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_white_board);
ButterKnife.bind(this);
//start 画板
initWhiteBoard();
//画板 end
whiteBoardReceiver = new WhiteBoardReceiver();
IntentFilter intentFilter = new IntentFilter();
//intentFilter.addAction(Constant.ACTION_CLEAR_WHITE_BOARD);//已舍弃 归入SEND_SKETCHDATA_WHITE_BOARD
intentFilter.addAction(Constant.ACTION_UNSYNCHROU);
intentFilter.addAction(Constant.ACTION_CLOSE_WHITE_BOARD);
intentFilter.addAction(Constant.SEND_SKETCHDATA_WHITE_BOARD);
registerReceiver(whiteBoardReceiver, intentFilter);
}
//画板 start
private void initWhiteBoard(){
initWhiteBoardData();
}
private void initWhiteBoardData() {
SketchData newSketchData = new SketchData();
sketchDataList.add(newSketchData);
mSketchView.setSketchData(newSketchData);
}
//画板 end
@Override
protected void onDestroy() {
unregisterReceiver(whiteBoardReceiver);
whiteBoardReceiver = null;
super.onDestroy();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
return false;
}
public class WhiteBoardReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.v("PDFMiNaReceiver", "onReceive");
if (intent != null && intent.getAction() != null && intent.getAction().equals(Constant.ACTION_UNSYNCHROU)) {
Log.e("ss", "解除同步");
activity.finish();
} else if (intent != null && intent.getAction() != null && intent.getAction().equals(Constant.ACTION_CLOSE_WHITE_BOARD)) {
Log.d(TAG,"收到关闭画板指令");
String sketchDataJSON = intent.getStringExtra("SKETCHDATA");
if (sketchDataJSON!=null){
Message msg = new Message();
msg.what = CLEAR_SKETCHVIEW_RECORDDATA_TAG;
msg.obj = sketchDataJSON;
sketchViewhandler.sendMessage(msg);
}
}else if(intent != null && intent.getAction() != null && intent.getAction().equals(Constant.SEND_SKETCHDATA_WHITE_BOARD)){
String sketchDataJSON = intent.getStringExtra("SKETCHDATA");
if (sketchDataJSON!=null){
Message msg = new Message();
msg.what = SKETCHVIEW_RECORDDATA_TAG;
msg.obj = sketchDataJSON;
sketchViewhandler.sendMessage(msg);
}
}
}
}
}