本文整理匯總了Java中android.graphics.Canvas.setBitmap方法的典型用法代碼示例。如果您正苦於以下問題:Java Canvas.setBitmap方法的具體用法?Java Canvas.setBitmap怎麽用?Java Canvas.setBitmap使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類android.graphics.Canvas
的用法示例。
在下文中一共展示了Canvas.setBitmap方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: createBitmapFromView
import android.graphics.Canvas; //導入方法依賴的package包/類
/**
* 從一個view創建Bitmap。
* 注意點:繪製之前要清掉 View 的焦點,因為焦點可能會改變一個 View 的 UI 狀態。
* 來源:https://github.com/tyrantgit/ExplosionField
*
* @param view 傳入一個 View,會獲取這個 View 的內容創建 Bitmap。
* @param scale 縮放比例,對創建的 Bitmap 進行縮放,數值支持從 0 到 1。
*/
public static Bitmap createBitmapFromView(View view, float scale) {
if (view instanceof ImageView) {
Drawable drawable = ((ImageView) view).getDrawable();
if (drawable != null && drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
}
}
view.clearFocus();
Bitmap bitmap = createBitmapSafely((int) (view.getWidth() * scale),
(int) (view.getHeight() * scale), Bitmap.Config.ARGB_8888, 1);
if (bitmap != null) {
synchronized (sCanvas) {
Canvas canvas = sCanvas;
canvas.setBitmap(bitmap);
canvas.save();
canvas.drawColor(Color.WHITE); // 防止 View 上麵有些區域空白導致最終 Bitmap 上有些區域變黑
canvas.scale(scale, scale);
view.draw(canvas);
canvas.restore();
canvas.setBitmap(null);
}
}
return bitmap;
}
示例2: toRoundCornerMutableBitmap
import android.graphics.Canvas; //導入方法依賴的package包/類
private static Bitmap toRoundCornerMutableBitmap(Canvas canvas, Paint paintClear, Bitmap srcBitmap, float[] roundRadius) {
canvas.setBitmap(srcBitmap);
RectF inset = new RectF(1, 1, 1, 1);
if (roundRadius.length == 2) {
float[] tmpRoundRadius = new float[8];
for (int i = 0; i < 4; ++i) {
tmpRoundRadius[i * 2 + 0] = roundRadius[0];
tmpRoundRadius[i * 2 + 1] = roundRadius[1];
roundRadius = tmpRoundRadius;
}
}
canvas.save();
canvas.translate(-1, -1);
RoundRectShape roundRectShape = new RoundRectShape(null, inset, roundRadius);
roundRectShape.resize(srcBitmap.getWidth() + 2, srcBitmap.getHeight() + 2);
roundRectShape.draw(canvas, paintClear);
canvas.restore();
canvas.setBitmap(null);
return srcBitmap;
}
示例3: drawScaledPreview
import android.graphics.Canvas; //導入方法依賴的package包/類
private Bitmap drawScaledPreview(Canvas canvas, Bitmap.Config config) {
Drawable d = mView.getBackground();
Rect bounds = getDrawableBounds(d);
int size = Launcher.getLauncher(mView.getContext()).getDeviceProfile().iconSizePx;
final Bitmap b = Bitmap.createBitmap(
size + DRAG_BITMAP_PADDING,
size + DRAG_BITMAP_PADDING,
config);
canvas.setBitmap(b);
canvas.save(Canvas.MATRIX_SAVE_FLAG);
canvas.translate(DRAG_BITMAP_PADDING / 2, DRAG_BITMAP_PADDING / 2);
canvas.scale(((float) size) / bounds.width(), ((float) size) / bounds.height(), 0, 0);
canvas.translate(bounds.left, bounds.top);
d.draw(canvas);
canvas.restore();
return b;
}
示例4: createDragOutline
import android.graphics.Canvas; //導入方法依賴的package包/類
@Override
public Bitmap createDragOutline(Canvas canvas) {
final Bitmap b = Bitmap.createBitmap(mOutlineSize[0], mOutlineSize[1], Bitmap.Config.ALPHA_8);
canvas.setBitmap(b);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.FILL);
// Use 0.9f times the radius for the actual circle to account for icon normalization.
float radius = getPreviewBounds().width() * 0.5f;
canvas.drawCircle(DRAG_BITMAP_PADDING / 2 + radius,
DRAG_BITMAP_PADDING / 2 + radius, radius * 0.9f, paint);
HolographicOutlineHelper.obtain(mLauncher).applyExpensiveOutlineWithBlur(b, canvas);
canvas.setBitmap(null);
return b;
}
示例5: createDragBitmap
import android.graphics.Canvas; //導入方法依賴的package包/類
/**
* Returns a new bitmap to show when the {@link #mView} is being dragged around.
* Responsibility for the bitmap is transferred to the caller.
*/
public Bitmap createDragBitmap(Canvas canvas) {
Bitmap b;
if (mView instanceof TextView) {
Drawable d = Workspace.getTextViewIcon((TextView) mView);
Rect bounds = getDrawableBounds(d);
b = Bitmap.createBitmap(bounds.width() + DRAG_BITMAP_PADDING,
bounds.height() + DRAG_BITMAP_PADDING, Bitmap.Config.ARGB_8888);
} else {
b = Bitmap.createBitmap(mView.getWidth() + DRAG_BITMAP_PADDING,
mView.getHeight() + DRAG_BITMAP_PADDING, Bitmap.Config.ARGB_8888);
}
canvas.setBitmap(b);
drawDragView(canvas);
canvas.setBitmap(null);
return b;
}
示例6: resource2Bitmap
import android.graphics.Canvas; //導入方法依賴的package包/類
private Bitmap resource2Bitmap(final Context context, final int resourceId) {
Drawable drawable = ContextCompat.getDrawable(context, resourceId);
Canvas canvas = new Canvas();
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
canvas.setBitmap(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
}
示例7: resource2Bitmap
import android.graphics.Canvas; //導入方法依賴的package包/類
private Bitmap resource2Bitmap(final int resourceId) {
Drawable drawable = ContextCompat.getDrawable(Utils.getApp(), resourceId);
Canvas canvas = new Canvas();
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
canvas.setBitmap(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
}
示例8: createDragOutline
import android.graphics.Canvas; //導入方法依賴的package包/類
@Override
public Bitmap createDragOutline(Canvas canvas) {
Bitmap b = drawScaledPreview(canvas, Bitmap.Config.ALPHA_8);
HolographicOutlineHelper.obtain(mView.getContext())
.applyExpensiveOutlineWithBlur(b, canvas);
canvas.setBitmap(null);
return b;
}
示例9: drawToBitmap
import android.graphics.Canvas; //導入方法依賴的package包/類
@Nullable
private static Bitmap drawToBitmap(
BitmapPool bitmapPool, Drawable drawable, int width, int height) {
if (width == Target.SIZE_ORIGINAL && drawable.getIntrinsicWidth() <= 0) {
if (Log.isLoggable(TAG, Log.WARN)) {
Log.w(TAG, "Unable to draw " + drawable + " to Bitmap with Target.SIZE_ORIGINAL because the"
+ " Drawable has no intrinsic width");
}
return null;
}
if (height == Target.SIZE_ORIGINAL && drawable.getIntrinsicHeight() <= 0) {
if (Log.isLoggable(TAG, Log.WARN)) {
Log.w(TAG, "Unable to draw " + drawable + " to Bitmap with Target.SIZE_ORIGINAL because the"
+ " Drawable has no intrinsic height");
}
return null;
}
int targetWidth = drawable.getIntrinsicWidth() > 0 ? drawable.getIntrinsicWidth() : width;
int targetHeight = drawable.getIntrinsicHeight() > 0 ? drawable.getIntrinsicHeight() : height;
Lock lock = TransformationUtils.getBitmapDrawableLock();
lock.lock();
Bitmap result = bitmapPool.get(targetWidth, targetHeight, Bitmap.Config.ARGB_8888);
try {
Canvas canvas = new Canvas(result);
drawable.setBounds(0, 0, targetWidth, targetHeight);
drawable.draw(canvas);
canvas.setBitmap(null);
} finally {
lock.unlock();
}
return result;
}
示例10: createDragBitmap
import android.graphics.Canvas; //導入方法依賴的package包/類
/**
* Returns a new bitmap to show when the {@link #mView} is being dragged around.
* Responsibility for the bitmap is transferred to the caller.
*/
public Bitmap createDragBitmap(Canvas canvas) {
float scale = 1f;
int width = mView.getWidth();
int height = mView.getHeight();
if (mView instanceof TextView) {
Drawable d = Workspace.getTextViewIcon((TextView) mView);
Rect bounds = getDrawableBounds(d);
width = bounds.width();
height = bounds.height();
} else if (mView instanceof LauncherAppWidgetHostView) {
scale = ((LauncherAppWidgetHostView) mView).getScaleToFit();
width = (int) (mView.getWidth() * scale);
height = (int) (mView.getHeight() * scale);
}
Bitmap b = Bitmap.createBitmap(width + blurSizeOutline, height + blurSizeOutline,
Bitmap.Config.ARGB_8888);
canvas.setBitmap(b);
canvas.save();
canvas.scale(scale, scale);
drawDragView(canvas);
canvas.restore();
canvas.setBitmap(null);
return b;
}
示例11: toRoundCornerBitmap
import android.graphics.Canvas; //導入方法依賴的package包/類
private static Bitmap toRoundCornerBitmap(Canvas canvas, Paint paintClear, Bitmap srcBitmap, float[] roundRadius) {
if (srcBitmap.isMutable() && srcBitmap.hasAlpha()) {
return toRoundCornerMutableBitmap(canvas, paintClear, srcBitmap, roundRadius);
}
Bitmap output;
if (false) {
output = BitmapTools.createBitmapFromRecycledBitmaps(srcBitmap.getWidth(), srcBitmap.getHeight(), XulManager.DEF_PIXEL_FMT);
} else {
output = BitmapTools.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), XulManager.DEF_PIXEL_FMT);
}
canvas.setBitmap(output);
Rect rect = new Rect(0, 0, srcBitmap.getWidth(), srcBitmap.getHeight());
canvas.drawBitmap(srcBitmap, rect, rect, null);
return toRoundCornerMutableBitmap(canvas, paintClear, output, roundRadius);
}
示例12: createDragOutline
import android.graphics.Canvas; //導入方法依賴的package包/類
/**
* Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location.
* Responsibility for the bitmap is transferred to the caller.
*/
public Bitmap createDragOutline(Canvas canvas) {
final Bitmap b = Bitmap.createBitmap(mView.getWidth() + DRAG_BITMAP_PADDING,
mView.getHeight() + DRAG_BITMAP_PADDING, Bitmap.Config.ALPHA_8);
canvas.setBitmap(b);
drawDragView(canvas);
HolographicOutlineHelper.obtain(mView.getContext())
.applyExpensiveOutlineWithBlur(b, canvas);
canvas.setBitmap(null);
return b;
}
示例13: getDefaultWallpaper
import android.graphics.Canvas; //導入方法依賴的package包/類
private static DefaultWallpaperInfo getDefaultWallpaper(Context context) {
File defaultThumbFile = getDefaultThumbFile(context);
Bitmap thumb = null;
boolean defaultWallpaperExists = false;
Resources res = context.getResources();
if (defaultThumbFile.exists()) {
thumb = BitmapFactory.decodeFile(defaultThumbFile.getAbsolutePath());
defaultWallpaperExists = true;
} else {
Point defaultThumbSize = getDefaultThumbSize(res);
Drawable wallpaperDrawable = WallpaperManager.getInstance(context).getBuiltInDrawable(
defaultThumbSize.x, defaultThumbSize.y, true, 0.5f, 0.5f);
if (wallpaperDrawable != null) {
thumb = Bitmap.createBitmap(
defaultThumbSize.x, defaultThumbSize.y, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(thumb);
wallpaperDrawable.setBounds(0, 0, defaultThumbSize.x, defaultThumbSize.y);
wallpaperDrawable.draw(c);
c.setBitmap(null);
}
if (thumb != null) {
defaultWallpaperExists = saveDefaultWallpaperThumb(context, thumb);
}
}
if (defaultWallpaperExists) {
return new DefaultWallpaperInfo(new BitmapDrawable(res, thumb));
}
return null;
}
示例14: createBitmap
import android.graphics.Canvas; //導入方法依賴的package包/類
private Bitmap createBitmap(int x, int y, int w, int h) {
Bitmap newBimap = delegate.getBitmap();
if (newBimap != null) {
bitmapToEdit = newBimap;
}
Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
Matrix matrix = new Matrix();
matrix.setTranslate(-bitmapToEdit.getWidth() / 2, -bitmapToEdit.getHeight() / 2);
matrix.postRotate(orientation);
if (orientation % 360 == 90 || orientation % 360 == 270) {
matrix.postTranslate(bitmapToEdit.getHeight() / 2 - x, bitmapToEdit.getWidth() / 2 - y);
} else {
matrix.postTranslate(bitmapToEdit.getWidth() / 2 - x, bitmapToEdit.getHeight() / 2 - y);
}
canvas.drawBitmap(bitmapToEdit, matrix, paint);
try {
canvas.setBitmap(null);
} catch (Exception e) {
//don't promt, this will crash on 2.x
}
return bitmap;
}
示例15: IconDrawer
import android.graphics.Canvas; //導入方法依賴的package包/類
IconDrawer(int padding) {
mPadding = padding;
mCircleClipBitmap = Bitmap.createBitmap(mSize, mSize, Bitmap.Config.ALPHA_8);
Canvas canvas = new Canvas();
canvas.setBitmap(mCircleClipBitmap);
canvas.drawCircle(mSize / 2, mSize / 2, mSize / 2 - padding, mPaint);
}