【发布时间】:2018-05-19 21:45:02
【问题描述】:
我有 textView (mTextOnImage) 和 imageView (mImageView)。
我使用 combineImages 函数将它们组合在一起,但是当我组合时,文本大小会发生变化。
//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);
这是combineImages函数
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;
}
位图合并成功,但文字大小改变了。
这就是我设置文本大小的方式
mTextOnImage.setTextSize(getResources().getDimensionPixelSize(R.dimen.myFontSize));
在字符串资源中,
<resources>
<dimen name="myFontSize">40sp</dimen>
</resources>
我从设备库中获取背景图片,因此分辨率(图片尺寸)可能不同。
有没有我错过的计算?
另外,textView (mTextOnImage) 是可拖动的,所以我还想在结合这两者时正确设置位置。
【问题讨论】:
-
@Nilu ,可能这个例子是将图像添加到
text String。但我需要的是像 overlay 这样结合文本和图像。像这样image -
你试过
comboImage.drawBitmap(background, new Matrix(), null)吗? -
@azizbekian,是的,我有,同样的结果..
标签: android android-canvas font-size android-bitmap image-resolution