Konwertowanie widoku na bitmapę bez wyświetlania go w systemie Android?

Postaram się wyjaśnić, co dokładnie muszę zrobić.

Mam 3 oddzielne ekrany powiedzieć A, B, C. Jest Inny ekran o nazwie powiedzieć HomeScreen, gdzie wszystkie 3 ekrany bitmapy powinny być wyświetlane w widoku galerii i użytkownik może wybrać, w którym widoku chce iść.

Udało mi się uzyskać bitmapy wszystkich 3 ekranów i wyświetlić go w widoku galerii, umieszczając cały kod w aktywności ekranu głównego tylko. To bardzo skomplikowało kod i chciałbym uprość to.

Tak, mogę wywołać inną aktywność z ekranu głównego i nie wyświetlać go i po prostu uzyskać bitmapę tego ekranu. Na przykład, powiedzmy, że po prostu wywołuję Ekran główny i wywołuje aktywność A, B, C i żadna z działań z A, B, C nie jest wyświetlana. To po prostu daje bitmapę tego ekranu przez getDrawingCache (). A następnie możemy wyświetlić te bitmapy w widoku galerii na ekranie głównym.

Mam nadzieję, że wyjaśniłem problem bardzo jasno.

Proszę dać mi znać, jeśli to jest rzeczywiście możliwe.

Author: Raghav Sood, 2010-05-10

6 answers

Jest na to sposób. musisz utworzyć bitmapę oraz widok Canvas I call.draw (canvas);

Oto kod:

public static Bitmap loadBitmapFromView(View v) {
    Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);                
    Canvas c = new Canvas(b);
    v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
    v.draw(c);
    return b;
}

Jeśli widok nie był wyświetlany przed jego rozmiarem będzie równy zero. Można to zmierzyć w ten sposób:

if (v.getMeasuredHeight() <= 0) {
    v.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    Bitmap b = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
    v.draw(c);
    return b;
}

EDIT: according to this post, podanie {[3] } jako wartości do makeMeasureSpec() nie robi nic dobrego (chociaż dla niektórych klas view to działa), a zalecana metoda to:

// Either this
int specWidth = MeasureSpec.makeMeasureSpec(parentWidth, MeasureSpec.AT_MOST);
// Or this
int specWidth = MeasureSpec.makeMeasureSpec(0 /* any */, MeasureSpec.UNSPECIFIED);
view.measure(specWidth, specWidth);
int questionWidth = view.getMeasuredWidth();
 174
Author: Simon Heinen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:34:37

Oto moje rozwiązanie:

public static Bitmap getBitmapFromView(View view) {
    Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(returnedBitmap);
    Drawable bgDrawable =view.getBackground();
    if (bgDrawable!=null) 
        bgDrawable.draw(canvas);
    else 
        canvas.drawColor(Color.WHITE);
    view.draw(canvas);
    return returnedBitmap;
}

Enjoy:)

 23
Author: Gil SH,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-03-07 08:12:45

Spróbuj tego,

        /**
         * Draw the view into a bitmap.
         */
        public static Bitmap getViewBitmap(View v) {
            v.clearFocus();
            v.setPressed(false);

            boolean willNotCache = v.willNotCacheDrawing();
            v.setWillNotCacheDrawing(false);

            // Reset the drawing cache background color to fully transparent
            // for the duration of this operation
            int color = v.getDrawingCacheBackgroundColor();
            v.setDrawingCacheBackgroundColor(0);

            if (color != 0) {
                v.destroyDrawingCache();
            }
            v.buildDrawingCache();
            Bitmap cacheBitmap = v.getDrawingCache();
            if (cacheBitmap == null) {
                Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException());
                return null;
            }

            Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);

            // Restore the view
            v.destroyDrawingCache();
            v.setWillNotCacheDrawing(willNotCache);
            v.setDrawingCacheBackgroundColor(color);

            return bitmap;
        }
 20
Author: Dwivedi Ji,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-06-30 05:59:51

Wiem, że to może być nieświeży problem, ale miałem problemy z uzyskaniem któregokolwiek z tych rozwiązań do pracy dla mnie. W szczególności stwierdziłem, że jeśli zostaną wprowadzone jakiekolwiek zmiany w widoku po jego zawyżeniu, zmiany te nie zostaną włączone do renderowanej bitmapy.

Oto metoda, która zadziałała w mojej sprawie. Jednak z jednym zastrzeżeniem. przed wywołaniem getViewBitmap(View) nadmuchałem widok i poprosiłem go o układ ze znanymi wymiarami. Było to potrzebne, ponieważ mój układ widoku sprawi, że zeruje wysokość/szerokość, dopóki zawartość nie zostanie umieszczona wewnątrz.
View view = LayoutInflater.from(context).inflate(layoutID, null);
//Do some stuff to the view, like add an ImageView, etc.
view.layout(0, 0, width, height);

Bitmap getViewBitmap(View view)
{
    //Get the dimensions of the view so we can re-layout the view at its current size
    //and create a bitmap of the same size 
    int width = view.getWidth();
    int height = view.getHeight();

    int measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
    int measuredHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY);

    //Cause the view to re-layout
    view.measure(measuredWidth, measuredHeight);
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

    //Create a bitmap backed Canvas to draw the view into
    Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);

    //Now that the view is laid out and we have a canvas, ask the view to draw itself into the canvas
    view.draw(c);

    return b;
}

"magiczny SOS" dla mnie został znaleziony tutaj: https://groups.google.com/forum/#! topic / Android-developers / BxIBAOeTA1Q

Zdrówko, zdrówko]}

Levi

 10
Author: levigroker,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-18 18:00:35

Hope this helps

View view="some view instance";        
view.setDrawingCacheEnabled(true);
Bitmap bitmap=view.getDrawingCache();
view.setDrawingCacheEnabled(false);
 2
Author: Akhilesh Kumar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-09 09:00:41
view.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
 -3
Author: Ashutosh Gupta,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-30 05:58:43