Java Code Examples for android.view.View#setDrawingCacheBackgroundColor()

The following examples show how to use android.view.View#setDrawingCacheBackgroundColor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AppUtils.java    From YCWebView with Apache License 2.0 6 votes vote down vote up
/**
 * 填充布局内容
 */
private static  Bitmap layoutView(final View viewBitmap, int width, int height) {
    // 整个View的大小 参数是左上角 和右下角的坐标
    viewBitmap.layout(0, 0, width, height);
    int measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
    int measuredHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.UNSPECIFIED);
    viewBitmap.measure(measuredWidth, measuredHeight);
    viewBitmap.layout(0, 0, viewBitmap.getMeasuredWidth(), viewBitmap.getMeasuredHeight());
    viewBitmap.setDrawingCacheEnabled(true);
    viewBitmap.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
    viewBitmap.setDrawingCacheBackgroundColor(Color.WHITE);
    // 把一个View转换成图片
    Bitmap cachebmp = viewConversionBitmap(viewBitmap);
    viewBitmap.destroyDrawingCache();
    return cachebmp;
}
 
Example 2
Source File: X5WebUtils.java    From YCWebView with Apache License 2.0 6 votes vote down vote up
/**
 * 填充布局内容
 * @param viewBitmap            view
 * @param width                 宽
 * @param height                高
 * @return
 */
private static  Bitmap layoutView(final View viewBitmap, int width, int height) {
    // 整个View的大小 参数是左上角 和右下角的坐标
    viewBitmap.layout(0, 0, width, height);
    //宽,父容器已经检测出view所需的精确大小,这时候view的最终大小SpecSize所指定的值,相当于match_parent或指定具体数值。
    int measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
    //高,表示父容器不对View有任何限制,一般用于系统内部,表示一种测量状态;
    int measuredHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.UNSPECIFIED);
    viewBitmap.measure(measuredWidth, measuredHeight);
    viewBitmap.layout(0, 0, viewBitmap.getMeasuredWidth(), viewBitmap.getMeasuredHeight());
    viewBitmap.setDrawingCacheEnabled(true);
    viewBitmap.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
    viewBitmap.setDrawingCacheBackgroundColor(Color.WHITE);
    // 把一个View转换成图片
    Bitmap cachebmp = viewConversionBitmap(viewBitmap);
    viewBitmap.destroyDrawingCache();
    return cachebmp;
}
 
Example 3
Source File: ViewShot.java    From zone-sdk with MIT License 5 votes vote down vote up
/**
 * @param view
 * @return
 * @deprecated use {@link #getCacheBitmap(View,Bitmap.Config)}
 * <br>感觉这个不好  会清除一些状态</br>
 * 仅仅为了得到的位图是RGB 565吧
 * 但是 结果来看 大部分还是RGB8888 要和  View.mUse32BitDrawingCache相关
 */
public static Bitmap getCacheBitmap_ConifgByWindow(View view) {
    // 将一个View转化成一张图片
    view.clearFocus(); // 清除视图焦点
    view.setPressed(false);// 将视图设为不可点击

    boolean willNotCache = view.willNotCacheDrawing(); // 返回视图是否可以保存他的画图缓存
    view.setWillNotCacheDrawing(false);

    // Reset the drawing cache background color to fully transparent
    // for the duration of this operation //将视图在此操作时置为透明
    int color = view.getDrawingCacheBackgroundColor(); // 获得绘制缓存位图的背景颜色
    view.setDrawingCacheBackgroundColor(0); // 设置绘图背景颜色
    if (color != 0) { // 如果获得的背景不是黑色的则释放以前的绘图缓存
        view.destroyDrawingCache(); // 释放绘图资源所使用的缓存
    }
    view.buildDrawingCache(); // 重新创建绘图缓存,此时的背景色是黑色
    Bitmap cacheBitmap = view.getDrawingCache(); // 将绘图缓存得到的,注意这里得到的只是一个图像的引用
    if (cacheBitmap == null)
        return failLog(view);
    Bitmap bitmap = Bitmap.createBitmap(cacheBitmap); // 将位图实例化
    // Restore the view //恢复视图
    view.destroyDrawingCache();// 释放位图内存
    view.setWillNotCacheDrawing(willNotCache);// 返回以前缓存设置
    view.setDrawingCacheBackgroundColor(color);// 返回以前的缓存颜色设置
    return bitmap;
}
 
Example 4
Source File: BgSparklineBuilder.java    From NightWatch with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Draw the view into a bitmap.
 */
private 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;
}
 
Example 5
Source File: DragController.java    From Trebuchet with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Draw the view into a bitmap.
 */
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);
    float alpha = v.getAlpha();
    v.setAlpha(1.0f);

    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.setAlpha(alpha);
    v.setWillNotCacheDrawing(willNotCache);
    v.setDrawingCacheBackgroundColor(color);

    return bitmap;
}
 
Example 6
Source File: BitmapUtil.java    From AndroidStudyDemo with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 把一个View的对象转换成bitmap
 *
 * @param view View
 * @return Bitmap
 */
public static Bitmap getBitmapFromView2(View view) {

    view.clearFocus();
    view.setPressed(false);

    // 能画缓存就返回false
    boolean willNotCache = view.willNotCacheDrawing();
    view.setWillNotCacheDrawing(false);
    int color = view.getDrawingCacheBackgroundColor();
    view.setDrawingCacheBackgroundColor(0);
    if (color != 0) {
        view.destroyDrawingCache();
    }
    view.buildDrawingCache();
    Bitmap cacheBitmap = view.getDrawingCache();
    if (cacheBitmap == null) {
        if (DEBUG) {
            Logger.e("failed getViewBitmap(" + view + ")",
                    new RuntimeException());
        }
        return null;
    }
    Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
    // Restore the view
    view.destroyDrawingCache();
    view.setWillNotCacheDrawing(willNotCache);
    view.setDrawingCacheBackgroundColor(color);
    return bitmap;
}
 
Example 7
Source File: FileOperateUtils.java    From LLApp with Apache License 2.0 5 votes vote down vote up
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("TTTTTTTTActivity", "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;
}
 
Example 8
Source File: Utils.java    From Noyze with Apache License 2.0 5 votes vote down vote up
public static Bitmap loadBitmapFromViewCache(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 (null == cacheBitmap) return null;
    Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);

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

    return bitmap;
}
 
Example 9
Source File: BgSparklineBuilder.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Draw the view into a bitmap.
 */
private 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(Color.TRANSPARENT);

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

        v.destroyDrawingCache(); // duplicate of below, flow could be reordered better
        v.setWillNotCacheDrawing(willNotCache);
        v.setDrawingCacheBackgroundColor(color);
        return null;
    }

    Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);

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

    return bitmap;
}
 
Example 10
Source File: BgSparklineBuilder.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Draw the view into a bitmap.
 */
protected 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(Color.TRANSPARENT);

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

        v.destroyDrawingCache(); // duplicate of below, flow could be reordered better
        v.setWillNotCacheDrawing(willNotCache);
        v.setDrawingCacheBackgroundColor(color);
        return null;
    }

    Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);

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

    return bitmap;
}
 
Example 11
Source File: BgSparklineBuilder.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Draw the view into a bitmap.
 */
private 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(Color.TRANSPARENT);

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

        v.destroyDrawingCache(); // duplicate of below, flow could be reordered better
        v.setWillNotCacheDrawing(willNotCache);
        v.setDrawingCacheBackgroundColor(color);
        return null;
    }

    Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);

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

    return bitmap;
}
 
Example 12
Source File: XBitmapUtils.java    From XFrame with Apache License 2.0 5 votes vote down vote up
/**
 * 把一个View的对象转换成bitmap
 *
 * @param view View
 * @return Bitmap
 */
public static Bitmap getBitmapFromView2(View view) {
    if (view == null) {
        return null;
    }
    view.clearFocus();
    view.setPressed(false);

    // 能画缓存就返回false
    boolean willNotCache = view.willNotCacheDrawing();
    view.setWillNotCacheDrawing(false);
    int color = view.getDrawingCacheBackgroundColor();
    view.setDrawingCacheBackgroundColor(0);
    if (color != 0) {
        view.destroyDrawingCache();
    }
    view.buildDrawingCache();
    Bitmap cacheBitmap = view.getDrawingCache();
    if (cacheBitmap == null) {
        XPrintUtils.e( "failed getViewBitmap(" + view + ") -->"+
                    new RuntimeException());
        return null;
    }
    Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
    // Restore the view
    view.destroyDrawingCache();
    view.setWillNotCacheDrawing(willNotCache);
    view.setDrawingCacheBackgroundColor(color);
    return bitmap;
}
 
Example 13
Source File: Utils.java    From Noyze with Apache License 2.0 5 votes vote down vote up
public static Bitmap loadBitmapFromViewCache(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 (null == cacheBitmap) return null;
    Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);

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

    return bitmap;
}
 
Example 14
Source File: ImageUtils.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
/**
 * 通过 View Cache 绘制为 Bitmap
 * @param view {@link View}
 * @return {@link Bitmap}
 */
public static Bitmap getBitmapFromViewCache(final View view) {
    if (view == null) return null;
    try {
        // 清除视图焦点
        view.clearFocus();
        // 将视图设为不可点击
        view.setPressed(false);

        // 获取视图是否可以保存画图缓存
        boolean willNotCache = view.willNotCacheDrawing();
        view.setWillNotCacheDrawing(false);

        // 获取绘制缓存位图的背景颜色
        int color = view.getDrawingCacheBackgroundColor();
        // 设置绘图背景颜色
        view.setDrawingCacheBackgroundColor(0);
        if (color != 0) { // 获取的背景不是黑色的则释放以前的绘图缓存
            view.destroyDrawingCache(); // 释放绘图资源所使用的缓存
        }

        // 重新创建绘图缓存, 此时的背景色是黑色
        view.buildDrawingCache();
        // 获取绘图缓存, 注意这里得到的只是一个图像的引用
        Bitmap cacheBitmap = view.getDrawingCache();
        if (cacheBitmap == null) return null;

        Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
        // 释放位图内存
        view.destroyDrawingCache();
        // 回滚以前的缓存设置、缓存颜色设置
        view.setWillNotCacheDrawing(willNotCache);
        view.setDrawingCacheBackgroundColor(color);
        return bitmap;
    } catch (Exception e) {
        LogPrintUtils.eTag(TAG, e, "getBitmapFromViewCache");
    }
    return null;
}
 
Example 15
Source File: BgSparklineBuilder.java    From NightWatch with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Draw the view into a bitmap.
 */
private 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;
}
 
Example 16
Source File: BgSparklineBuilder.java    From NightWatch with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Draw the view into a bitmap.
 */
private 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;
}
 
Example 17
Source File: IcsListPopupWindow.java    From CSipSimple with GNU General Public License v3.0 4 votes vote down vote up
private int measureHeightOfChildren(int widthMeasureSpec, int startPosition, int endPosition,
        final int maxHeight, int disallowPartialChildPosition) {

    final ListAdapter adapter = mAdapter;
    if (adapter == null) {
        return mDropDownList.getListPaddingTop() + mDropDownList.getListPaddingBottom();
    }

    // Include the padding of the list
    int returnedHeight = mDropDownList.getListPaddingTop() + mDropDownList.getListPaddingBottom();
    final int dividerHeight = ((mDropDownList.getDividerHeight() > 0) && mDropDownList.getDivider() != null) ? mDropDownList.getDividerHeight() : 0;
    // The previous height value that was less than maxHeight and contained
    // no partial children
    int prevHeightWithoutPartialChild = 0;
    int i;
    View child;

    // mItemCount - 1 since endPosition parameter is inclusive
    endPosition = (endPosition == -1/*NO_POSITION*/) ? adapter.getCount() - 1 : endPosition;

    for (i = startPosition; i <= endPosition; ++i) {
        child = mAdapter.getView(i, null, mDropDownList);
        if (mDropDownList.getCacheColorHint() != 0) {
            child.setDrawingCacheBackgroundColor(mDropDownList.getCacheColorHint());
        }

        measureScrapChild(child, i, widthMeasureSpec);

        if (i > 0) {
            // Count the divider for all but one child
            returnedHeight += dividerHeight;
        }

        returnedHeight += child.getMeasuredHeight();

        if (returnedHeight >= maxHeight) {
            // We went over, figure out which height to return.  If returnedHeight > maxHeight,
            // then the i'th position did not fit completely.
            return (disallowPartialChildPosition >= 0) // Disallowing is enabled (> -1)
                        && (i > disallowPartialChildPosition) // We've past the min pos
                        && (prevHeightWithoutPartialChild > 0) // We have a prev height
                        && (returnedHeight != maxHeight) // i'th child did not fit completely
                    ? prevHeightWithoutPartialChild
                    : maxHeight;
        }

        if ((disallowPartialChildPosition >= 0) && (i >= disallowPartialChildPosition)) {
            prevHeightWithoutPartialChild = returnedHeight;
        }
    }

    // At this point, we went through the range of children, and they each
    // completely fit, so return the returnedHeight
    return returnedHeight;
}
 
Example 18
Source File: ConvertUtils.java    From AndroidDownload with Apache License 2.0 4 votes vote down vote up
/**
 * 把view转化为bitmap(截图)
 * 参见:http://www.cnblogs.com/lee0oo0/p/3355468.html
 */
public static Bitmap toBitmap(View view) {
    int width = view.getWidth();
    int height = view.getHeight();
    if (view instanceof ListView) {
        height = 0;
        // 获取listView实际高度
        ListView listView = (ListView) view;
        for (int i = 0; i < listView.getChildCount(); i++) {
            height += listView.getChildAt(i).getHeight();
        }
    } else if (view instanceof ScrollView) {
        height = 0;
        // 获取scrollView实际高度
        ScrollView scrollView = (ScrollView) view;
        for (int i = 0; i < scrollView.getChildCount(); i++) {
            height += scrollView.getChildAt(i).getHeight();
        }
    }
    view.setDrawingCacheEnabled(true);
    view.clearFocus();
    view.setPressed(false);
    boolean willNotCache = view.willNotCacheDrawing();
    view.setWillNotCacheDrawing(false);
    // Reset the drawing cache background color to fully transparent for the duration of this operation
    int color = view.getDrawingCacheBackgroundColor();
    view.setDrawingCacheBackgroundColor(Color.WHITE);//截图去黑色背景(透明像素)
    if (color != Color.WHITE) {
        view.destroyDrawingCache();
    }
    view.buildDrawingCache();
    Bitmap cacheBitmap = view.getDrawingCache();
    if (cacheBitmap == null) {
        return null;
    }
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    canvas.drawBitmap(cacheBitmap, 0, 0, null);
    canvas.save(Canvas.ALL_SAVE_FLAG);
    canvas.restore();
    if (!bitmap.isRecycled()) {
        LogUtils.verbose("recycle bitmap: " + bitmap.toString());
        bitmap.recycle();
    }
    // Restore the view
    view.destroyDrawingCache();
    view.setWillNotCacheDrawing(willNotCache);
    view.setDrawingCacheBackgroundColor(color);
    return bitmap;
}
 
Example 19
Source File: ConvertUtils.java    From MyBookshelf with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 把view转化为bitmap(截图)
 * 参见:http://www.cnblogs.com/lee0oo0/p/3355468.html
 */
public static Bitmap toBitmap(View view) {
    int width = view.getWidth();
    int height = view.getHeight();
    if (view instanceof ListView) {
        height = 0;
        // 获取listView实际高度
        ListView listView = (ListView) view;
        for (int i = 0; i < listView.getChildCount(); i++) {
            height += listView.getChildAt(i).getHeight();
        }
    } else if (view instanceof ScrollView) {
        height = 0;
        // 获取scrollView实际高度
        ScrollView scrollView = (ScrollView) view;
        for (int i = 0; i < scrollView.getChildCount(); i++) {
            height += scrollView.getChildAt(i).getHeight();
        }
    }
    view.setDrawingCacheEnabled(true);
    view.clearFocus();
    view.setPressed(false);
    boolean willNotCache = view.willNotCacheDrawing();
    view.setWillNotCacheDrawing(false);
    // Reset the drawing cache background color to fully transparent for the duration of this operation
    int color = view.getDrawingCacheBackgroundColor();
    view.setDrawingCacheBackgroundColor(Color.WHITE);//截图去黑色背景(透明像素)
    if (color != Color.WHITE) {
        view.destroyDrawingCache();
    }
    view.buildDrawingCache();
    Bitmap cacheBitmap = view.getDrawingCache();
    if (cacheBitmap == null) {
        return null;
    }
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    canvas.drawBitmap(cacheBitmap, 0, 0, null);
    canvas.save();
    canvas.restore();
    if (!bitmap.isRecycled()) {
        bitmap.recycle();
    }
    // Restore the view
    view.destroyDrawingCache();
    view.setWillNotCacheDrawing(willNotCache);
    view.setDrawingCacheBackgroundColor(color);
    return bitmap;
}
 
Example 20
Source File: AbsHListView.java    From Klyph with MIT License 4 votes vote down vote up
/**
 * Get a view and have it show the data associated with the specified position. This is called when we have already discovered
 * that the view is not available for reuse in the recycle bin. The only choices left are converting an old view or making a new
 * one.
 * 
 * @param position
 *           The position to display
 * @param isScrap
 *           Array of at least 1 boolean, the first entry will become true if the returned view was taken from the scrap heap,
 *           false if otherwise.
 * 
 * @return A view displaying the data associated with the specified position
 */
@SuppressLint ( "NewApi" )
protected View obtainView( int position, boolean[] isScrap ) {
	isScrap[0] = false;
	View scrapView;

	scrapView = mRecycler.getTransientStateView( position );
	if ( scrapView != null ) {
		return scrapView;
	}

	scrapView = mRecycler.getScrapView( position );

	View child;
	if ( scrapView != null ) {
		child = mAdapter.getView( position, scrapView, this );

		if ( android.os.Build.VERSION.SDK_INT >= 16 ) {
			if ( child.getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO ) {
				child.setImportantForAccessibility( IMPORTANT_FOR_ACCESSIBILITY_YES );
			}
		}

		if ( child != scrapView ) {
			mRecycler.addScrapView( scrapView, position );
			if ( mCacheColorHint != 0 ) {
				child.setDrawingCacheBackgroundColor( mCacheColorHint );
			}
		} else {
			isScrap[0] = true;
			child.onFinishTemporaryDetach();
		}
	} else {
		child = mAdapter.getView( position, null, this );

		if ( android.os.Build.VERSION.SDK_INT >= 16 ) {
			if ( child.getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO ) {
				child.setImportantForAccessibility( IMPORTANT_FOR_ACCESSIBILITY_YES );
			}
		}

		if ( mCacheColorHint != 0 ) {
			child.setDrawingCacheBackgroundColor( mCacheColorHint );
		}
	}

	if ( mAdapterHasStableIds ) {
		final ViewGroup.LayoutParams vlp = child.getLayoutParams();
		LayoutParams lp;
		if ( vlp == null ) {
			lp = (LayoutParams) generateDefaultLayoutParams();
		} else if ( !checkLayoutParams( vlp ) ) {
			lp = (LayoutParams) generateLayoutParams( vlp );
		} else {
			lp = (LayoutParams) vlp;
		}
		lp.itemId = mAdapter.getItemId( position );
		child.setLayoutParams( lp );
	}

	if ( mAccessibilityManager.isEnabled() ) {
		if ( mAccessibilityDelegate == null ) {
			mAccessibilityDelegate = new ListItemAccessibilityDelegate();
		}

		// TODO: implement this ( hidden by google )
		// if (child.getAccessibilityDelegate() == null) {
		// child.setAccessibilityDelegate(mAccessibilityDelegate);
		// }
	}

	return child;
}