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

The following examples show how to use android.view.View#getDrawingCache() . 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: WindowUtil.java    From BaseProject with 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 File: BackgroundActivity.java    From UPMiss with GNU General Public License v3.0 6 votes vote down vote up
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 3
Source File: BitmapUtils.java    From NBAPlus with 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 4
Source File: NewMessageActivity.java    From toktok-android with GNU General Public License v3.0 6 votes vote down vote up
@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 5
Source File: UI.java    From Android-Commons with 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 6
Source File: SlidingPaneLayout.java    From V.FlyoutTest with 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 7
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 8
Source File: RxDeviceTool.java    From RxTools-master with 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 9
Source File: TakeScreenshotService.java    From GitJourney with Apache License 2.0 5 votes vote down vote up
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 10
Source File: JoH.java    From xDrip with 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 11
Source File: ListView3d.java    From codeexamples-android with Eclipse Public License 1.0 5 votes vote down vote up
private Bitmap getChildDrawingCache(final View child) {
	Bitmap bitmap = child.getDrawingCache();
	if (bitmap == null) {
		child.setDrawingCacheEnabled(true);
		child.buildDrawingCache();
		bitmap = child.getDrawingCache();
	}
	return bitmap;
}
 
Example 12
Source File: BaseActivity.java    From FriendBook with GNU General Public License v3.0 5 votes vote down vote up
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 13
Source File: SlidingPaneLayout.java    From CodenameOne with GNU General Public License v2.0 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 14
Source File: ScreenUtil.java    From TitleLayout with 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 15
Source File: ImageUtil.java    From YCAudioPlayer with 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 16
Source File: DragHelper.java    From DragBoardView with 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 17
Source File: ScreenUtils.java    From Jide-Note with 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 18
Source File: StatisticHostFragment.java    From MiBandDecompiled with Apache License 2.0 5 votes vote down vote up
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 19
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;
}
 
Example 20
Source File: DonutUtil.java    From appinventor-extensions with Apache License 2.0 2 votes vote down vote up
/**
 * 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);
}