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

The following examples show how to use android.view.View#draw() . 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: UltimateAndroid   File: GrabIt.java    License: Apache License 2.0 6 votes vote down vote up
public static Bitmap takeScreenshot(View view, Bitmap.Config config) {
  int width = view.getWidth();
  int height = view.getHeight();

  if (view != null && width > 0 && height > 0) {
    Bitmap bitmap = Bitmap.createBitmap(width, height, config);
    Canvas canvas = new Canvas(bitmap);
    view.draw(canvas);

    //canvas.drawColor(Color.RED, PorterDuff.Mode.DARKEN); //NOTES: debug option

    if (AphidLog.ENABLE_DEBUG) {
      AphidLog.d("create bitmap %dx%d, format %s", width, height, config);
    }

    return bitmap;
  } else {
    return null;
  }
}
 
Example 2
Source Project: TidyDemo   File: BlurUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static Bitmap drawViewToBitmap(Bitmap dest, View view, int width, int height, int downSampling, Drawable drawable) {
	float scale = 1f / downSampling;
	int heightCopy = view.getHeight();
	view.layout(0, 0, width, height);
	int bmpWidth = (int) (width * scale);
	int bmpHeight = (int) (height * scale);
	if (dest == null || dest.getWidth() != bmpWidth || dest.getHeight() != bmpHeight) {
		dest = Bitmap.createBitmap(bmpWidth, bmpHeight, Bitmap.Config.ARGB_8888);
	}
	Canvas c = new Canvas(dest);
	drawable.setBounds(new Rect(0, 0, width, height));
	drawable.draw(c);
	if (downSampling > 1) {
		c.scale(scale, scale);
	}
	view.draw(c);
	view.layout(0, 0, width, heightCopy);
	// saveToSdCard(original, "original.png");
	return dest;
}
 
Example 3
/**
 * Draws a header to a canvas, offsetting by some x and y amount
 *
 * @param recyclerView the parent recycler view for drawing the header into
 * @param canvas       the canvas on which to draw the header
 * @param header       the view to draw as the header
 * @param offset       a Rect used to define the x/y offset of the header. Specify x/y offset by setting
 *                     the {@link Rect#left} and {@link Rect#top} properties, respectively.
 */
public void drawHeader(RecyclerView recyclerView, Canvas canvas, View header, Rect offset) {
  canvas.save();

  if (recyclerView.getLayoutManager().getClipToPadding()) {
    // Clip drawing of headers to the padding of the RecyclerView. Avoids drawing in the padding
    initClipRectForHeader(mTempRect, recyclerView, header);
    canvas.clipRect(mTempRect);
  }

  canvas.translate(offset.left, offset.top);

  header.draw(canvas);
  canvas.restore();
}
 
Example 4
private BitmapDrawable createFloatingBitmap(View v) {
    floatingItemStatingBounds = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
    floatingItemBounds = new Rect(floatingItemStatingBounds);

    Bitmap bitmap = Bitmap.createBitmap(floatingItemStatingBounds.width(),
            floatingItemStatingBounds.height(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    v.draw(canvas);

    BitmapDrawable retDrawable = new BitmapDrawable(v.getResources(), bitmap);
    retDrawable.setBounds(floatingItemBounds);

    return retDrawable;
}
 
Example 5
Source Project: UltimateAndroid   File: ViewUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * change View to Bitmap Object
 * 
 * @param v
 * @return
 */
public static Bitmap loadBitmapFromView(View v) {
    if (v == null) {
        return null;
    }
    Bitmap screenshot;
    screenshot = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Config.ARGB_8888);
    Canvas c = new Canvas(screenshot);
    c.translate(-v.getScrollX(), -v.getScrollY());
    v.draw(c);
    return screenshot;
}
 
Example 6
Source Project: materialup   File: PostActivity.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Parcelable onCaptureSharedElementSnapshot(View sharedElement,
                                                 Matrix viewToGlobalMatrix,
                                                 RectF screenBounds) {
    // store a snapshot of the fab to fade out when morphing to the login dialog
    int bitmapWidth = Math.round(screenBounds.width());
    int bitmapHeight = Math.round(screenBounds.height());
    Bitmap bitmap = null;
    if (bitmapWidth > 0 && bitmapHeight > 0) {
        bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888);
        sharedElement.draw(new Canvas(bitmap));
    }
    return bitmap;
}
 
Example 7
public void onDrawFocusView(Canvas canvas) {
	View view = mFocusView;
	canvas.save();
	float scaleX = (float) (getMainUpView().getWidth()) / (float) view.getWidth();
	float scaleY = (float) (getMainUpView().getHeight()) / (float) view.getHeight();
	canvas.scale(scaleX, scaleY);
	view.draw(canvas);
	canvas.restore();
}
 
Example 8
private void captureValues(@NonNull TransitionValues transitionValues) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        return;
    }
    View view = transitionValues.view;
    if (view.getWidth() <= 0 || view.getHeight() <= 0) {
        return;
    }
    Rect bounds = new Rect(0, 0, view.getWidth(), view.getHeight());
    if (mFadeBehavior != FADE_BEHAVIOR_REVEAL) {
        bounds.offset(view.getLeft(), view.getTop());
    }
    transitionValues.values.put(PROPNAME_BOUNDS, bounds);

    if (Transition.DBG) {
        Log.d(LOG_TAG, "Captured bounds " + transitionValues.values.get(PROPNAME_BOUNDS));
    }
    Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),
            Bitmap.Config.ARGB_8888);
    if (view instanceof TextureView) {
        bitmap = ((TextureView) view).getBitmap();
    } else {
        Canvas c = new Canvas(bitmap);
        view.draw(c);
    }
    transitionValues.values.put(PROPNAME_BITMAP, bitmap);
    BitmapDrawable drawable = new BitmapDrawable(view.getResources(), bitmap);
    // TODO: lrtb will be wrong if the view has transXY set
    drawable.setBounds(bounds);
    transitionValues.values.put(PROPNAME_DRAWABLE, drawable);
}
 
Example 9
private Bitmap loadBitmapFromView(View mView) {
    Bitmap b = Bitmap.createBitmap(
            mView.getWidth(),
            mView.getHeight(),
            Bitmap.Config.ARGB_8888);

    Canvas c = new Canvas(b);

    // With the following, screen blinks
    //v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);

    mView.draw(c);

    return b;
}
 
Example 10
Source Project: DevUtils   File: CapturePictureUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 通过 View 绘制为 Bitmap
 * @param view   {@link View}
 * @param config {@link Bitmap.Config}
 * @return {@link Bitmap}
 */
public static Bitmap snapshotByView(final View view, final Bitmap.Config config) {
    if (view == null || config == null) return null;
    try {
        Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), config);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawColor(BACKGROUND_COLOR);
        view.layout(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
        view.draw(canvas);
        return bitmap;
    } catch (Exception e) {
        LogPrintUtils.eTag(TAG, e, "snapshotByView");
    }
    return null;
}
 
Example 11
private int getColorOfAPixel() {
    View view = runOnUiThreadBlocking(() -> mActivity.findViewById(android.R.id.content));
    Bitmap bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    view.draw(canvas);
    return bitmap.getPixel(0, 0);
}
 
Example 12
private BitmapDrawable createFloatingBitmap(View v) {
    floatingItemStatingBounds = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
    floatingItemBounds = new Rect(floatingItemStatingBounds);

    Bitmap bitmap = Bitmap.createBitmap(floatingItemStatingBounds.width(),
            floatingItemStatingBounds.height(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    v.draw(canvas);

    BitmapDrawable retDrawable = new BitmapDrawable(v.getResources(), bitmap);
    retDrawable.setBounds(floatingItemBounds);

    return retDrawable;
}
 
Example 13
Source Project: BoardView   File: BoardView.java    License: Apache License 2.0 5 votes vote down vote up
private Bitmap getBitmapFromView(View v, float scale){
    double radians = 0.0523599f;
    double s = Math.abs(Math.sin(radians));
    double c = Math.abs(Math.cos(radians));
    int width = (int)(v.getHeight()*s + v.getWidth()*c);
    int height = (int)(v.getWidth()*s + v.getHeight()*c);
    Bitmap bitmap = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    canvas.scale(scale,scale);
    v.draw(canvas);
    return bitmap;
}
 
Example 14
/**
 * Draws a header to a canvas, offsetting by some x and y amount
 *
 * @param recyclerView the parent recycler view for drawing the header into
 * @param canvas       the canvas on which to draw the header
 * @param header       the view to draw as the header
 * @param offset       a Rect used to define the x/y offset of the header. Specify x/y offset by setting
 *                     the {@link Rect#left} and {@link Rect#top} properties, respectively.
 */
public void drawHeader(@NonNull RecyclerView recyclerView, @NonNull Canvas canvas, @NonNull View header, @NonNull Rect offset) {
    canvas.save();

    if (recyclerView.getLayoutManager().getClipToPadding()) {
        // Clip drawing of headers to the padding of the RecyclerView. Avoids drawing in the padding
        initClipRectForHeader(mTempRect, recyclerView, header);
        canvas.clipRect(mTempRect);
    }

    canvas.translate(offset.left, offset.top);

    header.draw(canvas);
    canvas.restore();
}
 
Example 15
public static Bitmap loadBitmapFromView(View view) {
  if (view.getMeasuredHeight() <= 0) {
    view.measure(CoordinatorLayout.LayoutParams.WRAP_CONTENT, CoordinatorLayout.LayoutParams.WRAP_CONTENT);
    Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    view.layout(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
    view.draw(canvas);
    return bitmap;
  }
  return null;
}
 
Example 16
Source Project: BlurPopupWindow   File: BlurPopupWindow.java    License: Apache License 2.0 5 votes vote down vote up
BlurTask(View sourceView, int statusBarHeight, int navigationBarheight, BlurPopupWindow popupWindow, BlurTaskCallback blurTaskCallback) {
    mContextRef = new WeakReference<>(sourceView.getContext());
    mPopupWindowRef = new WeakReference<>(popupWindow);
    mBlurTaskCallback = blurTaskCallback;

    int height = sourceView.getHeight() - statusBarHeight - navigationBarheight;
    if (height < 0) {
        height = sourceView.getHeight();
    }

    Drawable background = sourceView.getBackground();
    mSourceBitmap = Bitmap.createBitmap(sourceView.getWidth(), height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(mSourceBitmap);
    int saveCount = 0;
    if (statusBarHeight != 0) {
        saveCount = canvas.save();
        canvas.translate(0, -statusBarHeight);
    }
    if (popupWindow.getBlurRadius() > 0) {
        if (background == null) {
            canvas.drawColor(0xffffffff);
        }
        sourceView.draw(canvas);
    }
    if (popupWindow.getTintColor() != 0) {
        canvas.drawColor(popupWindow.getTintColor());
    }
    if (statusBarHeight != 0 && saveCount != 0) {
        canvas.restoreToCount(saveCount);
    }
}
 
Example 17
/**
 * Draw the View v into the given Canvas.
 *
 * @param v the view to draw
 * @param destCanvas the canvas to draw on
 * @param padding the horizontal and vertical padding to use when drawing
 */
private static void drawDragView(View v, Canvas destCanvas, int padding) {
    final Rect clipRect = sTempRect;
    v.getDrawingRect(clipRect);

    boolean textVisible = false;

    destCanvas.save();
    if (v instanceof TextView) {
        Drawable d = getTextViewIcon((TextView) v);
        Rect bounds = getDrawableBounds(d);
        clipRect.set(0, 0, bounds.width() + padding, bounds.height() + padding);
        destCanvas.translate(padding / 2 - bounds.left, padding / 2 - bounds.top);
        d.draw(destCanvas);
    } else {
        if (v instanceof FolderIcon) {
            // For FolderIcons the text can bleed into the icon area, and so we need to
            // hide the text completely (which can't be achieved by clipping).
            if (((FolderIcon) v).getTextVisible()) {
                ((FolderIcon) v).setTextVisible(false);
                textVisible = true;
            }
        }
        destCanvas.translate(-v.getScrollX() + padding / 2, -v.getScrollY() + padding / 2);
        destCanvas.clipRect(clipRect, Op.REPLACE);
        v.draw(destCanvas);

        // Restore text visibility of FolderIcon if necessary
        if (textVisible) {
            ((FolderIcon) v).setTextVisible(true);
        }
    }
    destCanvas.restore();
}
 
Example 18
Source Project: PTVGlass   File: ChronometerDrawer.java    License: MIT License 5 votes vote down vote up
/**
 * Draws the view in the SurfaceHolder's canvas.
 */
private void draw(View view) {
    Canvas canvas;
    try {
        canvas = mHolder.lockCanvas();
    } catch (Exception e) {
        Log.e(TAG, "Unable to lock canvas: " + e);
        return;
    }
    if (canvas != null) {
        view.draw(canvas);
        mHolder.unlockCanvasAndPost(canvas);
    }
}
 
Example 19
Source Project: RichEditor   File: ViewUtil.java    License: MIT License 5 votes vote down vote up
/**
     * 通过canvas将view转化为bitmap
     * @param view 指定view
     * @return bitmap
     */
    public static Bitmap getViewBitmapByCanvas(View view) {
        if (view == null) {
            return null;
        }
        Bitmap bmp = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bmp);

        // 如果不设置canvas画布为白色,则生成透明
//        c.drawColor(Color.WHITE);
//        view.layout(0, 0, w, h);

        view.draw(canvas);
        return bmp;
    }
 
Example 20
Source Project: SmartOrnament   File: HintPopupWindow.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 得到bitmap位图, 传入View对象
 */
public static Bitmap getBitmapByView(View view) {

    Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    view.draw(new Canvas(bitmap));
    return bitmap;
}