我想将textView转换为Bitmap但我得到的是黑屏而不是textview中的数据。
屏幕我要转换
绘制缓存后我得到的屏幕
我不知道自己哪里出错了。
以下是我的代码
abc.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"
android:weightSum="3"
android:paddingBottom="5dp"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.bgc.activity.AlterQuotes"
tools:showIn="@layout/activity_alter_quotes">
<RelativeLayout
android:id="@+id/AlterQuotesrl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.18">
<TextView
android:id="@+id/AlterQuotestvTestText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="15sp"
android:textColor="#000000"
android:background="@drawable/frame"
android:layout_gravity="center"
android:gravity="center"
android:text="@string/sample_name"/>
</RelativeLayout>
</LinearLayout>
abc.java
RelativeLayout z = (RelativeLayout) findViewById(R.id.AlterQuotesrl);
z.setDrawingCacheEnabled(true);
z.buildDrawingCache();
Bitmap bitmapSetDrawingChache = Bitmap.createBitmap(z.getDrawingCache());
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmapSetDrawingChache.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
// saving in sdcard
File file = new File(Environment.getExternalStorageDirectory().toString() + File.separator + "Birthday Card" + File.separator);
if(!file.exists())
file.mkdirs();
String imageName = "Image-" + DateFormat.getDateTimeInstance().format(new Date()) + ".jpg";
fileImagePath = new File(file,imageName);
try {
FileOutputStream fileOutputStream = new FileOutputStream(fileImagePath);
fileOutputStream.write(byteArrayOutputStream.toByteArray());
fileOutputStream.close();
AlertDialog.Builder adb = new AlertDialog.Builder(AlterQuotes.this);
adb.setTitle("Alert");
adb.setMessage("You will not be able to change, are you sure you want to continue?");
adb.setIcon(this.getResources().getDrawable(R.drawable.ic_black_alert_two));
adb.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(AlterQuotes.this, CardMaker.class);
intent.putExtra("bitmapPath",fileImagePath.toString());
startActivity(intent);
}
});
adb.show();
} catch (IOException e) {
e.printStackTrace();
}
提前致谢..
答案 0 :(得分:2)
透明背景通常会导致背景在转换为位图时变黑,尝试将背景框架TextView
的背景颜色设置为有点像白色,然后尝试将其转换为位图。
答案 1 :(得分:0)
尝试从TextView创建位图,目前您正在将RelativeLayout转换为位图...