下面列出了android.graphics.drawable.Drawable#copyBounds() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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);
}
}
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);
}
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);
}
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);
}
/**
* @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);
}
/**
* 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;
}
@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;
}
/**
* 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;
}
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;
}
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;
}
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;
}
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);
}
}
@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));
}
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;
}
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);
}
}
/**
* 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();
}
@Override
public void set(Drawable object, PointF value) {
object.copyBounds(mBounds);
mBounds.offsetTo(Math.round(value.x), Math.round(value.y));
object.setBounds(mBounds);
}
@Override
public PointF get(Drawable object) {
object.copyBounds(mBounds);
return new PointF(mBounds.left, mBounds.top);
}
@Override
public void set(Drawable object, PointF value) {
object.copyBounds(mBounds);
mBounds.offsetTo(Math.round(value.x), Math.round(value.y));
object.setBounds(mBounds);
}
@Override
public PointF get(Drawable object) {
object.copyBounds(mBounds);
return new PointF(mBounds.left, mBounds.top);
}