从GestureOverlayView生成位图

时间:2011-12-07 16:55:54

标签: java android

我有一个应用需要捕获客户的签名,然后将其呈现在ImageView中。为了概念验证,我编写了一个小应用程序,其顶部有一个GestureOverlayView布局,然后是“Save Signature”按钮,然后是下面的ImageView。当我按下Save Signature按钮时,它应该从GestureOverlayView创建一个图像并将其显示在ImageView中。下面是代码/布局,然后是问题列表:

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <android.gesture.GestureOverlayView android:id="@+id/GestureView" 
        android:layout_width="match_parent" 
        android:layout_height="200dp"
        android:background="#FFFFFF" 
        android:fadeEnabled="false"
        android:gestureStrokeType="multiple"
        android:gestureStrokeWidth="1.5"
        android:gestureColor="#000000"
        android:fadeOffset="600000"
        android:eventsInterceptionEnabled="false"
        android:alwaysDrawnWithCache="true">
    </android.gesture.GestureOverlayView>
    <Button android:id="@+id/SubmitButton"  
        android:text="Save Signature" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content">
    </Button>
    <ImageView android:id="@+id/ImageView"
        android:layout_width="match_parent" 
        android:layout_height="200dp"
        android:background="#FFFFFF">
    </ImageView>
</LinearLayout>

代码:

public class TestActivity extends Activity {
    ImageView iv;
    GestureOverlayView gv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        iv = (ImageView)findViewById(R.id.ImageView);
        gv = (GestureOverlayView)findViewById(R.id.GestureView);

        Button submitButton = (Button)findViewById(R.id.SubmitButton);
        submitButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Bitmap b = Bitmap.createBitmap(gv.getDrawingCache());
                iv.setImageBitmap(b);
            }
        });
    }
}

每次在createBitmap()方法上都会出现Null Pointer异常。我相信这是因为gv.getDrawingCaches()返回null。如您所见,绘制缓存已启用,因此它应返回SOMETHING!

这是logcat:

FATAL EXCEPTION: main
java.lang.NullPointerException
    at android.graphics.Bitmap.createBitmap(Bitmap.java:358)
    at com.pgov.barcode.TestActivity$2.onClick(TestActivity.java:42)
    at android.view.View.performClick(View.java:2408)
    at android.view.View$PerformClick.run(View.java:8816)
    at android.os.Handler.handleCallback(Handler.java:587)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4627)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    at dalvik.system.NativeStart.main(Native Method)

非常感谢任何和所有帮助!

1 个答案:

答案 0 :(得分:2)

显然我回答了自己的问题。即使我用过:

android:alwaysDrawnWithCache="true"

我将代码更改为:

gv.setDrawingCacheEnabled(true);
Bitmap b = Bitmap.createBitmap(gv.getDrawingCache());
iv.setImageBitmap(b);
gv.setDrawingCacheEnabled(false);

它有效!不确定为什么XML标志不会影响它...