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

The following examples show how to use android.view.View#buildDrawingCache() . 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: BitmapUtils.java    From GridBuilder with Apache License 2.0 6 votes vote down vote up
/**
 * 将View转换成Bitmap
 */
public static Bitmap convertViewToBitmap(View view, int outMargin, int baseHeight) {
    Bitmap bitmap;
    view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
            , View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    if (baseHeight == 0) {
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        view.buildDrawingCache();
        bitmap = view.getDrawingCache();
    } else {
        bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bitmap);
        view.draw(c);
    }

    int height = bitmap.getHeight();
    int width = bitmap.getWidth();

    return Bitmap.createBitmap(bitmap
            , outMargin
            , height - height / PART_NUM - baseHeight + (baseHeight > 0 ? outMargin / PART_NUM : 0)
            , width - outMargin * 2
            , (height - outMargin * 2) / PART_NUM);
}
 
Example 2
Source File: ScreenUtils.java    From ClockView with Apache License 2.0 6 votes vote down vote up
/**
 * 获取当前屏幕截图,不包含状态栏
 * 
 * @param activity
 * @return
 */
public static Bitmap snapShotWithoutStatusBar(Activity activity)
{
	View view = activity.getWindow().getDecorView();
	view.setDrawingCacheEnabled(true);
	view.buildDrawingCache();
	Bitmap bmp = view.getDrawingCache();
	Rect frame = new Rect();
	activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
	int statusBarHeight = frame.top;

	int width = getScreenWidth(activity);
	int height = getScreenHeight(activity);
	Bitmap bp = null;
	bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height
			- statusBarHeight);
	view.destroyDrawingCache();
	return bp;

}
 
Example 3
Source File: SlidingDrawer.java    From flickr-uploader with GNU General Public License v2.0 6 votes vote down vote up
private void prepareContent() {
	if (mAnimating) {
		return;
	}

	// Something changed in the content, we need to honor the layout request
	// before creating the cached bitmap
	final View content = mContent;
	if (content.isLayoutRequested()) {
		final int childHeight = mHandleHeight;
		int height = getBottom() - getTop() - childHeight - mTopOffset;
		content.measure(MeasureSpec.makeMeasureSpec(getRight() - getLeft(), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
		content.layout(0, mTopOffset + childHeight, content.getMeasuredWidth(), mTopOffset + childHeight + content.getMeasuredHeight());
	}
	// Try only once… we should really loop but it's not a big deal
	// if the draw was cancelled, it will only be temporary anyway
	content.getViewTreeObserver().dispatchOnPreDraw();
	if (!content.isHardwareAccelerated())
		content.buildDrawingCache();

	content.setVisibility(View.GONE);
}
 
Example 4
Source File: Fglass.java    From Fglass with Apache License 2.0 6 votes vote down vote up
/**
 * 设置高斯模糊
 *
 * ps:
 * 设置高斯模糊是依靠scaleFactor和radius配合使用的,比如这里默认设置是:scaleFactor = 8;radius = 2; 模糊效果和scaleFactor = 1;radius = 20;是一样的,而且效率高
 * @param fromView 从某个View获取截图
 * @param toView 高斯模糊设置到某个View上
 * @param radius 模糊度
 * @param scaleFactor 缩放比例
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static void blur(View fromView, View toView,float radius,float scaleFactor) {

    //获取View的截图
    fromView.buildDrawingCache();
    Bitmap bkg = fromView.getDrawingCache();

    if (radius<1||radius>26) {
        scaleFactor = 8;
        radius = 2;
    }

    Bitmap overlay = Bitmap.createBitmap((int) (toView.getMeasuredWidth()/scaleFactor),
            (int) (toView.getMeasuredHeight()/scaleFactor), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(overlay);
    canvas.translate(-toView.getLeft()/scaleFactor, -toView.getTop()/scaleFactor);
    canvas.scale(1 / scaleFactor, 1 / scaleFactor);
    Paint paint = new Paint();
    paint.setFlags(Paint.FILTER_BITMAP_FLAG);
    canvas.drawBitmap(bkg, 0, 0, paint);

    overlay = Fglass.doBlur(overlay, (int) radius, true);
    toView.setBackground(new BitmapDrawable(overlay));

}
 
Example 5
Source File: ScreenUtil.java    From Android-Architecture with Apache License 2.0 6 votes vote down vote up
/**
 * 获取当前屏幕截图,不包含状态栏
 *
 * @param activity
 * @return
 */
public static Bitmap snapShotWithoutStatusBar(Activity activity) {
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap bmp = view.getDrawingCache();
    Rect frame = new Rect();
    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
    int statusBarHeight = frame.top;

    int width = getScreenWidth(activity);
    int height = getScreenHeight(activity);
    Bitmap bp = null;
    bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height - statusBarHeight);
    view.destroyDrawingCache();
    return bp;
}
 
Example 6
Source File: ScreenUtils.java    From Gizwits-SmartSocket_Android with MIT License 6 votes vote down vote up
/**
* 获取屏幕截图,不包含状态栏
* 
* @param activity
*            activity
* @return   屏幕截图
* 
* */
  public static Bitmap snapShotWithoutStatusBar(Activity activity)
  {
      View view = activity.getWindow().getDecorView();
      view.setDrawingCacheEnabled(true);
      view.buildDrawingCache();
      Bitmap bmp = view.getDrawingCache();
      Rect frame = new Rect();
      activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
      int statusBarHeight = frame.top;
 
      int width = getScreenWidth(activity);
      int height = getScreenHeight(activity);
      Bitmap bp = null;
      bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height
              - statusBarHeight);
      view.destroyDrawingCache();
      return bp;
 
  }
 
Example 7
Source File: ScreenUtils.java    From WelikeAndroid with Apache License 2.0 6 votes vote down vote up
/**
 * 获取当前屏幕截图,包含状态栏
 *
 * @param activity
 * @return
 */
public static Bitmap snapShotWithStatusBar(Activity activity) {

    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap bmp = view.getDrawingCache();
    int width = getScreenWidth(activity);
    int height = getScreenHeight(activity);
    Bitmap bp = null;
    bp = Bitmap.createBitmap(bmp, 0, 0, width, height);
    view.destroyDrawingCache();
    return bp;


}
 
Example 8
Source File: MultipleOrientationSlidingDrawer.java    From prayer-times-android with Apache License 2.0 6 votes vote down vote up
private void prepareContent() {

        if (mAnimating) {
            return;
        }

        // Something changed in the content, we need to honor the layout request
        // before creating the cached bitmap
        View content = mContent;
        if (content.isLayoutRequested()) {
            measureContent();
        }

        // Try only once... we should really loop but it's not a big deal
        // if the draw was cancelled, it will only be temporary anyway
        content.getViewTreeObserver().dispatchOnPreDraw();
        content.buildDrawingCache();

        content.setVisibility(View.GONE);
    }
 
Example 9
Source File: ScreenUtils.java    From LLApp with Apache License 2.0 6 votes vote down vote up
/**
 * 获取当前屏幕截图,不包含状态栏
 *
 * @param activity
 * @return
 */
public static Bitmap snapShotWithoutStatusBar(Activity activity) {
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap bmp = view.getDrawingCache();
    Rect frame = new Rect();
    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
    int statusBarHeight = frame.top;

    int width = getScreenWidth(activity);
    int height = getScreenHeight(activity);
    Bitmap bp = null;
    bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height
            - statusBarHeight);
    view.destroyDrawingCache();
    return bp;
}
 
Example 10
Source File: ScreenUtils.java    From PlayTogether with Apache License 2.0 5 votes vote down vote up
/**
 * 获取当前屏幕截图,包含状态栏
 *
 * @param activity
 * @return
 */
public static Bitmap snapShotWithStatusBar(Activity activity)
{
	View view = activity.getWindow().getDecorView();
	view.setDrawingCacheEnabled(true);
	view.buildDrawingCache();
	Bitmap bmp = view.getDrawingCache();
	int width = getScreenWidth(activity);
	int height = getScreenHeight(activity);
	Bitmap bp = null;
	bp = Bitmap.createBitmap(bmp, 0, 0, width, height);
	view.destroyDrawingCache();
	return bp;

}
 
Example 11
Source File: CapturePictureUtils.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
/**
 * 获取当前屏幕截图, 不包含状态栏 ( 如果 android:theme 全屏, 则截图无状态栏 )
 * @param activity {@link Activity}
 * @return 当前屏幕截图, 不包含状态栏
 */
public static Bitmap snapshotWithoutStatusBar(final Activity activity) {
    try {
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        // 重新创建绘图缓存, 此时的背景色是黑色
        view.buildDrawingCache();
        // 获取绘图缓存, 注意这里得到的只是一个图像的引用
        Bitmap cacheBitmap = view.getDrawingCache();
        if (cacheBitmap == null) return null;
        // 获取屏幕宽度
        int[] widthHeight = ScreenUtils.getScreenWidthHeight();
        // 获取状态栏高度
        int statusBarHeight = BarUtils.getStatusBarHeight();

        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        // 创建新的图片
        Bitmap bitmap = Bitmap.createBitmap(cacheBitmap, 0, statusBarHeight, widthHeight[0], widthHeight[1] - statusBarHeight);
        // 释放绘图资源所使用的缓存
        view.destroyDrawingCache();
        return bitmap;
    } catch (Exception e) {
        LogPrintUtils.eTag(TAG, e, "snapshotWithoutStatusBar");
    }
    return null;
}
 
Example 12
Source File: EWListView.java    From ExtractWordView with MIT License 5 votes vote down vote up
/**
     * @param activity
     * @param x        截图起始的横坐标
     * @param y        截图起始的纵坐标
     * @param width
     * @param height
     * @return
     */
    private Bitmap getBitmap(Activity activity, int x, int y, int width, int height) {
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        bitmap = view.getDrawingCache();
        //边界处理,否则会崩滴
        if (x < 0)
            x = 0;
        if (y < 0)
            y = 0;
        if (x + width > bitmap.getWidth()) {
//            x = x + WIDTH / 2;
//            width = bitmap.getWidth() - x;
            //保持不改变,截取图片宽高的原则
            x = bitmap.getWidth() - width;
        }
        if (y + height > bitmap.getHeight()) {
//            y = y + HEIGHT / 2;
//            height = bitmap.getHeight() - y;
            y = bitmap.getHeight() - height;
        }
        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int toHeight = frame.top;
        bitmap = Bitmap.createBitmap(bitmap, x, y, width, height);
        view.setDrawingCacheEnabled(false);
        return bitmap;
    }
 
Example 13
Source File: FileUtils.java    From letv with Apache License 2.0 5 votes vote down vote up
private Bitmap takeScreenShot(Activity context) {
    View view = context.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap screenBitmap = view.getDrawingCache();
    view.destroyDrawingCache();
    view.setDrawingCacheEnabled(false);
    return screenBitmap;
}
 
Example 14
Source File: ScreenUtils.java    From Gizwits-SmartSocket_Android with MIT License 5 votes vote down vote up
/**
* 获取屏幕截图,包含状态栏
* 
* @param activity
*            activity
* @return   屏幕截图
* 
* */
  public static Bitmap snapShotWithStatusBar(Activity activity)
  {
      View view = activity.getWindow().getDecorView();
      view.setDrawingCacheEnabled(true);
      view.buildDrawingCache();
      Bitmap bmp = view.getDrawingCache();
      int width = getScreenWidth(activity);
      int height = getScreenHeight(activity);
      Bitmap bp = null;
      bp = Bitmap.createBitmap(bmp, 0, 0, width, height);
      view.destroyDrawingCache();
      return bp;
 
  }
 
Example 15
Source File: SlidingDrawer.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private void prepareContent() {
    if (mAnimating) {
        return;
    }

    // Something changed in the content, we need to honor the layout request
    // before creating the cached bitmap
    final View content = mContent;
    if (content.isLayoutRequested()) {
        if (mVertical) {
            final int childHeight = mHandleHeight;
            int height = mBottom - mTop - childHeight - mTopOffset;
            content.measure(MeasureSpec.makeMeasureSpec(mRight - mLeft, MeasureSpec.EXACTLY),
                    MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
            content.layout(0, mTopOffset + childHeight, content.getMeasuredWidth(),
                    mTopOffset + childHeight + content.getMeasuredHeight());
        } else {
            final int childWidth = mHandle.getWidth();
            int width = mRight - mLeft - childWidth - mTopOffset;
            content.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
                    MeasureSpec.makeMeasureSpec(mBottom - mTop, MeasureSpec.EXACTLY));
            content.layout(childWidth + mTopOffset, 0,
                    mTopOffset + childWidth + content.getMeasuredWidth(),
                    content.getMeasuredHeight());
        }
    }
    // Try only once... we should really loop but it's not a big deal
    // if the draw was cancelled, it will only be temporary anyway
    content.getViewTreeObserver().dispatchOnPreDraw();
    if (!content.isHardwareAccelerated()) content.buildDrawingCache();

    content.setVisibility(View.GONE);        
}
 
Example 16
Source File: ScreenUtils.java    From Android_UE with Apache License 2.0 5 votes vote down vote up
/**
 * 获取当前屏幕截图,包含状态栏
 *
 * @param activity
 * @return
 */
public static Bitmap snapShotWithStatusBar(Activity activity) {
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap bmp = view.getDrawingCache(true);
    int width = getScreenWidth(activity);
    int height = getScreenHeight(activity);
    Bitmap bp = null;
    bp = Bitmap.createBitmap(bmp, 0, 0, width, height);
    view.destroyDrawingCache();
    return bp;

}
 
Example 17
Source File: ScreenShott.java    From screenshott with Apache License 2.0 5 votes vote down vote up
/**
 * Take screen shot of the View with spaces as per constraints
 *
 * @param v
 *     the v
 * @return the bitmap
 */
public Bitmap takeScreenShotOfView(View v) {
  v.setDrawingCacheEnabled(true);
  v.buildDrawingCache(true);

  // creates immutable clone
  Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
  v.setDrawingCacheEnabled(false); // clear drawing cache
  return b;
}
 
Example 18
Source File: ConvertUtils.java    From a 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 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: FoldingLayout.java    From Android-FoldingLayout with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * returns a new Bitmap with a clipped vertical area of the original view cache
 *
 * @param view   The view to clip
 * @param top    Top Y coordinate where to start to clip
 * @param bottom Lower Y coordinate where to end the clip
 * @return A new Bitmap with the clipped area
 */
private Bitmap clipBitmap(View view, int top, int bottom) {
    view.buildDrawingCache(false);

    Bitmap fullImage = view.getDrawingCache();

    Bitmap clipView = Bitmap.createBitmap(fullImage.getWidth(), fullImage.getHeight() / 2, Bitmap.Config.ARGB_8888);

    Canvas c = new Canvas(clipView);

    Rect source = new Rect(0, top, fullImage.getWidth(), bottom);
    Rect target = new Rect(0, 0, clipView.getWidth(), clipView.getHeight());

    c.drawBitmap(fullImage, source, target, null);
    view.destroyDrawingCache();

    return clipView;
}