如何在图像顶部绘制圆形
问题描述:
我的应用程序从相机拍摄图像并保存,然后将其显示在ImageView
上,但下一步是在显示的图像顶部放置一个圆形,以便用户触摸屏幕,然后保存“修改的图像”。如何在图像顶部绘制圆形
有点像图像编辑器,如果你愿意,问题是我不知道从哪里开始图像编辑。我试过这个
@Override
public boolean onTouch(View v, MotionEvent event) {
circleView.setVisibility(View.VISIBLE);
circleView.setX(event.getX()-125);
circleView.setY(event.getY()-125);
try{
Bitmap bitmap = Bitmap.createBitmap(relativeLayout.getWidth(),relativeLayout.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
v.draw(canvas);
mImageView.setImageBitmap(bitmap);
FileOutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory());
bitmap.compress(Bitmap.CompressFormat.PNG,100,output);
output.close();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
return true;
}//ENDOF onTouch
我该怎么做才能保存图像?
答
如果您包含更多关于您正在使用的库和语言的信息,这将有所帮助。从@override我会认为这是Android上的java?
至于如何创建一个圆 - 有很多技术可以使用,可能有多个库可以用来做到这一点。但是,通过使用Bitmap对象的接口上的函数,即getPixels和setPixels,我们可以保持它非常简单。
你需要做的是抓取一个像素矩形到预先分配的缓冲区(使用getPixels),然后画出你的圆到这个缓冲区,然后用'setPixels'写回缓冲区。 这里有一个简单的在你从javaish伪“的getPixels”获得缓冲器绘制一个圆(虽然不完全有效)的方法(未经测试):
//Return the distance between the point 'x1, y1' and 'x2, y2'
float distance(float x1, float y1, float x2, float y2)
{
float dx = x2 - x1;
float dy = y2 - y1;
return Math.sqrt(dx * dx + dy * dy);
}
//draw a circle in the buffer of pixels contained in 'int [] pixels'
//at position 'cx, cy' with the given radius and colour.
void drawCircle(int [] pixels, int stride, int height, float cx, float cy, float radius, int colour)
{
for (int y = 0; y < height; ++y)
for (int x = 0; x < stride; ++x)
{
if (distance((float)x, (float)y, cx, cy) < radius)
pixels[x + y * stride] = colour;
}
}
这只是问一个问题,对于每个像素, '是由'cx,cy,radius'给出的圆圈内的点'x,y'?'如果是,则绘制一个像素。 更高效的方法可能包括一个扫描线光栅器,它可以通过圆的左侧和右侧,不需要为每个像素进行昂贵的“距离”计算。
但是,这种“隐式曲面”方法非常灵活,您可以通过它获得很多效果。其他选项可能是复制预先制作的圆形位图,而不是实时创建自己的位图。
您还可以基于'距离 - 半径'的分数值混合'颜色'以实现抗锯齿。