gpt4 book ai didi

android - 绘制要覆盖的 Canvas

转载 作者:行者123 更新时间:2023-11-29 01:55:52 25 4
gpt4 key购买 nike

我在 doInBackground 中的 AsyncTask 中的 Canvas 上绘制了一个。绘图代码贯穿,逐级调试检查。 onPostExecute() 方法也被调用,也进行了检查。但是,没有绘制任何东西?我可以确认绘图代码是正确的,因为之前在 Overlay 子类的 onDraw() 方法中运行了相同的逻辑。这是来源:

import java.lang.ref.WeakReference;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.os.AsyncTask;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class AsyncOverlayTask extends AsyncTask<DataVO, Void, Canvas> {

private final Paint mPathPaint;
private final Paint mPointPaint;
private Path path;
private final MapView mMapView;
private final Canvas mCanvas;

public AsyncOverlayTask(final MapView mapView, final Paint pathPaint, final Paint pointPaint) {

mMapView = new WeakReference<MapView>( mapView ).get();
mPathPaint = pathPaint;
mPointPaint = pointPaint;
mCanvas = new Canvas();
}

@Override
protected Canvas doInBackground(final DataVO... params) {
Thread.currentThread().setName( "AsyncOverlayTask" );
final Canvas canvas = new Canvas();
final Bitmap bitmap = Bitmap.createBitmap( mMapView.getWidth(), mMapView.getHeight(), Bitmap.Config.ARGB_8888 );
canvas.setBitmap( bitmap );
canvas.drawColor( Color.TRANSPARENT );
GeoPoint previousGeoPoint = null;
GeoPoint geoPoint = null;
final Path path = new Path();
Projection projection;
// at least 2 elements in aData
if ( ( params != null ) && ( params.length > 0 ) ) {
for ( final DataVO dataVo : params ) {
if ( dataVo.getLatitude().replace( ".", "" ).length() > 8 ) {
geoPoint = new GeoPoint( Integer.valueOf( dataVo.getLatitude().replace( ".", "" ).substring( 0, 8 ) ), Integer.valueOf( dataVo
.getLongitude().replace( ".", "" ).substring( 0, 7 ) ) );
} else {
geoPoint = new GeoPoint( Integer.valueOf( dataVo.getLatitude().replace( ".", "" ) ), Integer.valueOf( dataVo.getLongitude()
.replace( ".", "" ) ) );
}
// project point
projection = mMapView.getProjection();
Point projectedPoint = new Point();
projectedPoint = projection.toPixels( geoPoint, projectedPoint );
// draw point
canvas.drawCircle( projectedPoint.x, projectedPoint.y, 7, mPointPaint );
if ( previousGeoPoint != null ) {
final Point prevPoint = new Point();
projection.toPixels( previousGeoPoint, prevPoint );
// if((projectedPoint.x != prevPoint.x) &&
// (projectedPoint.y != projectedPoint.y)) {
path.moveTo( projectedPoint.x, projectedPoint.y );
path.lineTo( prevPoint.x, prevPoint.y );
canvas.drawPath( path, mPathPaint );
// }
} else {
path.moveTo( projectedPoint.x, projectedPoint.y );
}
previousGeoPoint = geoPoint;
}

}
return canvas;

}

@Override
protected void onPostExecute(final Canvas canvas) {
if ( ( mMapView != null ) && ( canvas != null ) ) {
final DataOverlay dataOverlay = new DataOverlay();
dataOverlay.draw( canvas, mMapView, false );
if ( mMapView != null ) {
mMapView.getOverlays().add( ( dataOverlay ) );
mMapView.invalidate();
}
}
}

private class DataOverlay extends Overlay {

@Override
public void draw(final Canvas canvas, final MapView mapView, final boolean shadow) {
super.draw( canvas, mapView, shadow );
}
}
}

最佳答案

您使用 Canvas 绘制的所有内容实际上都绘制到您在该 Canvas 上设置的 Bitmap 上。您使用的 Bitmap 仅在 doInBackground() 中引用,在其他任何地方都没有引用。您的叠加层需要做的是绘制位图:

@Override
protected Bitmap doInBackground(final DataVO... params) {
return bitmap;
}

@Override
protected void onPostExecute(final Canvas canvas) {
// ...
final DataOverlay dataOverlay = new DataOverlay(bitmap);
// ...
}

private class DataOverlay extends Overlay {
private final Bitmap mBitmap;

DataOverlay(Bitmap b) { mBitmap = b; }

@Override
public void draw(final Canvas canvas, final MapView mapView, final boolean shadow) {
canvas.drawBitmap(b, 0.0f, 0.0f, null);
}
}

关于android - 绘制要覆盖的 Canvas ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15204884/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com