Java Code Examples for android.graphics.drawable.Drawable#copyBounds()

The following examples show how to use android.graphics.drawable.Drawable#copyBounds() . 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: ItemizedOverlay.java    From osmdroid with Apache License 2.0 6 votes vote down vote up
/**
 * @since 6.0.2
 */
protected boolean isEventOnItem(final Item pItem, final int pEventX, final int pEventY, final MapView pMapView) {
	if (pItem == null) {
		return false;
	}
	pMapView.getProjection().toPixels(pItem.getPoint(), mCurScreenCoords);
	final int state = (mDrawFocusedItem && (mFocusedItem == pItem) ? OverlayItem.ITEM_STATE_FOCUSED_MASK : 0);
	Drawable marker = pItem.getMarker(state);
	if (marker == null) {
		marker = getDefaultMarker(state);
	}
	boundToHotspot(marker, pItem.getMarkerHotspot());
	marker.copyBounds(mRect);
	mRect.offset(mCurScreenCoords.x, mCurScreenCoords.y);
	RectL.getBounds(mRect, mCurScreenCoords.x, mCurScreenCoords.y, -pMapView.getMapOrientation(), mOrientedMarkerRect);
	return mOrientedMarkerRect.contains(pEventX, pEventY);
}
 
Example 2
Source File: AbsSeekBar.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@Override
void drawTrack(Canvas canvas) {
    final Drawable thumbDrawable = mThumb;
    if (thumbDrawable != null && mSplitTrack) {
        final Insets insets = thumbDrawable.getOpticalInsets();
        final Rect tempRect = mTempRect;
        thumbDrawable.copyBounds(tempRect);
        tempRect.offset(mPaddingLeft - mThumbOffset, mPaddingTop);
        tempRect.left += insets.left;
        tempRect.right -= insets.right;

        final int saveCount = canvas.save();
        canvas.clipRect(tempRect, Op.DIFFERENCE);
        super.drawTrack(canvas);
        drawTickMarks(canvas);
        canvas.restoreToCount(saveCount);
    } else {
        super.drawTrack(canvas);
        drawTickMarks(canvas);
    }
}
 
Example 3
Source File: Utils.java    From StockChart-MPAndroidChart with MIT License 6 votes vote down vote up
public static void drawImage(Canvas canvas,
                             Drawable drawable,
                             int x, int y,
                             int width, int height) {

    MPPointF drawOffset = MPPointF.getInstance();
    drawOffset.x = x - (width / 2);
    drawOffset.y = y - (height / 2);

    drawable.copyBounds(mDrawableBoundsCache);
    drawable.setBounds(
            mDrawableBoundsCache.left,
            mDrawableBoundsCache.top,
            mDrawableBoundsCache.left + width,
            mDrawableBoundsCache.top + width);

    int saveId = canvas.save();
    // translate to the correct position and draw
    canvas.translate(drawOffset.x, drawOffset.y);
    drawable.draw(canvas);
    canvas.restoreToCount(saveId);
}
 
Example 4
Source File: Utils.java    From Ticket-Analysis with MIT License 6 votes vote down vote up
public static void drawImage(Canvas canvas,
                             Drawable drawable,
                             int x, int y,
                             int width, int height) {

    MPPointF drawOffset = MPPointF.getInstance();
    drawOffset.x = x - (width / 2);
    drawOffset.y = y - (height / 2);

    drawable.copyBounds(mDrawableBoundsCache);
    drawable.setBounds(
            mDrawableBoundsCache.left,
            mDrawableBoundsCache.top,
            mDrawableBoundsCache.left + width,
            mDrawableBoundsCache.top + width);

    int saveId = canvas.save();
    // translate to the correct position and draw
    canvas.translate(drawOffset.x, drawOffset.y);
    drawable.draw(canvas);
    canvas.restoreToCount(saveId);
}
 
Example 5
Source File: Utils.java    From android-kline with Apache License 2.0 6 votes vote down vote up
public static void drawImage(Canvas canvas,
                             Drawable drawable,
                             int x, int y,
                             int width, int height) {

    MPPointF drawOffset = MPPointF.getInstance();
    drawOffset.x = x - (width / 2);
    drawOffset.y = y - (height / 2);

    drawable.copyBounds(mDrawableBoundsCache);
    drawable.setBounds(
            mDrawableBoundsCache.left,
            mDrawableBoundsCache.top,
            mDrawableBoundsCache.left + width,
            mDrawableBoundsCache.top + width);

    int saveId = canvas.save();
    // translate to the correct position and draw
    canvas.translate(drawOffset.x, drawOffset.y);
    drawable.draw(canvas);
    canvas.restoreToCount(saveId);
}
 
Example 6
Source File: Overlay.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
/**
 * Convenience method to draw a Drawable at an offset. x and y are pixel coordinates. You can
 * find appropriate coordinates from latitude/longitude using the MapView.getProjection() method
 * on the MapView passed to you in draw(Canvas, MapView, boolean).
 *
 * @param shadow
 *            If true, draw only the drawable's shadow. Otherwise, draw the drawable itself.
 * @param aMapOrientation
 */
protected synchronized static void drawAt(final Canvas canvas, final Drawable drawable,
										  final int x, final int y, final boolean shadow,
										  final float aMapOrientation) {
	canvas.save();
	canvas.rotate(-aMapOrientation, x, y);
	drawable.copyBounds(mRect);
	drawable.setBounds(mRect.left + x, mRect.top + y, mRect.right + x, mRect.bottom + y);
	drawable.draw(canvas);
	drawable.setBounds(mRect);
	canvas.restore();
}
 
Example 7
Source File: WidgetPreviewLoader.java    From LB-Launcher with Apache License 2.0 5 votes vote down vote up
private static void renderDrawableToBitmap(
        Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
    if (bitmap != null) {
        Canvas c = new Canvas(bitmap);
        Rect oldBounds = d.copyBounds();
        d.setBounds(x, y, x + w, y + h);
        d.draw(c);
        d.setBounds(oldBounds); // Restore the bounds
        c.setBitmap(null);
    }
}
 
Example 8
Source File: Utils.java    From scissors with Apache License 2.0 5 votes vote down vote up
public static Bitmap asBitmap(Drawable drawable, int minWidth, int minHeight) {
    final Rect tmpRect = new Rect();
    drawable.copyBounds(tmpRect);
    if (tmpRect.isEmpty()) {
        tmpRect.set(0, 0, Math.max(minWidth, drawable.getIntrinsicWidth()), Math.max(minHeight, drawable.getIntrinsicHeight()));
        drawable.setBounds(tmpRect);
    }
    Bitmap bitmap = Bitmap.createBitmap(tmpRect.width(), tmpRect.height(), Bitmap.Config.ARGB_8888);
    drawable.draw(new Canvas(bitmap));
    return bitmap;
}
 
Example 9
Source File: ColorDialog.java    From TabletClock with MIT License 5 votes vote down vote up
@Override
public void onClick(View v) {
	Drawable d = v.getBackground();
	Bitmap bmp;
	Canvas c = new Canvas(bmp = Bitmap.createBitmap(1, 1,
			Bitmap.Config.ARGB_8888));
	Rect oldRect = d.copyBounds();
	d.setBounds(0, 0, 1, 1);
	d.draw(c);
	d.setBounds(oldRect);

	setSeekColor(bmp.getPixel(0, 0));
}
 
Example 10
Source File: WidgetPreviewLoader.java    From TurboLauncher with Apache License 2.0 5 votes vote down vote up
private static void renderDrawableToBitmap(
        Drawable d, Bitmap bitmap, int x, int y, int w, int h,
        float scale) {
    if (bitmap != null) {
        Canvas c = new Canvas(bitmap);
        c.scale(scale, scale);
        Rect oldBounds = d.copyBounds();
        d.setBounds(x, y, x + w, y + h);
        d.draw(c);
        d.setBounds(oldBounds); // Restore the bounds
        c.setBitmap(null);
    }
}
 
Example 11
Source File: CropView.java    From AlbumSelector with Apache License 2.0 5 votes vote down vote up
public Bitmap asBitmap(Drawable drawable, int minWidth, int minHeight) {
    final Rect tmpRect = new Rect();
    drawable.copyBounds(tmpRect);
    if (tmpRect.isEmpty()) {
        tmpRect.set(0, 0, Math.max(minWidth, drawable.getIntrinsicWidth()), Math.max(minHeight, drawable.getIntrinsicHeight()));
        drawable.setBounds(tmpRect);
    }
    Bitmap bitmap = Bitmap.createBitmap(tmpRect.width(), tmpRect.height(), Bitmap.Config.ARGB_8888);
    drawable.draw(new Canvas(bitmap));
    return bitmap;
}
 
Example 12
Source File: Workspace.java    From Trebuchet with GNU General Public License v3.0 5 votes vote down vote up
private static Rect getDrawableBounds(Drawable d) {
    Rect bounds = new Rect();
    d.copyBounds(bounds);
    if (bounds.width() == 0 || bounds.height() == 0) {
        bounds.set(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
    } else {
        bounds.offsetTo(0, 0);
    }
    if (d instanceof PreloadIconDrawable) {
        int inset = -((PreloadIconDrawable) d).getOutset();
        bounds.inset(inset, inset);
    }
    return bounds;
}
 
Example 13
Source File: DragPreviewProvider.java    From LaunchEnr with GNU General Public License v3.0 5 votes vote down vote up
protected static Rect getDrawableBounds(Drawable d) {
    Rect bounds = new Rect();
    d.copyBounds(bounds);
    if (bounds.width() == 0 || bounds.height() == 0) {
        bounds.set(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
    } else {
        bounds.offsetTo(0, 0);
    }
    return bounds;
}
 
Example 14
Source File: ItemizedOverlay.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
/**
 * Draws an item located at the provided screen coordinates to the canvas.
 *
 * @param canvas
 *            what the item is drawn upon
 * @param item
 *            the item to be drawn
 * @param curScreenCoords
 * @param pProjection
 * @return true if the item was actually drawn
 */
protected boolean onDrawItem(final Canvas canvas, final Item item, final Point curScreenCoords,
		final Projection pProjection) {

	

	final int state = (mDrawFocusedItem && (mFocusedItem == item) ? OverlayItem.ITEM_STATE_FOCUSED_MASK
			: 0);
	final Drawable marker = (item.getMarker(state) == null) ? getDefaultMarker(state) : item
			.getMarker(state);
	final HotspotPlace hotspot = item.getMarkerHotspot();

	boundToHotspot(marker, hotspot);

	int x = mCurScreenCoords.x;
	int y = mCurScreenCoords.y;

	marker.copyBounds(mRect);
	mMarkerRect.set(mRect);
	mRect.offset(x, y);
	RectL.getBounds(mRect, x, y, pProjection.getOrientation(), mOrientedMarkerRect);
	final boolean displayed = Rect.intersects(mOrientedMarkerRect, canvas.getClipBounds());
	if (displayed) {
		if (pProjection.getOrientation() != 0) { // optimization: step 1/2
			canvas.save();
			canvas.rotate(-pProjection.getOrientation(), x, y);
		}
		marker.setBounds(mRect);
		marker.draw(canvas);
		if (pProjection.getOrientation() != 0) { // optimization: step 2/2
			canvas.restore();
		}
		marker.setBounds(mMarkerRect);
	}

	return displayed;
}
 
Example 15
Source File: RotationMarker.java    From WhereYouGo with GNU General Public License v3.0 5 votes vote down vote up
@Override
public synchronized boolean draw(BoundingBox boundingBox, byte zoomLevel, Canvas canvas,
                                 Point canvasPosition) {
    GeoPoint geoPoint = this.getGeoPoint();
    Drawable drawable = this.getDrawable();
    if (geoPoint == null || drawable == null) {
        return false;
    }

    double latitude = geoPoint.latitude;
    double longitude = geoPoint.longitude;
    int pixelX =
            (int) (MercatorProjection.longitudeToPixelX(longitude, zoomLevel) - canvasPosition.x);
    int pixelY =
            (int) (MercatorProjection.latitudeToPixelY(latitude, zoomLevel) - canvasPosition.y);

    Rect drawableBounds = drawable.copyBounds();
    int left = pixelX + drawableBounds.left;
    int top = pixelY + drawableBounds.top;
    int right = pixelX + drawableBounds.right;
    int bottom = pixelY + drawableBounds.bottom;

    if (!intersect(canvas, left, top, right, bottom)) {
        return false;
    }

    int saveCount = canvas.save();
    canvas.rotate(rotation, (float) pixelX, (float) pixelY);
    drawable.setBounds(left, top, right, bottom);
    drawable.draw(canvas);
    drawable.setBounds(drawableBounds);
    canvas.restoreToCount(saveCount);
    return true;
}
 
Example 16
Source File: WallpaperColors.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs {@link WallpaperColors} from a drawable.
 * <p>
 * Main colors will be extracted from the drawable.
 *
 * @param drawable Source where to extract from.
 */
public static WallpaperColors fromDrawable(Drawable drawable) {
    if (drawable == null) {
        throw new IllegalArgumentException("Drawable cannot be null");
    }

    Rect initialBounds = drawable.copyBounds();
    int width = drawable.getIntrinsicWidth();
    int height = drawable.getIntrinsicHeight();

    // Some drawables do not have intrinsic dimensions
    if (width <= 0 || height <= 0) {
        width = MAX_BITMAP_SIZE;
        height = MAX_BITMAP_SIZE;
    }

    Size optimalSize = calculateOptimalSize(width, height);
    Bitmap bitmap = Bitmap.createBitmap(optimalSize.getWidth(), optimalSize.getHeight(),
            Bitmap.Config.ARGB_8888);
    final Canvas bmpCanvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
    drawable.draw(bmpCanvas);

    final WallpaperColors colors = WallpaperColors.fromBitmap(bitmap);
    bitmap.recycle();

    drawable.setBounds(initialBounds);
    return colors;
}
 
Example 17
Source File: ChangeBounds.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@Override
public PointF get(Drawable object) {
    object.copyBounds(mBounds);
    return new PointF(mBounds.left, mBounds.top);
}
 
Example 18
Source File: ChangeBounds.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@Override
public void set(Drawable object, PointF value) {
    object.copyBounds(mBounds);
    mBounds.offsetTo(Math.round(value.x), Math.round(value.y));
    object.setBounds(mBounds);
}
 
Example 19
Source File: AbsoluteChangeBounds.java    From scene with Apache License 2.0 4 votes vote down vote up
@Override
public PointF get(Drawable object) {
    object.copyBounds(mBounds);
    return new PointF(mBounds.left, mBounds.top);
}
 
Example 20
Source File: AbsoluteChangeBounds.java    From scene with Apache License 2.0 4 votes vote down vote up
@Override
public void set(Drawable object, PointF value) {
    object.copyBounds(mBounds);
    mBounds.offsetTo(Math.round(value.x), Math.round(value.y));
    object.setBounds(mBounds);
}