/* * Copyright (C) 2014 The CyanogenMod Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.cyanogenmod.themes.provider.util; import android.graphics.Bitmap; import android.graphics.Canvas; import android.view.View; import android.widget.FrameLayout; public class LayoutRenderUtils { public static Bitmap renderViewToBitmap(View view) { // Provide it with a layout params. It should necessarily be wrapping the // content as we not really going to have a parent for it. view.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT)); // Pre-measure the view so that height and width don't remain null. view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); // Assign a size and position to the view and all of its descendants view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); // Create the bitmap Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888); // Create a canvas with the specified bitmap to draw into Canvas c = new Canvas(bitmap); // Render this view to the given Canvas view.draw(c); return bitmap; } }