當前位置: 首頁>>代碼示例>>Java>>正文


Java Canvas.setBitmap方法代碼示例

本文整理匯總了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;
}
 
開發者ID:QMUI,項目名稱:QMUI_Android,代碼行數:33,代碼來源:QMUIDrawableHelper.java

示例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;
}
 
開發者ID:starcor-company,項目名稱:starcor.xul,代碼行數:21,代碼來源:XulWorker.java

示例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;
}
 
開發者ID:TeamBrainStorm,項目名稱:SimpleUILauncher,代碼行數:21,代碼來源:ShortcutDragPreviewProvider.java

示例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;
}
 
開發者ID:michelelacorte,項目名稱:FlickLauncher,代碼行數:19,代碼來源:ExternalDragPreviewProvider.java

示例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;
}
 
開發者ID:TeamBrainStorm,項目名稱:SimpleUILauncher,代碼行數:24,代碼來源:DragPreviewProvider.java

示例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;
}
 
開發者ID:Wilshion,項目名稱:HeadlineNews,代碼行數:10,代碼來源:SpanUtils.java

示例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;
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:10,代碼來源:SpanUtils.java

示例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;
}
 
開發者ID:michelelacorte,項目名稱:FlickLauncher,代碼行數:10,代碼來源:ShortcutDragPreviewProvider.java

示例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;
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:34,代碼來源:DrawableToBitmapConverter.java

示例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;
}
 
開發者ID:enricocid,項目名稱:LaunchEnr,代碼行數:34,代碼來源:DragPreviewProvider.java

示例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);
}
 
開發者ID:starcor-company,項目名稱:starcor.xul,代碼行數:16,代碼來源:XulWorker.java

示例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;
}
 
開發者ID:michelelacorte,項目名稱:FlickLauncher,代碼行數:15,代碼來源:DragPreviewProvider.java

示例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;
}
 
開發者ID:michelelacorte,項目名稱:FlickLauncher,代碼行數:31,代碼來源:DefaultWallpaperInfo.java

示例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;
}
 
開發者ID:chengzichen,項目名稱:KrGallery,代碼行數:28,代碼來源:PhotoCropView.java

示例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);
}
 
開發者ID:enricocid,項目名稱:LaunchEnr,代碼行數:8,代碼來源:BadgeRenderer.java


注:本文中的android.graphics.Canvas.setBitmap方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。