下面列出了android.graphics.PaintFlagsDrawFilter#android.graphics.Bitmap.Config 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private Bitmap bitMatrix2Bitmap(BitMatrix matrix) {
int w = matrix.getWidth();
int h = matrix.getHeight();
int[] rawData = new int[w * h];
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
int color = Color.WHITE;
if (matrix.get(i, j)) {
color = Color.BLACK;
}
rawData[i + (j * w)] = color;
}
}
Bitmap bitmap = Bitmap.createBitmap(w, h, Config.RGB_565);
bitmap.setPixels(rawData, 0, w, 0, 0, w, h);
return bitmap;
}
private static Bitmap compressManageAspect(final int height,
final int width, final Bitmap originalImage) {
final Bitmap compressedBitmap = Bitmap
.createBitmap(width, height, Config.ARGB_8888);
final float originalWidth = originalImage.getWidth(), originalHeight = originalImage
.getHeight();
final Canvas canvas = new Canvas(compressedBitmap);
final float scale = width / originalWidth;
final float xTranslation = 0.0f, yTranslation = (height - (originalHeight * scale)) / 2.0f;
final Matrix transformation = new Matrix();
transformation.postTranslate(xTranslation, yTranslation);
transformation.preScale(scale, scale);
final Paint paint = new Paint();
paint.setFilterBitmap(true);
canvas.drawBitmap(originalImage, transformation, paint);
return compressedBitmap;
}
@Override public @Nullable Resource<Bitmap> decode(File file, int w, int h, Options options) throws IOException {
ByteBuffer buffer = ByteBuffer.allocate(w * h * 4);
FileInputStream stream = new FileInputStream(file);
try {
stream.getChannel().read(buffer);
} finally {
stream.close();
}
Bitmap result = Bitmap.createBitmap(w, h, Config.ARGB_8888);
try {
buffer.rewind();
result.copyPixelsFromBuffer(buffer);
return BitmapResource.obtain(result, pool);
} catch (RuntimeException ex) {
result.recycle();
throw ex;
}
}
/**
* 生成水印图片 水印在右下角
*
* @param src the bitmap object you want proecss
* @param watermark the water mark above the src
* @return return a bitmap object ,if paramter's length is 0,return null
*/
public static Bitmap createWatermarkBitmap(Bitmap src, Bitmap watermark) {
if (src == null) {
return null;
}
int w = src.getWidth();
int h = src.getHeight();
int ww = watermark.getWidth();
int wh = watermark.getHeight();
// create the new blank bitmap
Bitmap newb = Bitmap.createBitmap(w, h, Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图
Canvas cv = new Canvas(newb);
// draw src into
cv.drawBitmap(src, 0, 0, null);// 在 0,0坐标开始画入src
// draw watermark into
cv.drawBitmap(watermark, w - ww + 5, h - wh + 5, null);// 在src的右下角画入水印
// save all clip
cv.save(Canvas.ALL_SAVE_FLAG);// 保存
// store
cv.restore();// 存储
return newb;
}
@Override
public Bitmap transform(final Bitmap source)
{
final Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
Bitmap output = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
canvas.drawRoundRect(new RectF(margin, margin, source.getWidth() - margin, source.getHeight() - margin), radius, radius, paint);
if (source != output) {
source.recycle();
}
return output;
}
public void buildCircularRevealCache() {
if (STRATEGY == BITMAP_SHADER) {
buildingCircularRevealCache = true;
hasCircularRevealCache = false;
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
if (bitmap == null && view.getWidth() != 0 && view.getHeight() != 0) {
bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
}
if (bitmap != null) {
revealPaint.setShader(new BitmapShader(bitmap, TileMode.CLAMP, TileMode.CLAMP));
}
buildingCircularRevealCache = false;
hasCircularRevealCache = true;
}
}
public static Bitmap drawableToBitmap(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
}
try {
Bitmap bitmap = Bitmap.createBitmap(Math.max(drawable.getIntrinsicWidth(), 2), Math.max(drawable.getIntrinsicHeight(), 2), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
} catch (Exception e) {
e.printStackTrace();
Log.w(TAG, "Failed to create bitmap from drawable!");
return null;
}
}
/**
* 返回马赛克最终结果
*
* @return 马赛克最终结果
*/
public Bitmap getMosaicBitmap()
{
if (bmMosaicLayer == null)
{
return null;
}
Bitmap bitmap = Bitmap.createBitmap(mImageWidth, mImageHeight,
Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(bmBaseLayer, 0, 0, null);
canvas.drawBitmap(bmMosaicLayer, 0, 0, null);
canvas.save();
return bitmap;
}
private static final boolean b(String str, int i, int i2) {
if (TextUtils.isEmpty(str)) {
return false;
}
Options options = new Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(str, options);
int i3 = options.outWidth;
int i4 = options.outHeight;
if (options.mCancel || options.outWidth == -1 || options.outHeight == -1) {
return false;
}
int i5 = i3 > i4 ? i3 : i4;
if (i3 >= i4) {
i3 = i4;
}
f.b("AsynScaleCompressImage", "longSide=" + i5 + "shortSide=" + i3);
options.inPreferredConfig = Config.RGB_565;
if (i5 > i2 || i3 > i) {
return true;
}
return false;
}
@SuppressLint({"NewApi"})
public static void blur_3(Context context, Bitmap bkg, View view) {
long startMs = System.currentTimeMillis();
Display d = ((Activity) context).getWindowManager().getDefaultDisplay();
Bitmap overlay = Bitmap.createBitmap((int) (((float) d.getWidth()) / 1.0f), (int) (((float) d.getHeight()) / 1.0f), Config.ARGB_8888);
Canvas canvas = new Canvas(overlay);
canvas.scale(1.0f / 1.0f, 1.0f / 1.0f);
Paint paint = new Paint();
paint.setFlags(2);
canvas.drawBitmap(bkg, 0.0f, 0.0f, paint);
overlay = FastBlur.doBlur(overlay, (int) 100.0f, true);
if (LetvUtils.getSDKVersion() >= 16) {
view.setBackground(new BitmapDrawable(context.getResources(), overlay));
} else {
view.setBackgroundDrawable(new BitmapDrawable(context.getResources(), overlay));
}
}
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
if (null == bitmap) {
return bitmap;
}
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = 10;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
/**
* 获得圆角的Bitmap
*
* @param bitmap 源Bitmap
* @param roundPx 圆角大小
* @return 期望Bitmap
*/
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
private Bitmap getResBitmap(int bmpResId) {
Options opts = new Options();
opts.inDither = false;
Resources res = getResources();
Bitmap bmp = BitmapFactory.decodeResource(res, bmpResId, opts);
if (bmp == null && isInEditMode()) {
// BitmapFactory.decodeResource doesn't work from the rendering
// library in Eclipse's Graphical Layout Editor. Use this workaround instead.
Drawable d = res.getDrawable(bmpResId);
int w = d.getIntrinsicWidth();
int h = d.getIntrinsicHeight();
bmp = Bitmap.createBitmap(w, h, Config.ARGB_8888);
Canvas c = new Canvas(bmp);
d.setBounds(0, 0, w - 1, h - 1);
d.draw(c);
}
return bmp;
}
@Override
public Bitmap onLoadBitmap(final Config pBitmapConfig) {
final Picture picture = this.mPicture;
if(picture == null) {
Debug.e("Failed loading Bitmap in " + this.getClass().getSimpleName() + ".");
return null;
}
final Bitmap bitmap = Bitmap.createBitmap(this.mTextureWidth, this.mTextureHeight, pBitmapConfig);
final Canvas canvas = new Canvas(bitmap);
final float scaleX = (float)this.mTextureWidth / this.mPicture.getWidth();
final float scaleY = (float)this.mTextureHeight / this.mPicture.getHeight();
canvas.scale(scaleX, scaleY, 0, 0);
picture.draw(canvas);
return bitmap;
}
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = pixels;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
public void createImage(Bitmap.CompressFormat format, int style,
float size, String out, Integer bg, Integer fg, boolean transparency)
throws IOException {
TeXIcon icon = createTeXIcon(style, size);
icon.setInsets(new Insets(1, 1, 1, 1));
int w = icon.getIconWidth(), h = icon.getIconHeight();
Bitmap image = Bitmap.createBitmap(w, h, Config.ARGB_8888);
Canvas g2 = new Canvas(image);
if (bg != null) {
Paint st = new Paint();
st.setStyle(Style.FILL_AND_STROKE);
st.setColor(bg);
g2.drawRect(0, 0, w, h, st);
}
icon.setForeground(fg == null ? Color.BLACK : fg);
icon.paintIcon(g2, 0, 0);
File file = new File(out);
FileOutputStream imout = new FileOutputStream(file);
image.compress(format, 90, imout);
imout.flush();
imout.close();
}
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;
}
public static Bitmap getPreviewBitmap(float density, int color) {
if (density == 0) {
density = 1.5f;
}
int d = (int) (density * 31); // 30dip
Bitmap bm = Bitmap.createBitmap(d, d, Config.ARGB_8888);
int w = bm.getWidth();
int h = bm.getHeight();
int c = color;
for (int i = 0; i < w; i++) {
for (int j = i; j < h; j++) {
c = (i <= 1 || j <= 1 || i >= w - 2 || j >= h - 2) ? Color.GRAY : color;
bm.setPixel(i, j, c);
if (i != j) {
bm.setPixel(j, i, c);
}
}
}
return bm;
}
@Override
public void onPreviewSizeChosen(final Size size, final int rotation) {
previewWidth = size.getWidth();
previewHeight = size.getHeight();
final int sensorOrientation = rotation - getScreenOrientation();
rgbFrameBitmap = Bitmap.createBitmap(previewWidth, previewHeight, Config.ARGB_8888);
croppedBitmap = Bitmap.createBitmap(INPUT_SIZE, INPUT_SIZE, Config.ARGB_8888);
frameToCropTransform = ImageUtils.getTransformationMatrix(
previewWidth, previewHeight,
INPUT_SIZE, INPUT_SIZE,
sensorOrientation, MAINTAIN_ASPECT);
final Matrix cropToFrameTransform = new Matrix();
frameToCropTransform.invert(cropToFrameTransform);
}
/**
* @param bitmap
* @return
*/
public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
final int reflectionGap = 4;
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix matrix = new Matrix();
matrix.preScale(1, -1);
Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, h / 2, w, h / 2, matrix, false);
Bitmap bitmapWithReflection = Bitmap.createBitmap(w, (h + h / 2), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmapWithReflection);
canvas.drawBitmap(bitmap, 0, 0, null);
Paint deafalutPaint = new Paint();
canvas.drawRect(0, h, w, h + reflectionGap, deafalutPaint);
canvas.drawBitmap(reflectionImage, 0, h + reflectionGap, null);
Paint paint = new Paint();
LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0, bitmapWithReflection.getHeight()
+ reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
paint.setShader(shader);
// Set the Transfer mode to be porter duff and destination in
paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
// Draw a rectangle using the paint with our linear gradient
canvas.drawRect(0, h, w, bitmapWithReflection.getHeight() + reflectionGap, paint);
return bitmapWithReflection;
}
/**
* 将彩色图转换为黑白图
*
* @param bmp 位图
* @return 返回转换好的位图
*/
public static Bitmap convertToBlackWhite(Bitmap bmp) {
int width = bmp.getWidth();
int height = bmp.getHeight();
int[] pixels = new int[width * height];
bmp.getPixels(pixels, 0, width, 0, 0, width, height);
int alpha = 0xFF << 24; // 默认将bitmap当成24色图片
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int grey = pixels[width * i + j];
int red = ((grey & 0x00FF0000) >> 16);
int green = ((grey & 0x0000FF00) >> 8);
int blue = (grey & 0x000000FF);
grey = (int) (red * 0.3 + green * 0.59 + blue * 0.11);
grey = alpha | (grey << 16) | (grey << 8) | grey;
pixels[width * i + j] = grey;
}
}
Bitmap newBmp = Bitmap.createBitmap(width, height, Config.RGB_565);
newBmp.setPixels(pixels, 0, width, 0, 0, width, height);
return newBmp;
}
public void createImage(Bitmap.CompressFormat format, int style,
float size, String out, Integer bg, Integer fg, boolean transparency)
throws IOException {
TeXIcon icon = createTeXIcon(style, size);
icon.setInsets(new Insets(1, 1, 1, 1));
int w = icon.getIconWidth(), h = icon.getIconHeight();
Bitmap image = Bitmap.createBitmap(w, h, Config.ARGB_8888);
Canvas g2 = new Canvas(image);
if (bg != null) {
Paint st = new Paint();
st.setStyle(Style.FILL_AND_STROKE);
st.setColor(bg);
g2.drawRect(0, 0, w, h, st);
}
icon.setForeground(fg == null ? Color.BLACK : fg);
icon.paintIcon(g2, 0, 0);
File file = new File(out);
FileOutputStream imout = new FileOutputStream(file);
image.compress(format, 90, imout);
imout.flush();
imout.close();
}
public UEImage transformRound(float radius){
final int width = bitmap.getWidth();
final int height = bitmap.getHeight();
final int color = 0xff888888;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, width, height);
final RectF rectF = new RectF(rect);
if (radius == TRANSFORMROUND_CIRCLE) {
radius = width > height ? width/2 : height/2;
}
Bitmap mBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(mBitmap);
paint.setAntiAlias(true);
paint.setColor(color);
canvas.drawARGB(0, 0, 0, 0);
canvas.drawRoundRect(rectF, radius, radius, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
this.bitmap = mBitmap;
return this;
}
public static Bitmap drawableToBitmap(Drawable d) {
if(d instanceof BitmapDrawable) {
return ((BitmapDrawable) d).getBitmap();
}
int w = d.getIntrinsicWidth();
w = w > 0 ? w : 1;
int h = d.getIntrinsicHeight();
h = h > 0 ? h : 1;
Bitmap b = Bitmap.createBitmap(w, h, Config.ARGB_8888);
Canvas c = new Canvas(b);
d.setBounds(0, 0, c.getWidth(), c.getHeight());
d.draw(c);
return b;
}
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;
}
private void verifyResize(
NetworkResponse networkResponse,
int maxWidth,
int maxHeight,
ScaleType scaleType,
int expectedWidth,
int expectedHeight) {
ImageRequest request =
new ImageRequest("", null, maxWidth, maxHeight, scaleType, Config.RGB_565, null);
Response<Bitmap> response = request.parseNetworkResponse(networkResponse);
assertNotNull(response);
assertTrue(response.isSuccess());
Bitmap bitmap = response.result;
assertNotNull(bitmap);
assertEquals(expectedWidth, bitmap.getWidth());
assertEquals(expectedHeight, bitmap.getHeight());
}
/**
* 从 Drawable 中获取 Bitmap 对象
*/
private Bitmap drawable2Bitmap(Drawable drawable) {
try {
if (drawable == null) {
return null;
}
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
}
int intrinsicWidth = drawable.getIntrinsicWidth();
int intrinsicHeight = drawable.getIntrinsicHeight();
Bitmap bitmap = Bitmap.createBitmap(intrinsicWidth <= 0 ? DEFAULT_OTHER_DRAWABLE_WIDTH : intrinsicWidth, intrinsicHeight <= 0 ? DEFAULT_OTHER_DRAWABLE_HEIGHT : intrinsicHeight, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
} catch (OutOfMemoryError e) {
return null;
}
}
/**
* @return 涟漪的bitmap
*/
public Bitmap makeCircle() {
// 画涟漪时要考虑到按钮的边界区域,不要把按钮的阴影边界也填满了
Bitmap output = Bitmap.createBitmap(
getWidth() - Utils.dpToPx(6, getResources()),
getHeight() - Utils.dpToPx(7, getResources()), Config.ARGB_8888);
return makeCircleFromBitmap(output);
}
/**
* Utility function for creating a scaled version of an existing bitmap
*
* @param unscaledBitmap Bitmap to scale
* @param dstWidth Wanted width of destination bitmap
* @param dstHeight Wanted height of destination bitmap
* @param scalingLogic Logic to use to avoid image stretching
* @return New scaled bitmap object
*/
public static Bitmap createScaledBitmap(Bitmap unscaledBitmap, int dstWidth, int dstHeight,
ScalingLogic scalingLogic) {
Rect srcRect = calculateSrcRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(),
dstWidth, dstHeight, scalingLogic);
Rect dstRect = calculateDstRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(),
dstWidth, dstHeight, scalingLogic);
Bitmap scaledBitmap = Bitmap.createBitmap(dstRect.width(), dstRect.height(),
Config.ARGB_8888);
Canvas canvas = new Canvas(scaledBitmap);
canvas.drawBitmap(unscaledBitmap, srcRect, dstRect, new Paint(Paint.FILTER_BITMAP_FLAG));
return scaledBitmap;
}
public boolean loadInBackground() {
ExifInterface ei = new ExifInterface();
if (readExif(ei)) {
Integer ori = ei.getTagIntValue(ExifInterface.TAG_ORIENTATION);
if (ori != null) {
mRotation = ExifInterface.getRotationForOrientationValue(ori.shortValue());
}
}
mDecoder = loadBitmapRegionDecoder();
if (mDecoder == null) {
mState = State.ERROR_LOADING;
return false;
} else {
int width = mDecoder.getWidth();
int height = mDecoder.getHeight();
if (mPreviewSize != 0) {
int previewSize = Math.min(mPreviewSize, MAX_PREVIEW_SIZE);
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
opts.inPreferQualityOverSpeed = true;
float scale = (float) previewSize / Math.max(width, height);
opts.inSampleSize = BitmapUtils.computeSampleSizeLarger(scale);
opts.inJustDecodeBounds = false;
mPreview = loadPreviewBitmap(opts);
}
mState = State.LOADED;
return true;
}
}