如何在 ImageViewZoom 中使用的位图上画线?
How to draw lines on the bitmap used in ImageViewZoom?
我最近开始使用 ImageViewZoom
(https://github.com/sephiroth74/ImageViewZoom)
而我要做的就是时不时地在这个视图中使用的位图上画一些线。
我试过按下面的方式进行,结果是View不能再缩放了
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;
}
那么正确的做法是什么呢?非常感谢!
嗯,我自己解决了!
我是按照以下方式做的:
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);
}
简单来说就是创建一个Canvas以原点为Bitmap,然后在上面画点东西,结果会存入位图中,并得到当前的矩阵,将新的位图设置到ImageViewZoom ,仅此而已。
我最近开始使用 ImageViewZoom (https://github.com/sephiroth74/ImageViewZoom) 而我要做的就是时不时地在这个视图中使用的位图上画一些线。
我试过按下面的方式进行,结果是View不能再缩放了
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;
}
那么正确的做法是什么呢?非常感谢!
嗯,我自己解决了!
我是按照以下方式做的:
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);
}
简单来说就是创建一个Canvas以原点为Bitmap,然后在上面画点东西,结果会存入位图中,并得到当前的矩阵,将新的位图设置到ImageViewZoom ,仅此而已。