如何在ImageViewZoom中使用的位图上绘制线条?

时间:2015-03-03 11:12:04

标签: android bitmap imageview

我最近开始使用ImageViewZoom (https://github.com/sephiroth74/ImageViewZoom) 而我将要做的是不时在此视图中使用的位图上绘制一些线条。

我尝试按以下方式执行此操作,但结果是视图无法再缩放。

protected void onDraw(Canvas canvas) 
{   
    // TODO Auto-generated method stub
    super.onDraw(canvas);

    Canvas bmp_canvas = new Canvas(bmp);//bmp is the original bitmap

    Paint paint = new Paint();

    //Draw map
    paint. setColor(Color.BLUE);
    paint. setStrokeWidth(10);
    int i;
    for(i=0; i<toDraw.size();i++)
    {
        Segment now = toDraw.get(i); //toDraw is a List and stores the lines

        PointType tmp_start = now.s;
        PointType tmp_end = now.e;

        bmp_canvas.drawLine((float)tmp_start.x, (float)tmp_start.y, 
                (float)tmp_end.x, (float)tmp_end.y, paint);
    }

    Matrix matrix = getImageViewMatrix();
    setImageBitmap(bmp, matrix, ZOOM_INVALID, ZOOM_INVALID);        
    return;
}

那么正确的方法是什么?非常感谢你!

1 个答案:

答案 0 :(得分:0)

好吧,我自己解决了!

我是按照以下方式做到的:

public void drawMap(Bitmap bmp) //a new function outside of onDraw()
{
    Bitmap now_bmp = Bitmap.createBitmap(bmp);
    Canvas canvas = new Canvas(now_bmp);
    Paint paint = new Paint();

    //Draw map
    paint. setColor(Color.BLUE);
    paint. setStrokeWidth(10);
    int i;
    for(i=0; i<toDraw.size();i++)
    {
        Segment now = toDraw.get(i);

        PointType tmp_start = now.s;
        PointType tmp_end = now.e;

        canvas.drawLine((float)tmp_start.x, (float)tmp_start.y, 
                (float)tmp_end.x, (float)tmp_end.y, paint);
    }

    Matrix matrix = getDisplayMatrix();
    setImageBitmap(now_bmp, matrix, ZOOM_INVALID, ZOOM_INVALID);     
}

简而言之,只需使用原点Bitmap创建一个Canvas,然后在其上绘制一些内容,结果将存储在位图中,并获取当前矩阵,并将新位图设置为ImageViewZoom,这就是全部。