【问题标题】:How to get drawing cache of a popup window?如何获取弹出窗口的绘图缓存?
【发布时间】:2017-02-06 09:35:59
【问题描述】:

我的片段上有一个弹出窗口,我想获取弹出窗口的绘图缓存。 我已经尝试过(如下),但出现空指针异常。我找不到原因。有人可以帮忙吗?

 LayoutInflater inflater2 = (LayoutInflater)         obj.getSystemService(LAYOUT_INFLATER_SERVICE);
        View customView_2 = inflater2.inflate(R.layout.share_lc_card, null);


       // results_page_ = (RelativeLayout) v.findViewById(R.id.fragment_results_layout);
        card_main_layout = (RelativeLayout) customView_2.findViewById(R.id.card_main_layout);

         card_main_layout.invalidate();
                    card_main_layout.requestLayout();
                    card_main_layout.setDrawingCacheEnabled(true);
                    card_main_layout.buildDrawingCache(true);
                    Bitmap drawingCache_bitmap_card  = card_main_layout.getDrawingCache();
                 //   Bitmap drawingCache_bitmap_card = Bitmap.createBitmap(card_main_layout.getDrawingCache());
                    card_main_layout.setDrawingCacheEnabled(false);
                    Log.e("gu", "hghh");


My popup window xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/card_main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="300dp"
    android:layout_height="450dp"
    android:layout_margin="4dp"
    android:padding="30dp"
    card_view:cardCornerRadius="26dp"
    card_view:cardElevation="10sp"
    card_view:cardUseCompatPadding="true">

    <RelativeLayout
        android:layout_width="300dp"
        android:layout_height="450dp">

        <ImageView
            android:id="@+id/background_image"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:src="@drawable/lc_background" />

        <TextView
            android:id="@+id/first_name_textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="80dp"
            android:layout_marginStart="80dp"
            android:layout_marginTop="30dp"

            android:text="@string/romeo" />

        <TextView
            android:id="@+id/and_symbol"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="130dp"
            android:layout_marginStart="130dp"
            android:layout_marginTop="30dp"
            android:text="@string/and" />


        <TextView
            android:id="@+id/second_name_textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="145dp"
            android:layout_marginStart="145dp"
            android:layout_marginTop="30dp"
            android:text="@string/juliet" />

        <ImageView
            android:id="@+id/small_heart_image_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="90dp"
            android:layout_marginStart="90dp"
            android:layout_marginTop="50dp"
            android:src="@drawable/heart_share_card" />

        <TextView
            android:id="@+id/result_message"
            android:layout_width="200dp"
            android:layout_height="100dp"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="40dp"
            android:layout_marginStart="40dp"
            android:layout_marginTop="120dp"
            android:gravity="center"
            android:lineSpacingExtra="-2dp"
            android:text="WOW ! MADE FOR EACH OTHER"
            android:textSize="25dp" />

        <ImageView
            android:id="@+id/small_heart_image_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="100dp"
            android:layout_marginLeft="90dp"
            android:layout_marginStart="90dp"
            android:src="@drawable/heart_share_card" />

        <TextView
            android:id="@+id/score_textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="80dp"
            android:layout_marginLeft="110dp"
            android:layout_marginStart="110dp"
            android:text="90/100" />
    </RelativeLayout>


</android.support.v7.widget.CardView>

我在这条线上得到了 NPE

Bitmap drawingCache_bitmap_card = Bitmap.createBitmap(card_main_layout.getDrawingCache());

异常

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object refe    at android.graphics.Bitmap.createBitmap(Bitmap.java:737)
 at tech.a.com.x.LC_Fragments.ResultsFragment$4.onClick(ResultsFragment.java:207)
 at android.view.View.performClick(View.java:5184)
 at android.view.View$PerformClick.run(View.java:20910)
 at android.os.Handler.handleCallback(Handler.java:739)
 at android.os.Handler.dispatchMessage(Handler.java:95)
 at android.os.Looper.loop(Looper.java:145)
 at android.app.ActivityThread.main(ActivityThread.java:5951)
 at java.lang.reflect.Method.invoke(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:372)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)

【问题讨论】:

    标签: android nullpointerexception popupwindow


    【解决方案1】:

    NullPointerException 在应用程序尝试使用具有 null 值的对象引用时抛出。

    首先,确保您的 Imageview 不是 Empty 。

            img_Obj.buildDrawingCache();
            Bitmap image = img_Obj.getDrawingCache();
    

    阅读createBitmap

    createBitmap(Bitmap source, int x, int y, int width, int height)
    

    问题

     card_main_layout = (RelativeLayout) customView_2.findViewById(R.id.card_main_layout);
    

    解决方案 1

     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/card_main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/lc_background">
    

    解决方案 2

    您的 card_main_layout 为空。尝试使用 ImageView

    ImageView background_imageObj= (ImageView) customView_2.findViewById(R.id.background_image);
    

    【讨论】:

    【解决方案2】:

    下面我创建了一个弹出窗口,方法是点击弹出窗口的按钮,它从弹出窗口创建位图,请检查下面的方法和 XML

    方法:java

    PopupWindow popupWindow = null;
    Bitmap bitmap = null;
    
    
     private void popUp() {
                LayoutInflater layoutInflater
                        = (LayoutInflater) getBaseContext()
                        .getSystemService(LAYOUT_INFLATER_SERVICE);
    
                View popupView = layoutInflater.inflate(R.layout.popup_window, null);
                popupWindow = new PopupWindow(
                        popupView,
                        LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
    
                Button btnDismiss = (Button) popupView.findViewById(R.id.dismiss);
                btnDismiss.setOnClickListener(new Button.OnClickListener() {
    
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        try {
                            View view = popupWindow.getContentView();
                            view.setDrawingCacheEnabled(true);
                            view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_LOW);
                            view.buildDrawingCache();
                            bitmap = Bitmap.createBitmap(view.getDrawingCache());
                            view.setDrawingCacheEnabled(false);
                            view.destroyDrawingCache();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        imageView.setImageBitmap(bitmap);
                        popupWindow.dismiss();
                    }
                });
    
                popupWindow.showAsDropDown(button, 50, -30);
            }
    

    XML-popup_window.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/rootView"
        android:background="@android:color/background_light"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:background="@android:color/darker_gray"
            android:orientation="vertical">
            >
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="20dp"
                android:orientation="vertical">
                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="It's a PopupWindow" />
                <ImageView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_menu_share" />
                <Button
                    android:id="@+id/dismiss"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="Dismiss" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
    

    为了显示创建的位图,我在主布局中使用了一个图像视图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-07
      • 2012-01-02
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 2011-08-20
      • 1970-01-01
      相关资源
      最近更新 更多