我正在尝试在画布上绘制位图图像。但是在这样做的时候,我得到了以下指定的异常。
10-10 11:29:18.592: A/libc(20706): Fatal signal 11 (SIGSEGV), code 1, fault addr 0xc0 in tid 20706 (apcanvasexample)
以下是我的代码。
public class MapView extends View implements IAsyncResponse {
private Path path;
private Paint paint;
private URL url;
private Bitmap bmp;
private Canvas canvas;
public MapView(Context context, AttributeSet attrs) {
super(context, attrs);
path = new Path();
paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.RED);
paint.setStrokeWidth(10);
path.moveTo(0, 0);
}
@SuppressLint("DrawAllocation")
@Override
public void onDraw(Canvas canvas) {
this.canvas = canvas;
path.lineTo(currentX, currentY);
canvas.drawPath(path, paint);
new DownloadImageTask(canvas, this)
.execute("http://<ip>:<port>/map/download/image?imageType=jpg&mapId=1");
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
IAsyncResponse delegate;
Canvas canvas;
public DownloadImageTask(Canvas canvas, IAsyncResponse delegate) {
this.canvas = canvas;
this.delegate = delegate;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
delegate.processFinished(result);
}
}
@Override
public void processFinished(Bitmap bmp) {
canvas.drawBitmap(bmp, new Rect(100,100,150,150), new Rect(0,0,50,50) , null);
}
}
答案 0 :(得分:1)
public class MapView
extends View
implements IAsyncResponse {
public MapView(Context context) {
super(context);
init();
}
public MapView(
Context context,
AttributeSet attrs) {
super(context, attrs);
init();
}
public MapView(
Context context,
AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private Path path;
private Paint paint;
private URL url;
private Bitmap bmp;
// use common init method for initialization, which is called from all the constructors
void init() {
path = new Path();
paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.RED);
paint.setStrokeWidth(10);
path.moveTo(0, 0);
// start the downloader task only once and not within onDraw callback
// please note that, reference to canvas is not hold
new DownloadImageTask(this).execute("my/image/url");
}
@Override
public void onDraw(Canvas canvas) {
// all your other drawing logic goes here
if (null != bmp) {
canvas.drawBitmap(bmp, new Rect(100, 100, 150, 150), new Rect(0, 0, 50, 50), null);
}
}
private class DownloadImageTask
extends AsyncTask<String, Void, Bitmap> {
IAsyncResponse delegate;
public DownloadImageTask(
IAsyncResponse delegate) {
this.delegate = delegate;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
// you probably don't even need delegate. But I have kept it for consistency.
// alternatively you can remove this interface and do -
// MapView.this.bmp = bmp;
// invalidate();
delegate.processFinished(result);
}
}
@Override
public void processFinished(Bitmap bmp) {
this.bmp = bmp;
invalidate();
}
}
答案 1 :(得分:0)
您需要禁用此视图的硬件加速。
yourview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
https://developer.android.com/guide/topics/graphics/hardware-accel.html#controlling