如何在android中的图像上绘制一个圆圈

时间:2013-12-23 07:39:21

标签: java android image screen geometry

我从drawable资源加载了一个图像,然后在android设备的屏幕上显示它。我需要在图像上画一个圆圈,并在时间内更新圆圈的位置。任何人都可以帮我解决这个过程。 这是我加载图像的代码,它完美地运行:

    private void createBitMap() {

    Bitmap bitMap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);  //creates bmp
    bitMap = bitMap.copy(bitMap.getConfig(), true);     //lets bmp to be mutable
    Canvas canvas = new Canvas(bitMap);                 //draw a canvas in defined bmp

    Paint paint = new Paint();                          //define paint and paint color
    paint.setColor(Color.RED);
    paint.setStyle(Style.FILL_AND_STROKE);
    //paint.setStrokeWidth(0.5f);
    paint.setAntiAlias(true);                           //smooth edges


    ImageView imageView = (ImageView) findViewById(R.id.imageView);
    imageView.setImageBitmap(bitMap);
    imageView.setImageResource(R.drawable.map);
    //canvas.drawCircle(50, 50, 3, paint);
}

2 个答案:

答案 0 :(得分:1)

setImageBitmap()之后设置图像资源在这种情况下永远不会绘制位图,如果你想在imageview中同时加载地图和圆形而不是改变你的代码以完全满足你的要求:

  private void createBitMap() {

    Bitmap bitMap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);  //creates bmp
    bitMap = bitMap.copy(bitMap.getConfig(), true);     //lets bmp to be mutable
    Canvas canvas = new Canvas(bitMap);                 //draw a canvas in defined bmp

    Paint paint = new Paint();                          //define paint and paint color
    paint.setColor(Color.RED);
    paint.setStyle(Style.FILL_AND_STROKE);
    //paint.setStrokeWidth(0.5f);
    paint.setAntiAlias(true);                           //smooth edges


    ImageView imageView = (ImageView) findViewById(R.id.imageView);
    imageView.setImageBitmap(bitMap);
    //changed set image resource to set image background resource
    imViewAndroid.setBackgroundResource(R.drawable.map);
    canvas.drawCircle(50, 50, 3, paint);
    //invalidate to update bitmap in imageview
    imageView.invalidate();

}

现在将绘制圆圈。 干杯, 哈马德

答案 1 :(得分:0)

试试这个:

private void createBitMap() {

    Bitmap bitMap = BitmapFactory.decodeResource(null, R.drawable.map);  //creates bmp
    bitMap = bitMap.copy(bitMap.getConfig(), true);     //lets bmp to be mutable
    Canvas canvas = new Canvas(bitMap);                 //draw a canvas in defined bmp

    Paint paint = new Paint();                          //define paint and paint color
    paint.setColor(Color.RED);
    paint.setStyle(Style.FILL_AND_STROKE);
    //paint.setStrokeWidth(0.5f);
    paint.setAntiAlias(true);                           //smooth edges

    canvas.drawCircle(50, 50, 3, paint);

    ImageView imageView = (ImageView) findViewById(R.id.imageView);
    imageView.setImageBitmap(bitMap);
}