use of android.graphics.BitmapShader in project Klyph by jonathangerbaud.
the class BitmapUtil method getCirleBitmap.
public static Bitmap getCirleBitmap(Bitmap bitmap, int borderColor, int border) {
if (bitmap == null)
return null;
int side = (int) Math.min(bitmap.getWidth(), bitmap.getHeight());
int x = (int) (bitmap.getWidth() - side) / 2;
int y = (int) (bitmap.getHeight() - side) / 2;
Bitmap b = Bitmap.createBitmap(bitmap, x, y, side, side, new Matrix(), true);
BitmapShader bitmapShader = new BitmapShader(b, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(bitmapShader);
Paint bPaint = new Paint();
bPaint.setAntiAlias(true);
bPaint.setColor(borderColor);
bPaint.setStrokeWidth(border / 2);
bPaint.setStyle(Style.STROKE);
Bitmap returnBitmap;
try {
returnBitmap = Bitmap.createBitmap(side, side, Config.ARGB_8888);
} catch (OutOfMemoryError e) {
return null;
}
Canvas canvas = new Canvas(returnBitmap);
float center = (float) side / 2;
float radius = (float) (side - border / 2) / 2;
canvas.drawCircle(center, center, center, paint);
canvas.drawCircle(center, center, radius, bPaint);
return returnBitmap;
}
use of android.graphics.BitmapShader in project ImagePicker by jeasonlzy.
the class CropImageView method makeCropBitmap.
/**
* @param bitmap 需要裁剪的图片
* @param focusRect 中间需要裁剪的矩形区域
* @param imageMatrixRect 当前图片在屏幕上的显示矩形区域
* @param expectWidth 希望获得的图片宽度,如果图片宽度不足时,拉伸图片
* @param exceptHeight 希望获得的图片高度,如果图片高度不足时,拉伸图片
* @param isSaveRectangle 是否希望按矩形区域保存图片
* @return 裁剪后的图片的Bitmap
*/
private Bitmap makeCropBitmap(Bitmap bitmap, RectF focusRect, RectF imageMatrixRect, int expectWidth, int exceptHeight, boolean isSaveRectangle) {
float scale = imageMatrixRect.width() / bitmap.getWidth();
int left = (int) ((focusRect.left - imageMatrixRect.left) / scale);
int top = (int) ((focusRect.top - imageMatrixRect.top) / scale);
int width = (int) (focusRect.width() / scale);
int height = (int) (focusRect.height() / scale);
if (left < 0)
left = 0;
if (top < 0)
top = 0;
if (left + width > bitmap.getWidth())
width = bitmap.getWidth() - left;
if (top + height > bitmap.getHeight())
height = bitmap.getHeight() - top;
try {
bitmap = Bitmap.createBitmap(bitmap, left, top, width, height);
if (expectWidth != width || exceptHeight != height) {
bitmap = Bitmap.createScaledBitmap(bitmap, expectWidth, exceptHeight, true);
if (mStyle == CropImageView.Style.CIRCLE && !isSaveRectangle) {
//如果是圆形,就将图片裁剪成圆的
int length = Math.min(expectWidth, exceptHeight);
int radius = length / 2;
Bitmap circleBitmap = Bitmap.createBitmap(length, length, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(circleBitmap);
BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
Paint paint = new Paint();
paint.setShader(bitmapShader);
canvas.drawCircle(expectWidth / 2f, exceptHeight / 2f, radius, paint);
bitmap = circleBitmap;
}
}
} catch (OutOfMemoryError e) {
e.printStackTrace();
}
return bitmap;
}
use of android.graphics.BitmapShader in project fresco by facebook.
the class RoundedBitmapDrawable method updatePaint.
private void updatePaint() {
Bitmap bitmap = getBitmap();
if (mLastBitmap == null || mLastBitmap.get() != bitmap) {
mLastBitmap = new WeakReference<Bitmap>(bitmap);
mPaint.setShader(new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
mIsShaderTransformDirty = true;
}
if (mIsShaderTransformDirty) {
mPaint.getShader().setLocalMatrix(mTransform);
mIsShaderTransformDirty = false;
}
}
use of android.graphics.BitmapShader in project InstaMaterial by frogermcs.
the class CircleTransformation method transform.
@Override
public Bitmap transform(Bitmap source) {
int size = Math.min(source.getWidth(), source.getHeight());
int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2;
Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
if (squaredBitmap != source) {
source.recycle();
}
Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());
Canvas canvas = new Canvas(bitmap);
Paint avatarPaint = new Paint();
BitmapShader shader = new BitmapShader(squaredBitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
avatarPaint.setShader(shader);
Paint outlinePaint = new Paint();
outlinePaint.setColor(Color.WHITE);
outlinePaint.setStyle(Paint.Style.STROKE);
outlinePaint.setStrokeWidth(STROKE_WIDTH);
outlinePaint.setAntiAlias(true);
float r = size / 2f;
canvas.drawCircle(r, r, r, avatarPaint);
canvas.drawCircle(r, r, r - STROKE_WIDTH / 2, outlinePaint);
squaredBitmap.recycle();
return bitmap;
}
use of android.graphics.BitmapShader in project nmid-headline by miao1007.
the class CircleTransformation method transform.
@Override
public Bitmap transform(Bitmap source) {
int size = Math.min(source.getWidth(), source.getHeight());
int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2;
Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
if (squaredBitmap != source) {
source.recycle();
}
Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
BitmapShader shader = new BitmapShader(squaredBitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
paint.setShader(shader);
paint.setAntiAlias(true);
float r = size / 2f;
// Prepare the background
Paint paintBg = new Paint();
paintBg.setColor(BORDER_COLOR);
paintBg.setAntiAlias(true);
// Draw the background circle
canvas.drawCircle(r, r, r, paintBg);
// Draw the image smaller than the background so a little border will be seen
canvas.drawCircle(r, r, r - BORDER_RADIUS, paint);
squaredBitmap.recycle();
return bitmap;
}
Aggregations