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

The following examples show how to use android.view.View#getDrawingCache() . These examples are extracted from open source projects. 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 Project: BaseProject   File: WindowUtil.java    License: MIT License 6 votes vote down vote up
/**
 * 获取当前屏幕截图,不包含状态栏
 */
public static Bitmap snapShotWithoutStatusBar(@NonNull 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 = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height - statusBarHeight);
    view.destroyDrawingCache();
    return bp;
}
 
Example 2
Source Project: NBAPlus   File: BitmapUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static Bitmap drawViewToBitmap(View view, int width, int height, float translateX,
                                      float translateY, int downSampling,String color) {
    float scale = 1f / downSampling;
    int bmpWidth = (int) (width * scale - translateX / downSampling);
    int bmpHeight = (int) (height * scale - translateY / downSampling);
    Bitmap dest = Bitmap.createBitmap(bmpWidth, bmpHeight, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(dest);
    canvas.translate(-translateX / downSampling, -translateY / downSampling);
    if (downSampling > 1) {
        canvas.scale(scale, scale);
    }
    Paint paint = new Paint();
    paint.setFlags(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG);
    PorterDuffColorFilter filter =
            new PorterDuffColorFilter(Color.parseColor(color),PorterDuff.Mode.SRC_ATOP);
    paint.setColorFilter(filter);
    view.buildDrawingCache();
    Bitmap cache = view.getDrawingCache();
    canvas.drawBitmap(cache, 0, 0, paint);
    cache.recycle();
    view.destroyDrawingCache();

    return dest;
}
 
Example 3
@NonNull
private BitmapDrawable convertViewToDrawable(@NonNull View view) {
    int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);

    view.measure(spec, spec);
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

    Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmap);
    canvas.translate(-view.getScrollX(), -view.getScrollY());
    view.draw(canvas);

    view.setDrawingCacheEnabled(true);

    Bitmap cacheBmp = view.getDrawingCache();
    Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888, true);

    view.destroyDrawingCache();
    return new BitmapDrawable(null, viewBmp);
}
 
Example 4
Source Project: Android-Commons   File: UI.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Generates a screenshot of the specified `View`
 *
 * @param view the `View` component
 * @return the screenshot
 */
public static Bitmap getViewScreenshot(final View view) {
	// set up the drawing cache
	view.setDrawingCacheEnabled(true);
	view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);

	// get the screenshot
	final Bitmap viewScreenshot = view.getDrawingCache(true);
	final Bitmap output = viewScreenshot.copy(viewScreenshot.getConfig(), false);

	// disable the drawing cache again
	view.destroyDrawingCache();
	view.setDrawingCacheEnabled(false);

	return output;
}
 
Example 5
private Bitmap getRootBitmap() {
    final View view = this.getWindow().getDecorView();
    Bitmap bitmap = view.getDrawingCache();

    int statusBarHeight = 0;
    // If need cut statusBar on sdk < KITKAT
    if (NEED_CUT) {
        Rect frame = new Rect();
        view.getWindowVisibleDisplayFrame(frame);
        statusBarHeight = frame.top;
    }

    // Src
    bitmap = Bitmap.createBitmap(bitmap, 0, statusBarHeight, bitmap.getWidth(),
            bitmap.getHeight() - statusBarHeight);
    view.destroyDrawingCache();

    return bitmap;
}
 
Example 6
@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    final LayoutParams lp = (LayoutParams) child.getLayoutParams();
    boolean result;
    final int save = canvas.save(Canvas.CLIP_SAVE_FLAG);

    if (mCanSlide && !lp.slideable && mSlideableView != null) {
        // Clip against the slider; no sense drawing what will immediately be covered.
        canvas.getClipBounds(mTmpRect);
        mTmpRect.right = Math.min(mTmpRect.right, mSlideableView.getLeft());
        canvas.clipRect(mTmpRect);
    }

    if (Build.VERSION.SDK_INT >= 11) { // HC
        result = super.drawChild(canvas, child, drawingTime);
    } else {
        if (lp.dimWhenOffset && mSlideOffset > 0) {
            if (!child.isDrawingCacheEnabled()) {
                child.setDrawingCacheEnabled(true);
            }
            final Bitmap cache = child.getDrawingCache();
            if (cache != null) {
                canvas.drawBitmap(cache, child.getLeft(), child.getTop(), lp.dimPaint);
                result = false;
            } else {
                Log.e(TAG, "drawChild: child view " + child + " returned null drawing cache");
                result = super.drawChild(canvas, child, drawingTime);
            }
        } else {
            if (child.isDrawingCacheEnabled()) {
                child.setDrawingCacheEnabled(false);
            }
            result = super.drawChild(canvas, child, drawingTime);
        }
    }

    canvas.restoreToCount(save);

    return result;
}
 
Example 7
private void a()
{
    View view = getView().findViewById(0x7f0a001e);
    view.buildDrawingCache();
    android.graphics.Bitmap bitmap = view.getDrawingCache();
    String s = (new StringBuilder()).append(Environment.getExternalStorageDirectory().getAbsolutePath()).append("/tmp.jpg").toString();
    Utils.saveBitmapToFile(s, bitmap);
    view.findViewById(0x7f0a001e).destroyDrawingCache();
    Intent intent = new Intent();
    intent.setClass(getActivity(), cn/com/smartdevices/bracelet/ui/ShareActivity);
    intent.putExtra("date", d.getText());
    intent.putExtra("Mode", 1);
    intent.putExtra("Image", s);
    startActivityForResult(intent, 32769);
}
 
Example 8
Source Project: Jide-Note   File: ScreenUtils.java    License: MIT License 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 9
Source Project: DragBoardView   File: DragHelper.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 抓起
 *
 * @param dragger  抓起的 View
 * @param position 抓起的 View 在 RecyclerView 的 position
 */
public void dragItem(View dragger, int position) {
    dragger.destroyDrawingCache();
    dragger.setDrawingCacheEnabled(true);
    Bitmap bitmap = dragger.getDrawingCache();
    if (bitmap != null && !bitmap.isRecycled()) {
        mDragImageView.setImageBitmap(bitmap);
        mDragImageView.setRotation(1.5f);
        mDragImageView.setAlpha(0.8f);

        isDraggingItem = true;
        if (dragger.getTag() instanceof DragItem) {
            dragItem = (DragItem) dragger.getTag();
        }
        int dragPage = mHorizontalRecyclerView.getCurrentPosition();
        RecyclerView.ViewHolder holder = (RecyclerView.ViewHolder) mHorizontalRecyclerView.findViewHolderForAdapterPosition(dragPage);
        if (holder != null && holder.itemView != null && holder.getItemViewType() == TYPE_CONTENT) {
            mCurrentVerticalRecycleView = ((DragHorizontalViewHolder) holder).getRecyclerView();

            mPagerPosition = dragPage;
        }

        getTargetHorizontalRecyclerViewScrollBoundaries();
        getTargetVerticalRecyclerViewScrollBoundaries();

        int[] location = new int[2];
        dragger.getLocationOnScreen(location);
        mWindowParams.x = location[0];
        mWindowParams.y = location[1];
        mBornLocationX = location[0];
        mBornLocationY = location[1];
        confirmOffset = false;
        mPosition = position;
        mWindowManager.addView(mDragImageView, mWindowParams);
        getCurrentVerticalAdapter().onDrag(position);
    }
}
 
Example 10
Source Project: YCAudioPlayer   File: ImageUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static Bitmap convertViewToBitMap(View view){
    // 打开图像缓存
    view.setDrawingCacheEnabled(true);
    // 必须调用measure和layout方法才能成功保存可视组件的截图到png图像文件
    // 测量View大小
    int i = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    int n = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    view.measure(i, n);
    // 发送位置和尺寸到View及其所有的子View
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
    // 获得可视组件的截图
    Bitmap bitmap = view.getDrawingCache();
    return bitmap;
}
 
Example 11
Source Project: TitleLayout   File: ScreenUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 获取当前屏幕截图,不包含状态栏
 *
 * @param activity activity
 * @return Bitmap
 */
public static Bitmap captureWithoutStatusBar(Activity activity) {
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap bmp = view.getDrawingCache();
    int statusBarHeight = getStatusBarHeight(activity);//获取状态栏高度
    DisplayMetrics dm = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    Bitmap ret = Bitmap.createBitmap(bmp, 0, statusBarHeight, dm.widthPixels, dm.heightPixels - statusBarHeight);
    view.destroyDrawingCache();
    return ret;
}
 
Example 12
Source Project: V.FlyoutTest   File: SlidingPaneLayout.java    License: MIT License 5 votes vote down vote up
@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    final LayoutParams lp = (LayoutParams) child.getLayoutParams();
    boolean result;
    final int save = canvas.save(Canvas.CLIP_SAVE_FLAG);

    if (mCanSlide && !lp.slideable && mSlideableView != null) {
        // Clip against the slider; no sense drawing what will immediately be covered.
        canvas.getClipBounds(mTmpRect);
        mTmpRect.right = Math.min(mTmpRect.right, mSlideableView.getLeft());
        canvas.clipRect(mTmpRect);
    }

    if (Build.VERSION.SDK_INT >= 11) { // HC
        result = super.drawChild(canvas, child, drawingTime);
    } else {
        if (lp.dimWhenOffset && mSlideOffset > 0) {
            if (!child.isDrawingCacheEnabled()) {
                child.setDrawingCacheEnabled(true);
            }
            final Bitmap cache = child.getDrawingCache();
            if (cache != null) {
                canvas.drawBitmap(cache, child.getLeft(), child.getTop(), lp.dimPaint);
                result = false;
            } else {
                Log.e(TAG, "drawChild: child view " + child + " returned null drawing cache");
                result = super.drawChild(canvas, child, drawingTime);
            }
        } else {
            if (child.isDrawingCacheEnabled()) {
                child.setDrawingCacheEnabled(false);
            }
            result = super.drawChild(canvas, child, drawingTime);
        }
    }

    canvas.restoreToCount(save);

    return result;
}
 
Example 13
public static Bitmap getActivityBitmap(Activity activity) {
        View view = activity.getWindow().getDecorView();
/*        Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        view.draw(canvas);*/
        view.buildDrawingCache();
        return view.getDrawingCache();
    }
 
Example 14
private Bitmap getChildDrawingCache(final View child) {
	Bitmap bitmap = child.getDrawingCache();
	if (bitmap == null) {
		child.setDrawingCacheEnabled(true);
		child.buildDrawingCache();
		bitmap = child.getDrawingCache();
	}
	return bitmap;
}
 
Example 15
Source Project: xDrip   File: JoH.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static Bitmap screenShot2(View view) {
    Log.d(TAG, "Screenshot2 called: " + view.getWidth() + "," + view.getHeight());
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache(true);
    final Bitmap bitmap = view.getDrawingCache(true);
    return bitmap;
}
 
Example 16
public void takeScreenShot() {
    View v1 = activity.getWindow().getDecorView().getRootView();
    v1.setDrawingCacheEnabled(true);
    Bitmap myBitmap = v1.getDrawingCache();
    String filePath = saveBitmap(myBitmap);
    v1.setDrawingCacheEnabled(false);
    if (filePath != null) {
        sendMail(filePath);
    }
}
 
Example 17
Source Project: RxTools-master   File: RxDeviceTool.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 获取当前屏幕截图,包含状态栏
 *
 * @param activity activity
 * @return Bitmap
 */
public static Bitmap captureWithStatusBar(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 ret = Bitmap.createBitmap(bmp, 0, 0, width, height);
    view.destroyDrawingCache();
    return ret;
}
 
Example 18
/**
 * 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 19
/**
 * 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;
}
 
Example 20
/**
 * Calls {@link View#getDrawingCache(boolean)}.
 *
 * @return A bitmap representing this view, or null if caching is disabled
 *         or {@link View#buildDrawingCache(boolean)} has not been called
 */
public static Bitmap getDrawingCache(View view, boolean autoScale) {
  return view.getDrawingCache(autoScale);
}