Android: Draw an image in the center of another image

I have one image image 1 , and one from the server that image 2 . I try to make the second, only in the center of the first. as a result, I want a single image, as in fig. image

+4
source share
2 answers

This should do what you are looking for:

BackgroundBitmap will be your image1 and bitmapToDrawInTheCenter will be your 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); } 
+13
source

Provided: draw text / image on another image in Android

Drawing images on top of each other is pretty easy with Canvas. The canvas mainly serves as a drawing board for drawing text / images. You just need to build a canvas with the first image, and then draw a second image in the center, as shown below

 /* 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); 
0
source

Source: https://habr.com/ru/post/1433123/


All Articles