所以,我有一个应用程序,在主要活动(主屏幕)上有一张地图(一个jpg文件)。
现在我想在这张地图上画一个圆圈,因为用户会搜索一个位置,然后,我想用圆圈标记这个位置,我该如何实现呢?
我试过这个:
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
paint.setStyle(Paint.Style.STROKE);
canvas.drawCircle(50, 50, 30, paint);
}
在我的DrawDis课程中,在我的主要活动课程中,我得到了这个:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
//RelativeLayout relativeLayout = new RelativeLayout(this);
//relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(
//ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
//setContentView(relativeLayout);
//relativeLayout.addView(new DrawDis(this));
}
我知道创建一个新的布局会改变整个屏幕,但我怎么能以某种方式只是绘制到屏幕上而不改变活动?
答案 0 :(得分:0)
以下代码段会在用户触摸图片的任何地方绘制一个圆圈。
另见link
image.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View arg0, MotionEvent arg1) {
System.out.println("Touch recieved at "+arg1.getX() + " " + arg1.getY());
touchX = (arg1.getX());
touchY = (arg1.getY());
System.out.println("Touch recieved at "+touchX + " " + touchY);
image.setImageBitmap(createImage());
return true;
}
});
public Bitmap createImage(){
Bitmap image = bmp.copy(Bitmap.Config.RGB_565, true);
Canvas canvas = new Canvas(image);
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.GREEN);
touchX=touchX- image.getWidth() / 2;
touchY=touchY- image.getHeight() / 2;
canvas.drawCircle(touchX, touchY, radius, paint);
System.out.println("Drew a circle at "+touchX+" " + touchY+" with a radius of "+radius+".");
return image;
}