解决黑屏,同时将textview转换为位图 [英] Solve black screen while converting textview into bitmap

查看:69
本文介绍了解决黑屏,同时将textview转换为位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将textView转换为Bitmap,但是却出现黑屏,而不是textview中的数据.

I want to convert textView into Bitmap but I am getting blackscreen Instead of the data inside textview.

我要转换的屏幕

绘制缓存后得到的屏幕

我不知道我要去哪里错了.

I don't know where I am going wrong.

以下是我的代码

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();
  }

先谢谢了.

推荐答案

透明背景通常会在将其转换为位图时使背景变黑,请尝试将TextView背景框架的背景色设置为类似于白色,然后尝试将其转换为位图.

Transparent background usually causes the background to get black when converted into bitmap, try putting background color of your background frame of TextView to somewhat like white color then try converting it to bitmap.

这篇关于解决黑屏,同时将textview转换为位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆