I have textView (mTextOnImage
) and imageView (mImageView
) .
I combine them by using the function combineImages
, but when I combine , the text size is changed .
//generate bitmap of textView by using getDrawingCache()
Bitmap bmp = Bitmap.createBitmap(mTextOnImage.getDrawingCache());
//getting image as bitmap from image view ( to use as background to combine )
BitmapDrawable drawable = (BitmapDrawable) mImageView.getDrawable();
Bitmap bitmapBackground = drawable.getBitmap();
//combining two bitmaps
Bitmap combined = combineImages(bitmapBackground, bmp);
This is combineImages
function
public Bitmap combineImages(Bitmap background, Bitmap foreground) {
Bitmap cs;
cs = Bitmap.createBitmap(background.getWidth(), background.getHeight(), Bitmap.Config.ARGB_8888);
//creating canvas by background image's width and height
Canvas comboImage = new Canvas(cs);
background = Bitmap.createScaledBitmap(background, background.getWidth(), background.getHeight(), true);
//Drawing background to canvas
comboImage.drawBitmap(background, 0, 0, null);
//Drawing foreground (text) to canvas
comboImage.drawBitmap(foreground, mTextOnImage.getLeft(),mTextOnImage.getTop(), null);
return cs;
}
Bitmap combined successfully but the text size is changed .
This is how I set text size
mTextOnImage.setTextSize(getResources().getDimensionPixelSize(R.dimen.myFontSize));
In string resource ,
<resources>
<dimen name="myFontSize">40sp</dimen>
</resources>
I get the background image from device gallery , so the resolution (image dimension) may be different.
Is there any calculation I missed ?
Additionally , textView (mTextOnImage
) is draggable, so I also want to set the position correctly on combining those two.
It would be helpful to see your layout XML and a couple of images. Lacking those, I suggest that you check to make sure that your images are not somehow being resized upon display.
Update: Before looking at the longer solution, try changing how you are setting the text size. The default is "sp" and you are using "px".
If that doesn't work, try the following:
I took your code and made a few changes to try to reproduce the problem. In the layout, I display a text view (height and width =
wrap_content
) and a non-resized image. Below these two views I display the combined view. I have positioned the text of the combined view at the top with a white background so I could make a quick comparison. Here is the result:The two "Hello World!"s look the same to me. This leads me to believe that your combined image view is being stretched or shrunk and, in the process, your text is changing size since it is just part of the image.
Here is my code that produces the above image. The image is just a "png" graphic.
MainActivity.java
activity_main.xml