我是新的android开发人员,我试图将文本视图转换或截图成图像,最近我发现了这个问题的代码片段代码,它可以正常工作当文本非常小时,就像一行一行,文本大小一样的文本,但如果文本大小超过一行或文本大小较大,则文本会以一行压缩文本,文本的大小变得非常小。 这里是java代码。如何将textview转换为精确大小的图像,如屏幕截图
public class MainActivity extends AppCompatActivity {
ImageView bmImage;
TextView view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = (TextView) findViewById(R.id.screen);
bmImage = (ImageView)findViewById(R.id.image);
view.setDrawingCacheEnabled(true);
view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
bmImage.setImageBitmap(b);
bmImage.setBackgroundColor(Color.GRAY);
}}
这里是xml代码。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.jarad.c_to_i.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/screen"
android:textSize="20dp"
android:text="My name is Mo7mdech I am 30 years old, please help my in this problemَ"
/>
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
请帮忙,谢谢。
保存屏幕比作物借你的TextView的坐标的形象?我知道它不是答案,但我相信这会起作用 –
我想过它,但我想通过我的代码来解决它,因为我确信我错过了某些东西。 – Mo7mdech