本文整理匯總了Java中android.graphics.drawable.Drawable.draw方法的典型用法代碼示例。如果您正苦於以下問題:Java Drawable.draw方法的具體用法?Java Drawable.draw怎麽用?Java Drawable.draw使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類android.graphics.drawable.Drawable
的用法示例。
在下文中一共展示了Drawable.draw方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: drawableToBitmap
import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
/**
* Drawable轉Bitmap方法
*
* @param drawable
* @return
*/
public static Bitmap drawableToBitmap(Drawable drawable) {
Bitmap bitmap;
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
if (bitmapDrawable.getBitmap() != null) {
return bitmapDrawable.getBitmap();
}
}
if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
bitmap = Bitmap.createBitmap(1, 1,
Bitmap.Config.ARGB_8888);
} else {
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
示例2: draw
import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
@Override
public void draw(@NonNull final Canvas canvas, final CharSequence text,
final int start, final int end, final float x,
final int top, final int y, final int bottom, @NonNull final Paint paint) {
Drawable d = getCachedDrawable();
Rect rect = d.getBounds();
canvas.save();
final float fontHeight = paint.getFontMetrics().descent - paint.getFontMetrics().ascent;
int transY = bottom - rect.bottom;
if (rect.height() < fontHeight) { // this is the fucking code which I waste 3 days
if (mVerticalAlignment == ALIGN_BASELINE) {
transY -= paint.getFontMetricsInt().descent;
} else if (mVerticalAlignment == ALIGN_CENTER) {
transY -= (fontHeight - rect.height()) / 2;
} else if (mVerticalAlignment == ALIGN_TOP) {
transY -= fontHeight - rect.height();
}
}
canvas.translate(x, transY);
d.draw(canvas);
canvas.restore();
}
示例3: getBitmapFromDrawable
import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
private Bitmap getBitmapFromDrawable(Drawable drawable) {
if (drawable == null) {
return null;
}
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
}
try {
Bitmap bitmap;
if (drawable instanceof ColorDrawable) {
bitmap = Bitmap.createBitmap(COLORDRAWABLE_DIMENSION, COLORDRAWABLE_DIMENSION, BITMAP_CONFIG);
} else {
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), BITMAP_CONFIG);
}
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
} catch (OutOfMemoryError e) {
return null;
}
}
示例4: onDraw
import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
@Override
protected void onDraw(Canvas canvas) {
int canvasWidth = canvas.getWidth();
int canvasHeight = canvas.getHeight();
if (adapter != null && !adapter.isEmpty()) {
Target.Level current = target.getCurrentLevel();
if (current != null) {
for (int position = current.getPosition(); position < maxNumberOfLevels; position++) {
// draw always from the first drawable to the last
levelListDrawable.setLevel(position - current.getPosition());
Drawable drawable = levelListDrawable.getCurrent();
computeDrawingBounds(canvasWidth, canvasHeight, target.getLevelAt(position).getSizePercent());
drawable.setBounds(drawingBounds);
drawable.draw(canvas);
}
}
}
}
示例5: drawableToBitmap
import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
private Bitmap drawableToBitmap(Drawable drawable) {
Bitmap bitmap;
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
if (bitmapDrawable.getBitmap() != null) {
return bitmapDrawable.getBitmap();
}
}
if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
} else {
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
示例6: drawableToBitmap
import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
public static Bitmap drawableToBitmap(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
}
Bitmap bitmap;
int width = Math.max(drawable.getIntrinsicWidth(), 2);
int height = Math.max(drawable.getIntrinsicHeight(), 2);
try {
bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
} catch (Exception e) {
e.printStackTrace();
Log.w(TAG, "Failed to create bitmap from drawable!");
bitmap = null;
}
return bitmap;
}
示例7: onDraw
import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
@Override
protected void onDraw(Canvas canvas) {
final Drawable drawable = getDrawable();
if (drawable != null) {
canvas.save();
canvas.translate(getMeasuredWidth() / 2, getMeasuredHeight() / 2);
canvas.rotate(degrees);
final int intrinsicWidth = drawable.getIntrinsicWidth();
final int intrinsicHeight = drawable.getIntrinsicHeight();
drawable.setBounds(-intrinsicWidth / 2, -intrinsicHeight / 2, intrinsicWidth / 2, intrinsicHeight / 2);
drawable.draw(canvas);
canvas.restore();
}
degrees += 10;
if (isLoading) {
mChoreographer.postFrameCallback(this);
}
}
示例8: draw
import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
@Override
public void draw(Canvas c) {
super.draw(c);
final boolean isLayoutRtl = isLayoutRtlSupport();
Drawable shadowDrawable;
if (isLayoutRtl) {
shadowDrawable = mShadowDrawableRight;
} else {
shadowDrawable = mShadowDrawableLeft;
}
final View shadowView = getChildCount() > 1 ? getChildAt(1) : null;
if (shadowView == null || shadowDrawable == null) {
// No need to draw a shadow if we don't have one.
return;
}
final int top = shadowView.getTop();
final int bottom = shadowView.getBottom();
final int shadowWidth = shadowDrawable.getIntrinsicWidth();
final int left;
final int right;
if (isLayoutRtlSupport()) {
left = shadowView.getRight();
right = left + shadowWidth;
} else {
right = shadowView.getLeft();
left = right - shadowWidth;
}
shadowDrawable.setBounds(left, top, right, bottom);
shadowDrawable.draw(c);
}
示例9: getBitmapDraw
import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
private Bitmap getBitmapDraw(Drawable drawable, boolean isThumb) {
if (drawable == null) {
return null;
}
int width;
int height;
int maxRange = IndicatorUtils.dp2px(mContext, CUSTOM_DRAWABLE_MAX_LIMITED_WIDTH);
int intrinsicWidth = drawable.getIntrinsicWidth();
if (intrinsicWidth > maxRange) {
if (isThumb) {
width = p.mThumbSize;
} else {
width = p.mTickSize;
}
height = getHeightByRatio(drawable, width);
if (width > maxRange) {
width = maxRange;
height = getHeightByRatio(drawable, width);
}
} else {
width = drawable.getIntrinsicWidth();
height = drawable.getIntrinsicHeight();
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
示例10: onDrawKeyBackground
import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
protected void onDrawKeyBackground(@Nonnull final Key key, @Nonnull final Canvas canvas,
@Nonnull final Drawable background) {
final int keyWidth = key.getDrawWidth();
final int keyHeight = key.getHeight();
final int bgWidth, bgHeight, bgX, bgY;
if (key.needsToKeepBackgroundAspectRatio(mDefaultKeyLabelFlags)
// HACK: To disable expanding normal/functional key background.
&& !key.hasCustomActionLabel()) {
final int intrinsicWidth = background.getIntrinsicWidth();
final int intrinsicHeight = background.getIntrinsicHeight();
final float minScale = Math.min(
keyWidth / (float)intrinsicWidth, keyHeight / (float)intrinsicHeight);
bgWidth = (int)(intrinsicWidth * minScale);
bgHeight = (int)(intrinsicHeight * minScale);
bgX = (keyWidth - bgWidth) / 2;
bgY = (keyHeight - bgHeight) / 2;
} else {
final Rect padding = mKeyBackgroundPadding;
bgWidth = keyWidth + padding.left + padding.right;
bgHeight = keyHeight + padding.top + padding.bottom;
bgX = -padding.left;
bgY = -padding.top;
}
final Rect bounds = background.getBounds();
if (bgWidth != bounds.right || bgHeight != bounds.bottom) {
background.setBounds(0, 0, bgWidth, bgHeight);
}
canvas.translate(bgX, bgY);
background.draw(canvas);
canvas.translate(-bgX, -bgY);
}
示例11: getBitmapFromDrawable
import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
private Bitmap getBitmapFromDrawable(Drawable drawable) {
if (drawable == null) {
return null;
}
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
}
try {
Bitmap bitmap;
if (drawable instanceof ColorDrawable) {
bitmap = Bitmap.createBitmap(COLOR_DRAWABLE_DIMENSION, COLOR_DRAWABLE_DIMENSION, BITMAP_CONFIG);
} else {
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), BITMAP_CONFIG);
}
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
示例12: getBitmapFromDrawable
import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
static Bitmap getBitmapFromDrawable(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
} else {
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
}
示例13: generateIconBitmaps
import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
private Bitmap[] generateIconBitmaps(Drawable drawable) {
if (drawable == null)
return null;
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return generateIconBitmaps(Bitmap.createScaledBitmap(bitmap, iconSize, iconSize, false));
}
示例14: drawableToBitmap
import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
/**
* Convert a drawable object into a Bitmap.
* @param drawable Drawable to extract a Bitmap from.
* @return A Bitmap created from the drawable parameter.
*/
public Bitmap drawableToBitmap(Drawable drawable) {
if (drawable == null) // Don't do anything without a proper drawable
return null;
else if (drawable instanceof BitmapDrawable) { // Use the getBitmap() method instead if BitmapDrawable
Log.i(TAG, "Bitmap drawable!");
return ((BitmapDrawable) drawable).getBitmap();
}
int intrinsicWidth = drawable.getIntrinsicWidth();
int intrinsicHeight = drawable.getIntrinsicHeight();
if (!(intrinsicWidth > 0 && intrinsicHeight > 0))
return null;
try {
// Create Bitmap object out of the drawable
Bitmap bitmap = Bitmap.createBitmap(intrinsicWidth, intrinsicHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
} catch (OutOfMemoryError e) {
// Simply return null of failed bitmap creations
Log.e(TAG, "Encountered OutOfMemoryError while generating bitmap!");
return null;
}
}
示例15: getBitmapFromDrawable
import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
private Bitmap getBitmapFromDrawable(Drawable drawable) {
if (drawable == null) {
return null;
}
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
}
try {
Bitmap bitmap;
if (drawable instanceof ColorDrawable) {
bitmap = Bitmap.createBitmap(COLORDRAWABLE_DIMENSION, COLORDRAWABLE_DIMENSION, BITMAP_CONFIG);
} else {
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), BITMAP_CONFIG);
}
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}