Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android:Draw image in the center of another image

Tags:

android

image

I have one image image 1 and one is coming from server that is image 2 i am trying to draw second one just at the center of the first. as result i want single image like in pic . image

like image 471
Yasir Khan Avatar asked Sep 08 '12 11:09

Yasir Khan


1 Answers

This should do what you're looking for:

The backgroundBitmap variable would be your image1 and the bitmapToDrawInTheCenter would be your image2.

public void centerImageInOtherImage()
{
    Bitmap backgroundBitmap        = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
    Bitmap bitmapToDrawInTheCenter = BitmapFactory.decodeResource(getResources(), R.drawable.ic_action_search);
    
    Bitmap resultingBitmap = Bitmap.createBitmap(backgroundBitmap.getWidth(), backgroundBitmap.getHeight(), backgroundBitmap.getConfig());

    Canvas canvas = new Canvas(resultingBitmap);
    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(resultingBitmap);
}
like image 60
Bruno Bieri Avatar answered Sep 28 '22 15:09

Bruno Bieri