我有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
)是可拖动的,所以我也想在合并这两个位置时正确设置位置。
答案 0 :(得分:2)
查看布局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>