查看您的布局 XML 和几张图像会很有帮助。缺少这些,我建议您检查以确保您的图像在显示时不会以某种方式调整大小。
更新:在查看更长的解决方案之前,请尝试更改设置文本大小的方式。默认值为“sp”,您使用的是“px”。
mTextOnImage.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.myFontSize))
如果这不起作用,请尝试以下操作:
我采用了您的代码并进行了一些更改以尝试重现该问题。在布局中,我显示了一个文本视图(高度和宽度 = wrap_content
)和一个未调整大小的图像。在这两个视图下方,我显示了组合视图。我已将组合视图的文本放置在顶部并带有白色背景,以便进行快速比较。结果如下:
两个“Hello World!”在我看来是一样的。这使我相信您的组合图像视图正在被拉伸或缩小,并且在此过程中,您的文本正在改变大小,因为它只是图像的一部分。
这是我生成上述图像的代码。该图像只是一个“png”图形。
MainActivity.java
public class MainActivity extends AppCompatActivity {
private TextView mTextOnImage;
private ImageView mImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextOnImage = findViewById(R.id.textOnImage);
mImageView = findViewById(R.id.imageView);
mTextOnImage.post(new Runnable() {
@Override
public void run() {
//generate bitmap of textView by using getDrawingCache()
mTextOnImage.buildDrawingCache();
Bitmap bmp = Bitmap.createBitmap(mTextOnImage.getDrawingCache());
//getting image as bitmap from image view ( to use as background to combine )
mImageView.buildDrawingCache();
BitmapDrawable drawable = (BitmapDrawable) mImageView.getDrawable();
Bitmap bitmapBackground = drawable.getBitmap();
// Bitmap bitmapBackground = mImageView.getDrawingCache();
//combining two bitmaps
Bitmap combined = combineImages(bitmapBackground, bmp);
((ImageView) findViewById(R.id.imageCombined)).setImageBitmap(combined);
}
});
}
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);
comboImage.drawBitmap(foreground, (mImageView.getWidth() - foreground.getWidth()) / 2,
0, null);
return cs;
}
}
activity_main.xml
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
app:layout_constraintBottom_toTopOf="@+id/textOnImage"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed"
app:srcCompat="@drawable/sky" />
<TextView
android:id="@+id/textOnImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:text="Hello World!"
android:textColor="@android:color/holo_red_dark"
android:textSize="40sp"
app:layout_constraintBottom_toTopOf="@+id/imageCombined"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
<ImageView
android:id="@+id/imageCombined"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textOnImage" />
</android.support.constraint.ConstraintLayout>