这是我简单的布局:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/my_long_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/long_text_str" />
<Button
android:id="@+id/capture_screen"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="captureScreen"
android:text="Capture Screen" />
</LinearLayout>
</ScrollView>
captureScreen()函数中的以下代码崩溃了应用程序:
TextView tv = (TextView) findViewById(R.id.my_long_textview);
tv.setDrawingCacheEnabled(true);
tv.buildDrawingCache(); // comment out this line makes no difference
bitmap = Bitmap.createBitmap(tv.getDrawingCache()); // this is line 31 in stack trace
tv.setDrawingCacheEnabled(false);
此代码有效但我真的想要捕获整个TextView:
View rv = v.getRootView();
rv.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(rv.getDrawingCache());
rv.setDrawingCacheEnabled(false);
Stack Trace如下:
04-21 00:17:08.023: E/AndroidRuntime(27472): java.lang.IllegalStateException: Could not execute method of the activity
04-21 00:17:08.023: E/AndroidRuntime(27472): at android.view.View$1.onClick(View.java:2072)
04-21 00:17:08.023: E/AndroidRuntime(27472): at android.view.View.performClick(View.java:2408)
04-21 00:17:08.023: E/AndroidRuntime(27472): at android.view.View$PerformClick.run(View.java:8816)
04-21 00:17:08.023: E/AndroidRuntime(27472): at android.os.Handler.handleCallback(Handler.java:587)
04-21 00:17:08.023: E/AndroidRuntime(27472): at android.os.Handler.dispatchMessage(Handler.java:92)
04-21 00:17:08.023: E/AndroidRuntime(27472): at android.os.Looper.loop(Looper.java:123)
04-21 00:17:08.023: E/AndroidRuntime(27472): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-21 00:17:08.023: E/AndroidRuntime(27472): at java.lang.reflect.Method.invokeNative(Native Method)
04-21 00:17:08.023: E/AndroidRuntime(27472): at java.lang.reflect.Method.invoke(Method.java:521)
04-21 00:17:08.023: E/AndroidRuntime(27472): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-21 00:17:08.023: E/AndroidRuntime(27472): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-21 00:17:08.023: E/AndroidRuntime(27472): at dalvik.system.NativeStart.main(Native Method)
04-21 00:17:08.023: E/AndroidRuntime(27472): Caused by: java.lang.reflect.InvocationTargetException
04-21 00:17:08.023: E/AndroidRuntime(27472): at com.gtgg.ui.TextViewAct.captureScreen(TextViewAct.java:31)
04-21 00:17:08.023: E/AndroidRuntime(27472): at java.lang.reflect.Method.invokeNative(Native Method)
04-21 00:17:08.023: E/AndroidRuntime(27472): at java.lang.reflect.Method.invoke(Method.java:521)
04-21 00:17:08.023: E/AndroidRuntime(27472): at android.view.View$1.onClick(View.java:2067)
04-21 00:17:08.023: E/AndroidRuntime(27472): ... 11 more
04-21 00:17:08.023: E/AndroidRuntime(27472): Caused by: java.lang.NullPointerException
04-21 00:17:08.023: E/AndroidRuntime(27472): at android.graphics.Bitmap.createBitmap(Bitmap.java:358)
04-21 00:17:08.023: E/AndroidRuntime(27472): ... 15 more
有没有办法将大文本视图保存到位图中,即使它比rootView大?
非常感谢!