我能够将字符串转换为具有多行的位图。但是如何使位图高度相对于字符串长度意味着现在我必须传递位图的长度。
因此,问题是如果字符串长度大于高度位图高度,则会裁剪一些字符。
我的代码如下: -
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@android:color/black"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/btnMerge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClickMerge"
android:text="Merge" />
<ImageView
android:id="@+id/imgView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:layout_gravity="center"/>
</LinearLayout>
java代码: -
public void onClickMerge(View v) {
String pathName=Environment.getExternalStorageDirectory()+"/demoApp";
File f = new File(pathName,"CameraImg_4.jpg");
if(f.exists()){
Log.e("File ", "file exist");
}
else{
Log.e("File ", "file not exist");
}
Bitmap bitmap=BitmapFactory.decodeFile(f.getAbsolutePath());
Bitmap bitmap2=textAsBitmapMultiline("Hello! How are you? What is going on? ", 80, Color.WHITE,bitmap.getWidth(),50);
Bitmap bitmap3=combineImages(bitmap, bitmap2);
imgView.setImageBitmap(bitmap3);
saveBitmapToFile(pathName+"/merged.jpg", bitmap3);
}
public Bitmap textAsBitmapMultiline(String mText, float textSize, int textColor,int width,int height){
Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(image);
TextPaint mTextPaint=new TextPaint();
mTextPaint.setTextSize(textSize);
mTextPaint.setColor(textColor);
StaticLayout mTextLayout = new StaticLayout(mText, mTextPaint, canvas.getWidth(), Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
canvas.save();
mTextLayout.draw(canvas);
canvas.restore();
return image;
}
public static Bitmap combineImages(Bitmap c, Bitmap s) {
Bitmap cs = null;
int width, height = 0;
int sWidth, sHeight = 0;
sHeight = s.getHeight();
if(c.getWidth() > s.getWidth()) {
width = c.getWidth();
height = c.getHeight() + s.getHeight();
} else {
width = s.getWidth();
height = c.getHeight() + s.getHeight();
}
cs = Bitmap.createBitmap(width, height+30, Bitmap.Config.ARGB_8888);
Canvas comboImage = new Canvas(cs);
comboImage.drawBitmap(c, 0f, 0f, null);
comboImage.drawBitmap(s,2f , c.getHeight() , null);
return cs;
}
附加屏幕截图: -
如果有人有想法。请帮助我。谢谢。
答案 0 :(得分:0)
您可以使用
缩放位图bitmap = Bitmap.createScaledBitmap(bitmap,width,height,true);