如何在画布圆圈(蓝色圆圈)的中心放置位图图像(图钉)?

时间:2018-12-30 08:49:27

标签: android canvas android-canvas android-bitmap

   int x = 1460;
   int y = 800;

   BitmapFactory.Options myOptions = new BitmapFactory.Options();
   myOptions.inDither = true;
   myOptions.inScaled = false;
   myOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// important
   myOptions.inPurgeable = true;

   Bitmap bitmap = BitmapFactory.decodeResource(getResources(), 
   R.mipmap.map, myOptions);
   Paint paint = new Paint();
   paint.setAntiAlias(true);
   paint.setColor(Color.BLUE);
   Bitmap workingBitmap = Bitmap.createBitmap(bitmap);
   Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
   Canvas canvas = new Canvas(mutableBitmap);
   canvas.drawCircle(x, y, 25, paint);
   Bitmap currentPin = BitmapFactory.decodeResource(getResources(), 
   R.mipmap.pushpin_blue);
   canvas.drawBitmap(currentPin, x, y, null);

我将大头针和圆形都放置在相同的xy坐标中,但如下图所示

I need to place the image on center of the bluie circle

1 个答案:

答案 0 :(得分:0)

要解释该问题,请考虑您具有:
1:128 * 128像素圆圈
2:128 * 128像素的mipmap
然后,如果都位于x=0y=0中,则输出将为

enter image description here

为了将mipmap的底部放置在蓝色圆圈的中心,您应该将x=0; y=0;中的mipmap放置在x=0; y=64;中的圆圈中,输出将是:

enter image description here

未将mipmap放置在圆上的原因是mipmap的尺寸大于圆。所以如果:
1:圆圈为32 * 32像素
2:mipmap为128 * 128像素
然后,如果都位于x=0y=0中,则输出将是:

enter image description here