Captures a bitmap of a View and draws it to a Canvas. - Android User Interface

Android examples for User Interface:View Bitmap

Description

Captures a bitmap of a View and draws it to a Canvas.

Demo Code

//package com.java2s;
import android.graphics.Canvas;
import android.view.View;
import android.view.ViewGroup;

public class Main {
    /**//  w w w  . j  a  v  a 2 s  . com
     * Captures a bitmap of a View and draws it to a Canvas.
     */
    public static void captureBitmap(View view, Canvas canvas) {
        recursiveInvalidate(view);
        view.draw(canvas);
    }

    /**
     * Invalidates a view and all of its descendants.
     */
    private static void recursiveInvalidate(View view) {
        view.invalidate();
        if (view instanceof ViewGroup) {
            ViewGroup group = (ViewGroup) view;
            int childCount = group.getChildCount();
            for (int i = 0; i < childCount; i++) {
                View child = group.getChildAt(i);
                if (child.getVisibility() == View.VISIBLE) {
                    recursiveInvalidate(child);
                }
            }
        }
    }
}

Related Tutorials