Проблема с фоном изображения на холсте

Я пытаюсь создать изображение из моего RelativeLayout и хочу поделиться им. Мне удалось сгенерировать изображение и поделиться им, но оно рисует черный фон за моим изображением, я пробовал сделать цвет прозрачным, но у него все еще черный фон. Мой код выглядит так

findViewById(R.id.actionImage).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //constant.ad_count = constant.ad_count + 1;
       // methods.ShowIntAds();
        Bitmap bitmap =getBitmapFromView(main);
        try {
            File file = new File(QuoteDetailsActivity.this.getExternalCacheDir(),"logicchip.png");
            FileOutputStream fOut = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
            fOut.flush();
            fOut.close();
            //file.setReadable(true, false);
            final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
            intent.setType("image/png");
            startActivity(Intent.createChooser(intent, "Share image via"));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
});

а также

private Bitmap getBitmapFromView(View view) {
    Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(returnedBitmap);
    canvas.drawColor(Color.TRANSPARENT);
    /*Drawable bgDrawable =view.getBackground();
    if (bgDrawable!=null) {
        //has background drawable, then draw it on the canvas
        bgDrawable.draw(canvas);
    }   else{
        //does not have background drawable, then draw white background on the canvas
        canvas.drawColor(Color.WHITE);
    }*/
    view.draw(canvas);
    return returnedBitmap;
}

Мой XML

<?xml version = "1.0" encoding = "utf-8"?>
<RelativeLayout
    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"
    app:layout_behavior = "@string/appbar_scrolling_view_behavior"
    tools:context = ".activities.QuoteDetailsActivity"
    tools:showIn = "@layout/quote_details_activity">


    <LinearLayout
        android:layout_width = "match_parent"
        android:layout_height = "match_parent"
        android:background = "@drawable/main_background"
        android:orientation = "vertical"
        android:layout_alignParentTop = "true">

        <LinearLayout
            android:layout_width = "match_parent"
            android:layout_height = "wrap_content"
            android:layout_marginLeft = "24dp"
            android:layout_marginRight = "24dp"
            android:layout_marginTop = "4dp"
            android:orientation = "horizontal">

            <ImageView
                android:contentDescription = "@null"
                android:layout_width = "@dimen/likeIcon"
                android:layout_height = "@dimen/likeIcon"
                android:layout_gravity = "center_vertical"
                android:layout_marginEnd = "5dp"
                android:layout_marginRight = "5dp"
                android:layout_marginTop = "1dp"
                android:tint = "@color/quote_time_icon"
                android:src = "@drawable/ic_time" />

            <TextView
                android:id = "@+id/lblTime"
                android:layout_width = "0dp"
                android:layout_height = "wrap_content"
                android:layout_marginEnd = "@dimen/activity_vertical_margin"
                android:layout_marginRight = "@dimen/activity_vertical_margin"
                android:layout_weight = "1"
                android:textColor = "@color/quote_time_text"
                android:textSize = "@dimen/lblTime"
                android:textStyle = "bold" />

            <TextView
                android:id = "@+id/textPageCount1"
                android:layout_width = "wrap_content"
                android:layout_height = "wrap_content"
                android:layout_gravity = "end"
                android:gravity = "end"
                android:textColor = "@color/quote_time_text"
                android:textSize = "@dimen/lblTime"
                android:textStyle = "bold" />

        </LinearLayout>

        <RelativeLayout
            android:id = "@+id/mybg"
            android:layout_width = "match_parent"
            android:layout_height = "0dp"
            android:layout_weight = "1">

            <android.support.v4.view.ViewPager
                android:id = "@+id/quoteViewPager"
                android:layout_width = "match_parent"
                android:layout_height = "match_parent"
                android:layout_alignParentTop = "true"
                android:layout_alignParentLeft = "true"
                android:layout_alignParentStart = "true">
            </android.support.v4.view.ViewPager>


        </RelativeLayout>

        <LinearLayout
            android:id = "@+id/admob"
            android:orientation = "horizontal"
            android:visibility = "gone"
            android:gravity = "center"
            android:layout_marginBottom = "5dp"
            android:layout_width = "match_parent"
            android:layout_height = "wrap_content">
        </LinearLayout>


        <View
            android:id = "@+id/bottomShadow"
            android:layout_width = "fill_parent"
            android:layout_height = "1.5dp"
            android:background = "@color/toolbar_devider" >
        </View>

        <LinearLayout
            android:id = "@+id/pageNavLayout"
            android:layout_width = "match_parent"
            android:layout_height = "@dimen/toolbar"
            android:background = "@drawable/title_bar"
            android:orientation = "horizontal">


            <RelativeLayout
                android:id = "@+id/butPrev"
                style = "@style/SelectableItemBackground"
                android:layout_width = "0dp"
                android:layout_height = "match_parent"
                android:layout_weight = "1"
                android:clickable = "true"
                android:focusable = "true">

                <ImageView
                    android:contentDescription = "@null"
                    android:layout_width = "@dimen/nav_but_size"
                    android:layout_height = "@dimen/nav_but_size"
                    android:scaleType = "fitCenter"
                    android:src = "@drawable/tool_prev"
                    android:tint = "@color/toolbar_icon"
                    android:layout_centerVertical = "true"
                    android:layout_centerHorizontal = "true" />
            </RelativeLayout>

            <TextView
                android:layout_width = "1.5dp"
                android:layout_height = "match_parent"
                android:background = "@color/toolbar_devider" />




            <RelativeLayout

                android:id = "@+id/img_fav"
                style = "@style/SelectableItemBackground"
                android:layout_width = "0dp"
                android:layout_height = "match_parent"
                android:layout_weight = "1"
                android:clickable = "true"
                android:focusable = "true">

                <ImageView
                    android:contentDescription = "@null"
                    android:layout_width = "@dimen/nav_but_size"
                    android:layout_height = "@dimen/nav_but_size"
                    android:layout_centerInParent = "true"
                    android:src = "@drawable/ic_clipboard"
                    android:tint = "@color/toolbar_icon" />
            </RelativeLayout>

            <TextView
                android:layout_width = "1.5dp"
                android:layout_height = "match_parent"
                android:background = "@color/toolbar_devider" />

            <RelativeLayout
                android:id = "@+id/actionImage"
                style = "@style/SelectableItemBackground"
                android:layout_width = "0dp"
                android:layout_height = "match_parent"
                android:layout_weight = "1"
                android:clickable = "true"
                android:focusable = "true">


                <ImageView
                    android:contentDescription = "@null"
                    android:id = "@+id/imgImage"
                    android:layout_width = "@dimen/nav_but_size"
                    android:layout_height = "@dimen/nav_but_size"
                    android:src = "@drawable/ic_background"
                    android:layout_centerInParent = "true"
                    android:tint = "@color/toolbar_icon" />

            </RelativeLayout>

            <TextView
                android:layout_width = "1.5dp"
                android:layout_height = "match_parent"
                android:background = "@color/toolbar_devider" />

            <RelativeLayout
                android:id = "@+id/actionFav"
                style = "@style/SelectableItemBackground"
                android:layout_width = "0dp"
                android:layout_height = "match_parent"
                android:layout_weight = "1"
                android:clickable = "true"
                android:focusable = "true">


                <ImageView
                    android:contentDescription = "@null"
                    android:id = "@+id/imgFavourite"
                    android:layout_width = "@dimen/nav_but_size"
                    android:layout_height = "@dimen/nav_but_size"
                    android:src = "@drawable/fav_new_ic"
                    android:layout_centerInParent = "true"
                    android:tint = "@color/toolbar_icon" />

            </RelativeLayout>

            <TextView
                android:layout_width = "1.5dp"
                android:layout_height = "match_parent"
                android:background = "@color/toolbar_devider" />

            <RelativeLayout

                android:id = "@+id/butShare"
                style = "@style/SelectableItemBackground"
                android:layout_width = "0dp"
                android:layout_height = "match_parent"
                android:layout_weight = "1"
                android:clickable = "true"
                android:focusable = "true">

                <ImageView
                    android:contentDescription = "@null"
                    android:layout_width = "@dimen/nav_but_size"
                    android:layout_height = "@dimen/nav_but_size"
                    android:layout_centerInParent = "true"
                    android:src = "@drawable/fab_share"
                    android:tint = "@color/toolbar_icon" />
            </RelativeLayout>

            <TextView
                android:layout_width = "1.5dp"
                android:layout_height = "match_parent"
                android:background = "@color/toolbar_devider" />

            <RelativeLayout

                android:id = "@+id/butNext"
                style = "@style/SelectableItemBackground"
                android:layout_width = "0dp"
                android:layout_height = "match_parent"
                android:layout_weight = "1"
                android:clickable = "true"
                android:focusable = "true">

                <ImageView
                    android:contentDescription = "@null"
                    android:layout_width = "@dimen/nav_but_size"
                    android:layout_height = "@dimen/nav_but_size"
                    android:layout_centerInParent = "true"
                    android:src = "@drawable/tool_next"
                    android:tint = "@color/toolbar_icon" />
            </RelativeLayout>

        </LinearLayout>

    </LinearLayout>


</RelativeLayout>

Примечание. Я хочу получить изображение представления с именем mybg.

Дайте мне знать, если кто-нибудь может помочь мне создать полноразмерное изображение без черного фона. Спасибо

что возвращает view.getBackground()? это не null? если да, то каковы его границы?

pskink 09.03.2019 12:42

Привет! Показывает ноль, я понятия не имею о границах.

Raju Bhatt 09.03.2019 12:55

значит у тебя нет фона

pskink 09.03.2019 12:56

Мне не нужен никакой фон.

Raju Bhatt 09.03.2019 12:57

опубликуйте свое определение RelativeLayout в xml и растровое изображение, полученное с помощью метода getBitmapFromView

pskink 09.03.2019 12:58

@pskink, я добавил файл xml. Спасибо

Raju Bhatt 09.03.2019 13:00

Давайте продолжить обсуждение в чате.

Raju Bhatt 09.03.2019 13:02
Пользовательский скаляр GraphQL
Пользовательский скаляр GraphQL
Листовые узлы системы типов GraphQL называются скалярами. Достигнув скалярного типа, невозможно спуститься дальше по иерархии типов. Скалярный тип...
Как вычислять биты и понимать побитовые операторы в Java - объяснение с примерами
Как вычислять биты и понимать побитовые операторы в Java - объяснение с примерами
В компьютерном программировании биты играют важнейшую роль в представлении и манипулировании данными на двоичном уровне. Побитовые операции...
Поднятие тревоги для долго выполняющихся методов в Spring Boot
Поднятие тревоги для долго выполняющихся методов в Spring Boot
Приходилось ли вам сталкиваться с требованиями, в которых вас могли попросить поднять тревогу или выдать ошибку, когда метод Java занимает больше...
Полный курс Java для разработчиков веб-сайтов и приложений
Полный курс Java для разработчиков веб-сайтов и приложений
Получите сертификат Java Web и Application Developer, используя наш курс.
0
7
39
0

Другие вопросы по теме