优化视图的ondraw方法

时间:2014-11-22 18:10:33

标签: android view ondraw

我想在地图上绘制地点的图钉。由于我正在使用的库中的这个类,地图是可缩放的和可空的:

https://github.com/davemorrissey/subsampling-scale-image-view

我扩展了subsamplingimageview类来绘制图像。地图的分辨率 是2494乘2048像素,每个像素是一个坐标。当活动可见时,只有一部分 整个地图都可见。

问题: 我需要在地图上绘制250个引脚,我试过这样,但缩放和平移地图的表现不佳。

如何在不失性能的情况下在地图上绘制图钉? 我尝试在图像位图上绘制引脚,但是我不能在视图中放置带有引脚图像的地图,除非它在资源文件夹中,但是这个文件夹是只读的......

protected override void OnDraw(Canvas canvas) 
{
    base.OnDraw(canvas);

    // Don't draw pin before image is ready so it doesn't move around during setup.
    if (!IsImageReady)
    {
        return;
    }

    Paint paint = new Paint();
    paint.AntiAlias = true;

    foreach (Location location in locations)
    {

    if (location != null && pin != null) 
    {
        PointF vPin = SourceToViewCoord(new PointF( (float)location.X    , (float) location.Y    ));
        float vX = vPin.X - (pin.Width/2);
        float vY = vPin.Y - pin.Height;
        canvas.DrawBitmap(pin, vX, vY, paint);
        if (pinPushed) 
        {
            if (location != null && info != null) 
            {
                PointF vInfo = SourceToViewCoord (new PointF( (float)location.X    , (float) location.Y    ));
                float vInfoX = vInfo.X - (info.Width / 2) + (int)ConvertDpToPix(8);//8 pixels cause image is not in the middle
                float vInfoY = vInfo.Y - info.Height;
                //canvas.DrawBitmap (info, vX, vY, paint);
                //float w2 =  info.Width;
                //float h2 =  info.Height;
                canvas.DrawBitmap (textBalloonBitmap, vInfoX, vInfoY, paint);
            }
        }
        }
    }

1 个答案:

答案 0 :(得分:0)

我不知道在你的情况下调用了多少onDraw但通常超过1.因此最好不要使用" new"在onDraw里面。但这只是一点点优化。也许您可以检查引脚在当前帧中是否可见(也许并非所有250引脚都在同一时间可见)。

仅举例来说,我不尝试。

private Paint paint = new Paint();
private PointF vPin;
private PointF vPinSTVC = PointF();
private PointF vInfo;
private PointF vInfoSTVC = PointF();

protected void OnDraw(Canvas canvas) 
    {
        base.OnDraw(canvas);

        // Don't draw pin before image is ready so it doesn't move around during setup.
        if (!IsImageReady)
        {
            return;
        }

        paint.reset();
        paint.AntiAlias = true;

        foreach (Location location in locations)
        {

        if (location != null && pin != null) 
        {
            vPin = SourceToViewCoord(new PointF( vPinSTVC.set((float)location.X, (float) location.Y));
            float vX = vPin.X - (pin.Width/2);
            float vY = vPin.Y - pin.Height;
            canvas.DrawBitmap(pin, vX, vY, paint);
            if (pinPushed) 
            {
                if (location != null && info != null) 
                {
                    vInfo = SourceToViewCoord (vInfoSTVC.set( (float)location.X    , (float) location.Y));
                    float vInfoX = vInfo.X - (info.Width / 2) + (int)ConvertDpToPix(8);//8 pixels cause image is not in the middle
                    float vInfoY = vInfo.Y - info.Height;
                    //canvas.DrawBitmap (info, vX, vY, paint);
                    //float w2 =  info.Width;
                    //float h2 =  info.Height;
                    canvas.DrawBitmap (textBalloonBitmap, vInfoX, vInfoY, paint);

                }
            }
            }
        }

文档(您的库)中的下一个选项可以使用imageView.setImageUri(" ..");也许你可以启动应用程序绘制位图并将其保存在SD卡上并在setImmageUri中使用它。