无法将视图转换为位图

时间:2017-03-20 21:10:14

标签: java android bitmap

我正在尝试将视图转换为位图。我正在关注这段代码,它接受一个视图并返回一个位图

public Bitmap screenShot(View view) {
   view=getWindow().getDecorView().getRootView();
    view.setDrawingCacheEnabled(true);
    Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
    view.setDrawingCacheEnabled(false);
    return bitmap;
}

但我得到了java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference

这是xml中的视图,我尝试将宽度和高度更改为dp,但没有更改

<RelativeLayout
    android:id="@+id/bg_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/titleText"
    android:layout_marginTop="20dp"
    android:background="@color/green_bg">

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:layout_width="145dp"
            android:layout_height="145dp"
            android:layout_marginLeft="30dp"
            android:layout_marginStart="30dp"
            android:layout_marginTop="20dp"
            android:background="@drawable/red_circle">

            <TextView
                android:id="@+id/percentTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:text="80%"
                android:textColor="@android:color/white"
                android:textSize="60sp" />
        </RelativeLayout>
    </FrameLayout>

</RelativeLayout>

这里究竟出了什么问题?

0 个答案:

没有答案