在图像视图上画一个圆(点)

Draw a circle(dot) on an imageview

我正在尝试编写一个简单的脚本,当有人触摸美国地图时,它会在屏幕上的 (x,y) 坐标处放置一个点。

代码如下。

public class map extends AppCompatActivity {

    ImageView ivMap_ActivityMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);

        ivMap_ActivityMap = (ImageView)findViewById(R.id.ivMap_ActivityMap);
        ivMap_ActivityMap.setImageResource(R.drawable.usa);
        ivMap_ActivityMap.setOnTouchListener(handleTouch);

    }

    private final View.OnTouchListener handleTouch = new View.OnTouchListener() {

        @SuppressLint("ResourceAsColor")

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            int x = (int) event.getX();
            int y = (int) event.getY();
            Toast.makeText(map.this, "x = " + x + ", y = " + y, Toast.LENGTH_LONG).show();

            Paint paint = new Paint();
            int radius;
            radius = 100;
            paint.setStyle(Paint.Style.FILL);
            paint.setColor(R.color.colorPrimaryDark);
            Canvas canvas = new Canvas();
            canvas.drawCircle(x, y, radius, paint);

            return true;
            }
        };
    }

试试这个。希望对您有所帮助

class DrawingView extends SurfaceView {

        private final SurfaceHolder surfaceHolder;
        private final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

        public DrawingView(Context context) {
            super(context);
            surfaceHolder = getHolder();
            paint.setColor(Color.RED);
            paint.setStyle(Style.FILL);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_DOWN) {
                if (surfaceHolder.getSurface().isValid()) {
                    Canvas canvas = surfaceHolder.lockCanvas();
                    canvas.drawColor(Color.BLACK);
                    canvas.drawCircle(event.getX(), event.getY(), 5, paint);
                    surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }
            return false;
        }
    }

您的代码无法正常工作,因为您正在绘制一个孤儿 Canvas。

您可以通过使用 ImageView 大小的空位图制作 canvas 并绘制圆圈,最后将 drawable 设置为以下内容的 ImageView 来实现您的目标方式,

public class map extends AppCompatActivity {

    ImageView ivMap_ActivityMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);

        ivMap_ActivityMap = (ImageView)findViewById(R.id.ivMap_ActivityMap);
        // Set the map as background  
        ivMap_ActivityMap.setBackgroundResource(R.drawable.usa);
        ivMap_ActivityMap.setOnTouchListener(handleTouch);
}

private final View.OnTouchListener handleTouch = new View.OnTouchListener() {
    // Stored to draw multiple circle. 
    // If you want to draw only one circle then you can make it a local variable
    private Bitmap bmp;

    @SuppressLint("ResourceAsColor")

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        int x = (int) event.getX();
        int y = (int) event.getY();
        Toast.makeText(map.this, "x = " + x + ", y = " + y, Toast.LENGTH_LONG).show();

        Paint paint = new Paint();
        int radius;
        radius = 100;
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(R.color.colorPrimaryDark);
        // Create a bitmap object of your view size
        if(bmp==null) bmp = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
               // Create the canvas with the bitmap
        Canvas canvas = new Canvas(bmp);
        canvas.drawCircle(x, y, radius, paint);
        // Set the bitmap to the imageView
        ImageView iv = (ImageView) v;
               iv.setImageBitmap(bmp);
        return true;
        }
    };
}