Android:在另一个图像的中心绘制图像

时间:2012-09-08 11:14:19

标签: android image

我有一张图片image 1,其中一张来自服务器image 2我正试图在第一张图片的中心绘制第二张图片。结果我想要像pic中的单个图像。 image

2 个答案:

答案 0 :(得分:11)

这应该是你要找的东西:

backgroundBitmap将是您的image1,而bitmapToDrawInTheCenter将是您的image2

public void createImageInImageCenter()
{
    Bitmap backgroundBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
    Bitmap bitmapToDrawInTheCenter = BitmapFactory.decodeResource(getResources(), R.drawable.ic_action_search);

    Bitmap resultBitmap = Bitmap.createBitmap(backgroundBitmap.getWidth(),backgroundBitmap.getHeight(), backgroundBitmap.getConfig());
    Canvas canvas = new Canvas(resultBitmap);
    canvas.drawBitmap(backgroundBitmap, new Matrix(), null);
    canvas.drawBitmap(bitmapToDrawInTheCenter, (backgroundBitmap.getWidth() - bitmapToDrawInTheCenter.getWidth()) / 2, (backgroundBitmap.getHeight() - bitmapToDrawInTheCenter.getHeight()) / 2, new Paint());

    ImageView image = (ImageView)findViewById(R.id.myImage);
    image.setImageBitmap(resultBitmap);
}

答案 1 :(得分:0)

礼貌:Draw text/Image on another Image in Android

使用Canvas相互绘制图像相当简单。 Canvas基本上充当绘制文本/图像的绘图板。您只需要使用第一个Image构建一个画布,然后在中心绘制第二个Image,如下所示

/* This ImageOne will be used as the canvas to draw an another image over it. Hence we make it mutable using the copy API
as shown below
*/
      Bitmap imageOne = BitmapFactory.decodeResource(getResources(), R.drawable.imageOne).copy(Bitmap.Config.ARGB_8888,true);
      // Decoding the image two resource into a Bitmap
      Bitmap imageTwo= BitmapFactory.decodeResource(getResources(), R.drawable.imageTwo);
      // Here we construct the canvas with the specified bitmap to draw onto
      Canvas canvas=new Canvas(imageOne);

/*Here we draw the image two on the canvas using the drawBitmap API.
drawBitmap takes in four parameters
1 . The Bitmap to draw
2.  X co-ordinate to draw from
3.  Y co ordinate to draw from
4.  Paint object to define style
*/
      canvas.drawBitmap(imageTwo,(imageOne.getWidth())/2,(imageOne.getHeight())/2,new Paint());

      imageView.setImageBitmap(imageOne);